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

标签 c# .net asp.net html

我在 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>

我做错了什么?

最佳答案

What the heck am I missing?

显然,很多。在您的标记中,您声明了一个 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 标签之后添加的。

关于c# - 为什么我的表在 ASP.NET 中没有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/659377/

相关文章:

c# - 这两种声明 self 绑定(bind)的方式有什么区别?

.net - F# 和结果类型将是无限错误

javascript - onclick 事件未在 firefox 4.1 中的 anchor 标记中触发

c# - 如何获取 NHibernate 中的连接标准计数

c# - Kung foo调用多次,如何从测试中知道调用了什么方法?

c# - ILGenerator : Load created method

javascript - 实时更改 Repeater 内的标签

.net - ASP.NET View 引擎不会执行我的代码片段

c# - 如何从此方法返回 Dictionary<string,Dictionary<int,decimal>> ?

c# - 尝试在Naudio(C#)中播放ISampleProvider时获取NullReferenceException