如何在代码后面动态添加 asp:TextBox? (不是文本区域)
How to add asp:TextBox dynamically on code behind ? (Not TextArea)
这是一个示例 asp:TextBox
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:TextBox runat="server" ID="MyBox" />
</ContentTemplate>
</asp:UpdatePanel>
在后面的代码中,我从数据库中获取了大量数据,因此我想创建相应的 asp:TextBox
文本框。
是否可以从后面的代码添加 UpdatePanel asp:TextBox
?
背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
int numberOfItems = AccountsBank.Bank_DAL.GetNumberOfActiveAccount();
// create 'numberOfItems' asp:TextBox
}
请注意,我不是在寻找 TextArea,我需要的是多个 asp:TextBox
。
非常感谢您的帮助
为了以编程方式向页面添加控件,新控件必须有一个容器。例如,如果您正在创建 table 行,则容器是 table。如果没有明显的控件充当容器,您可以使用 PlaceHolder 或 Panel Web 服务器控件。
<asp:PlaceHolder ID="container" runat="server" /
>
这个容器有一个名字,'container',你可以在后面的代码中调用它。
foreach(DataRow dataRow in dataTable.Rows)
{
TextBox tb = new TextBox();
tb.Name = "tb_" + dataRow.Id;
tb.Text = dataRow.Content;
container.Controls.Add(tb);
}
以编程方式创建控件的问题在于您需要确保在每次回发时都创建它们。话虽如此,更简单、更可靠的方法是使用中继器。然后您可以根据帐户数量重复文本框的数量。像这样:
标记:
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="myRep" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" ID="MyBox" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
绑定代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int numberOfItems = AccountsBank.Bank_DAL.GetNumberOfActiveAccount();
myRep.DataSource = Enumerable.Range(0, numberOfItems).ToList();
myRep.DataBind();
}
}
参考:
这是一个示例 asp:TextBox
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:TextBox runat="server" ID="MyBox" />
</ContentTemplate>
</asp:UpdatePanel>
在后面的代码中,我从数据库中获取了大量数据,因此我想创建相应的 asp:TextBox
文本框。
是否可以从后面的代码添加 UpdatePanel asp:TextBox
?
背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
int numberOfItems = AccountsBank.Bank_DAL.GetNumberOfActiveAccount();
// create 'numberOfItems' asp:TextBox
}
请注意,我不是在寻找 TextArea,我需要的是多个 asp:TextBox
。
非常感谢您的帮助
为了以编程方式向页面添加控件,新控件必须有一个容器。例如,如果您正在创建 table 行,则容器是 table。如果没有明显的控件充当容器,您可以使用 PlaceHolder 或 Panel Web 服务器控件。
<asp:PlaceHolder ID="container" runat="server" /
>
这个容器有一个名字,'container',你可以在后面的代码中调用它。
foreach(DataRow dataRow in dataTable.Rows)
{
TextBox tb = new TextBox();
tb.Name = "tb_" + dataRow.Id;
tb.Text = dataRow.Content;
container.Controls.Add(tb);
}
以编程方式创建控件的问题在于您需要确保在每次回发时都创建它们。话虽如此,更简单、更可靠的方法是使用中继器。然后您可以根据帐户数量重复文本框的数量。像这样:
标记:
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="myRep" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" ID="MyBox" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
绑定代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int numberOfItems = AccountsBank.Bank_DAL.GetNumberOfActiveAccount();
myRep.DataSource = Enumerable.Range(0, numberOfItems).ToList();
myRep.DataBind();
}
}
参考: