【发布时间】:2012-02-16 03:18:41
【问题描述】:
实际上我正在使用 asp.net 和 c# 创建 Web 模板。
在我的user control 页面中,我必须动态创建表格。我只是从 XML 文件中读取数据,然后检索每个表的名称和列数和行数。在创建表格时,我将名称和 ID 分配给每个单元格。因为我有一行,包括一些 textbox 用于将数据添加到数据库。但是在我创建表之后,我无法访问基于 id 的textbox 单元格以获取它们的数据并将其插入数据库。
我的动态表代码是:
public void CreateAddDynamicTable()
{
XmlDocument xDocRead = new XmlDocument();
xDocRead.Load(Server.MapPath("ModuleTemp.xml"));
string xModuleName = hid_ChooseModule.Value;
XmlNode xColCounter = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/List");
int colCount = xColCounter.ChildNodes.Count;
int nonPkCounter = 0;
string[] nonPrimaryKey = new string[100];
string[] nonPkNewDataTemp = new string[100];
for (int i = 1; i <= colCount; i++)
{
if (xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Add/TableColumn" + i).Attributes.GetNamedItem("IsPrimaryKey").Value == "N")
{
nonPrimaryKey[nonPkCounter] = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Add/TableColumn" + i).Attributes.GetNamedItem("Name").Value;
nonPkCounter++;
}
}
ph_Uc_AddModule.Controls.Clear();
// Fetch the number of Rows and Columns for the table
// using the properties
int tblRows = nonPkCounter;
int tblCols = 2;
// Create a Table and set its properties
Table tbl = new Table();
// Add the table to the placeholder control
ph_Uc_AddModule.Controls.Add(tbl);
// Now iterate through the table and add your controls
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
Label td_Add_Header = new Label();
TextBox td_Add = new TextBox();
if (j == 0)
{
td_Add_Header.Text = nonPrimaryKey[i];
td_Add_Header.ID = "lb" + (i + 1) + "_header_AddModule";
// Add the control to the TableCell
tc.Controls.Add(td_Add_Header);
tc.CssClass = "td_Header_AddModule";
}
else
{
td_Add.Text = "";
td_Add.ID = "tb" + (i + 1) + "_AddModule";
// Add the control to the TableCell
tc.Controls.Add(td_Add);
tc.CssClass = "td_Tb_AddModule";
}
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}
}
包含PlaceHolder 的用户控制页面如下:
<asp:Panel ID="pn_Uc_TModule" runat="server">
<asp:Table runat="server" ID="table_Uc_TModule" CssClass="table_Uc_TModule" Width="100%">
<asp:TableRow runat="server" VerticalAlign="Top" Height="50px">
<asp:TableCell runat="server">
<asp:Button runat="server" Text="" CssClass="btn_Add_Active" OnClick="btn_AddNew_Click" />
<asp:Button ID="btn_Cancel_NewItem" runat="server" Text="" CssClass="btn_Cancel_New" OnClick="btn_Cancel_AddNew" Visible="false" />
<asp:Table runat="server" ID="table_Uc_AddModule" Visible="false">
<asp:TableRow runat="server">
<asp:TableCell runat="server" >
<asp:PlaceHolder ID="ph_Uc_AddModule" runat="server">
</asp:PlaceHolder>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell runat="server" CssClass="td_Tb_AddModule" HorizontalAlign="Center">
<asp:Button ID="btn_Insert" runat="server" OnClick="Uc_AddModule_ItemInsert" Text="" CssClass="btn_Add" />
<asp:Button ID="btn_Cancel" runat="server" OnClick="Uc_AddModule_Clear" Text="" CssClass="btn_Clear" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
我在后面代码的插入函数中使用下面的方法来访问插入到textbox的数据:
for (int i = 1; i <= nonPkCounter; i++)
{
TextBox textBox = (ph_Uc_AddModule.FindControl("tb" + i + "_AddModule")) as TextBox;
}
我尝试过pn_Uc_TModule.FindControl("tb" + i + "_AddModule") 和This.ph_Uc_AddModule.FindControl("tb" + i + "_AddModule"),但仍然无法将texbox 数据插入数据库。
你能指导我如何完成它。
感谢您的考虑。
【问题讨论】:
-
在什么情况下调用 CreateAddDynamicTable 方法?在里面?加载?
标签: c# asp.net asp.net-ajax updatepanel user-controls