简体   繁体   中英

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

I have built a table in a 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;
}

In my Default.aspx.cs page I have

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

In Default.aspx

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

Output shows 4 rows, but table is empty.

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

What am I doing wrong?

What the heck am I missing?

Apparently, a lot. In your markup, you have declared an instance of a System.Web.UI.WebControls.Table . In your instance of the Page class, this will have a variable name of "ButtonTable". It will also be automatically added to the Page.Controls collection. When the page is going to be rendered, the Controls collection will be iterated and rendered in turn.

In your default.aspx.cs code, you're simply pointing your ButtonTable reference to a different Table control - but you're not affecting the Page.Controls collection. When render time comes, it is the (blank) Table defined in the markup that will be rendered - not the result of your BuildTable call.

All of this is a fairly long winded "you're doing it wrong". The answer to why you want your table building code in a separate class would shed some light on the "right way". But - and I mean no offense - I think you need to study the basics behind ASP.NET before you go any further.

That being said, the most immediate fix (but likely not what you really want ) is to add the table to the Controls collection so that it gets rendered:

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

Note that it will render separately from your markup defined ButtonTable, and so will be placed after the Output label. That is because it was added after the Output label.

I really suggest you:

  • Run it line by line in the debugger to see what's going on
  • Refactor it, the code is hard to read. Adding more comments won't solve that.
  • Consider what you want to achieve and check if it can fits to databind to a control like ListView.

That said, your code:

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

Just assigning the control in the page it's not the way to do it. Either add the returned table to the controls collection, or change BuildTable to receive the table it will load the info into. Never directly assign to the control of the asp.net page, once I had to debug code with some very strange issues and a developer had assigned null to a control (not to a property of the control) which messed up during the asp.net render cycle.

ButtonTable = Buttons.BuildTable();

What do you do in there here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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