【问题标题】:Why does my table have no content in ASP.NET?为什么我的表格在 ASP.NET 中没有内容?
【发布时间】:2010-10-14 03:33:51
【问题描述】:

我在 GetData.cs 类中建立了一个表

public Table  BuildTable()
{
    Table tButtons = new Table();
    TableRow tRow = new TableRow();
    TableCell tCell = new TableCell();
    long lColumn = 0;
    long lPreviousColumn = 0;
    long lRow = 0;
    long lPreviousRow = 0;
    long lLanguage = 0;
    long lPreviousLanguage=0;
    OpenConnection();
    ButtonData();
    Int32 lRowOrd = aReader.GetOrdinal("RowNumber");
    Int32 lColOrd = aReader.GetOrdinal("ColumnNumber");
    Int32 lLangOrd = aReader.GetOrdinal("Language");
    Int32 lLabelOrd = aReader.GetOrdinal("Label");

    while (aReader.Read())
    {

        lRow = IsDbNull(aReader,lRowOrd);//first get our column number
        lColumn = IsDbNull(aReader,lColOrd);//first get our column number
        lLanguage = IsDbNull(aReader,lLangOrd);//first get our column number
        if (lPreviousRow != lRow)//we have a new row 
        {
            if (lPreviousRow != 0)//then we are working on one and need to save it before moving on
            {
                tButtons.Rows.Add(tRow);//add the new row to the table
            }
            lPreviousRow = lRow;//remember the value for next time
            tRow = new TableRow();
            tRow.Visible = true;
            //*******put the category titles in here somewhere
        }
        if (lPreviousColumn != lColumn)//we have a new column
        {
            if (lPreviousColumn != 0)//then we are working on one and need to save it before moving on
            {
                tRow.Cells.Add(tCell);//add the new cell to the row
            }
            lPreviousColumn = lColumn;//remember the value for next time
            //*******add the cell colors
            if (lPreviousLanguage != lLanguage)//we have a new column
            {
                lPreviousLanguage = lLanguage;//remember the value for next time
                tCell.Text = IsDbNull(aReader,lLabelOrd,"");
                //*******add the languages to properties
            }
            tCell = new TableCell();
            tCell.Visible=true;
        }
    }
    CloseConnection();
    tButtons.Visible=true;
    return tButtons;
}

在我的 Default.aspx.cs 页面中,我有

GetData Buttons = new GetData();//create a reference to the class
ButtonTable = Buttons.BuildTable();
OutPut.Text = ButtonTable.Rows.Count.ToString();

在 Default.aspx 中

<asp:Table runat="server" ID="ButtonTable" />   
<asp:Label runat="server" ID="OutPut" />

输出显示 4 行,但表为空。

<table id="ButtonTable" border="0"></table>

我做错了什么?

【问题讨论】:

  • 郑重声明,这是一个 ASP.NET 问题,我发现 C# 是一种美丽而优雅的语言。我发现 ASP.NET 很混乱,有点麻烦。
  • 在什么事件处理程序中调用 Default.aspx.cs 中的代码?
  • 是 public Table ButtonTable() 应该是 public Table BuildTable() 吗?
  • @pfunk- 是的,这绝对是他遇到的一个问题
  • 我在 Page_Load 中调用它我应该能够有一个 fn 调用任何东西对吗?因此指向表 BuildTable 的 ButtonTable 返回?

标签: c# .net asp.net html


【解决方案1】:

我真的建议你:

  • 在调试器中逐行运行,看看发生了什么
  • 重构它,代码很难阅读。添加更多的 cmets 并不能解决这个问题。
  • 考虑您想要实现的目标并检查它是否适合将数据绑定到像 ListView 这样的控件。

也就是说,你的代码:

GetData Buttons = new GetData();
ButtonTable = Buttons.BuildTable(); // this is what's wrong
OutPut.Text = ButtonTable.Rows.Count.ToString();

只是在页面中分配控件不是这样做的方法。将返回的表添加到控件集合中,或者更改 BuildTable 以接收它将加载信息的表。永远不要直接分配给 asp.net 页面的控件,一旦我不得不调试一些非常奇怪的问题的代码,并且开发人员将 null 分配给在 asp.net 期间搞砸的控件(而不是控件的属性)渲染周期。

【讨论】:

  • 感谢您的指示。我会做相应的修改。 :)
  • @Praesagus 如果您的意思是它解决了您的问题,您可以随时更改接受;)
【解决方案2】:

我到底错过了什么?

显然,很多。在您的标记中,您声明了一个System.Web.UI.WebControls.Table 的实例。在您的 Page 类实例中,这将有一个变量名“ButtonTable”。它还将自动添加到Page.Controls 集合中。当页面要渲染的时候,Controls集合会依次迭代渲染。

在您的 default.aspx.cs 代码中,您只是将 ButtonTable 引用指向不同的 Table 控件 - 但您不会影响 Page.Controls 集合。当渲染时间到来时,将被渲染的是标记中定义的(空白)表,而不是您的 BuildTable 调用的结果。

所有这一切都是一个相当冗长的“你做错了”。 为什么您希望将表格构建代码放在单独的类中的答案将为“正确的方式”提供一些启示。但是 - 我无意冒犯 - 我认为您需要先学习 ASP.NET 背后的基础知识,然后再继续学习。

话虽如此,最直接的解决方法(但可能不是您真正想要的)是将表格添加到 Controls 集合中以便它被渲染:

GetData Buttons = new GetData();//create a reference to the class
ButtonTable = Buttons.BuildTable();
this.Controls.Add(ButtonTable);
OutPut.Text = ButtonTable.Rows.Count.ToString();

请注意,它将与您的标记定义的ButtonTable 分开呈现,因此将放置在 输出标签之后。那是因为它是在Output标签之后添加的。

【讨论】:

  • 没有冒犯。你摇滚 - 谢谢。我想我指向的是一个创建的表变量,但不知道如何将它传递给 Render。为什么要分离类,以便代码可以灵活并模块化重用。 :)
  • RE:单独的类:我建议使用 UserControl 或 Server Control。你真的不想陷入“动态控件”的兔子洞(也就是那些不在标记中的控件),除非你必须这样做。
【解决方案3】:

ButtonTable = Buttons.BuildTable();

你在这里做什么?

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多