如何访问用户控制页面中动态创建的表的文本框?
实际上我正在使用 asp.net 和 c# 创建网页模板。
在我的用户控制
页面中,我必须动态创建表。我只是从 XML 文件中读取数据,然后检索每个表的名称以及列数和行数。当我创建表时,我为每个单元格分配名称和 ID。因为我有一行包含一些用于将数据添加到数据库的文本框。但在我创建表后,我无法根据 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
数据将其插入数据库。 你能指导我如何完成它吗? 感谢您的考虑。
Actually i'm creating web template using asp.net and c#.
in my user control
page i have to create the table dynamically. i just read the data from XML file then retrieve the name and number of columns and rows of each table. while i'm creating the table i assign the name and id to each cell. because i have one row including some textbox
for adding the data to database. but after i have created the table i can not access to the textbox
cells based on id to get their data and insert it to database.
my dynamic table code is:
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);
}
}
my user control page which is contain a PlaceHolder
is as below:
<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>
i'm using below method in my insert function at the code behind to access the data inserted at the textbox
:
for (int i = 1; i <= nonPkCounter; i++)
{
TextBox textBox = (ph_Uc_AddModule.FindControl("tb" + i + "_AddModule")) as TextBox;
}
i have tried pn_Uc_TModule.FindControl("tb" + i + "_AddModule")
and also This.ph_Uc_AddModule.FindControl("tb" + i + "_AddModule")
but still con not get the texbox
data to insert it to database.
could you please guide me how to get it done.
appreciate your consideration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该始终在 Init 事件中调用 CreateAddDynamicTable。这将使视图状态有意义,并且 ASP.NET 会将所有值加载到控件中。然后,您可以将文本框添加到集合中并访问它们,而无需在 Page_Load 事件中使用 FindControl,仅使用引用。
You should call CreateAddDynamicTable on your Init event always. That will make the viewstate make sense and asp.net will load all the values to the controls. Then you can add the textboxes to a collection and access them without using FindControl, just the references, in your Page_Load event.