繁体   English   中英

为什么我的表格在 ASP.NET 中没有内容?

[英]Why does my table have no content in ASP.NET?

我在 class 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" />

Output 显示 4 行,但表格为空。

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

我究竟做错了什么?

我到底错过了什么?

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

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

所有这一切都是一个相当冗长的“你做错了”。 为什么您希望您的表格构建代码在单独的 class 中的答案将阐明“正确的方式”。 但是 - 我的意思是没有冒犯 - 我认为你需要在 go 之前研究 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 label 之后。 那是因为它是在 Output label 之后添加的。

我真的建议你:

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

也就是说,您的代码:

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

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

ButtonTable = Buttons.BuildTable();

你在里面做什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM