我想显示多个文本框,具体取决于用户从下拉框中选择的数字。示例如下:
因此,在下拉列表中选择了任何数字,页面将刷新并显示该文本框的数量。我需要去20个领域。有没有办法在C#中执行此操作,或者使用Ajax Control Toolkit?
ASPX
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
答案 0 :(得分:1)
int ctrlCount=Convert.ToInt32(DropDownList1.SelectedItem.Value);
int ctrlTopPos=30;
lbl_name.Text="Name:";
for(int i=0;i<ctrlCount;i++)
{
Label lbl_name=new Label();
TextBox txt_cur=new TextBox();
txt_cur.Top=ctrlTopPos+(i*30);
lbl_name.top=ctrlTopPos+(i*30);
txt_cur.left=lbl_name.Width+30;
Panel1.Controls.Add(lbl_name);
Panel1.Controls.Add(txt_cur);
}
创建一个asp:面板并将其命名为Panel1。 将给定代码放在下拉列表的SelectedIndexChanged事件中。 将下拉列表的autopostback属性设置为true。 它会工作。 希望有所帮助。
答案 1 :(得分:0)
试试这个
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<div>
<asp:PlaceHolder id="ContentPlaceHolder1" runat="server" />
</div>
protected void NumAccountsList_SelectedIndexChanged(object sender, EventArgs e)
{
ContentPlaceHolder1.Controls.Clear();
for(i=0; i<Convert.ToInt32(EmpNameList.SelectedItem.Value); i++)
{
TextBox tx= new TextBox();
tx.ID="tx"+i;
ContentPlaceHolder1.Controls.Add(tx);
ContentPlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}