簡體   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