Pra*_*gus 1 .net html c# asp.net
我在类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;
}
Run Code Online (Sandbox Code Playgroud)
在我的Default.aspx.cs页面中
GetData Buttons = new GetData();//create a reference to the class
ButtonTable = Buttons.BuildTable();
OutPut.Text = ButtonTable.Rows.Count.ToString();
Run Code Online (Sandbox Code Playgroud)
在Default.aspx中
<asp:Table runat="server" ID="ButtonTable" />
<asp:Label runat="server" ID="OutPut" />
Run Code Online (Sandbox Code Playgroud)
输出显示4行,但表为空.
<table id="ButtonTable" border="0"></table>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我错过了什么?
显然,很多.在标记中,您已声明了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();
Run Code Online (Sandbox Code Playgroud)
请注意,它会单独渲染从您的标记定义ButtonTable,因此将被放置后输出的标签.那是因为它是在Output标签之后添加的.
| 归档时间: |
|
| 查看次数: |
279 次 |
| 最近记录: |