Question

I want to show multiple text boxes depending on the number a user selects from a dropdown box. Example below:

enter image description here

So whatever number is selected in the drop down the page refreshes and displays that number of text boxes. I need to go up to 20 fields. Is there a way to do this in C#, or maybe with the 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>
Était-ce utile?

La solution 2

try this

<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 />"));
                  }

    }  

Autres conseils

sure there is. use:

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);
}

Create an asp:panel and name it Panel1. Put the given code inside the SelectedIndexChanged event of your dropdownlist. set the autopostback property of your dropdownlist to true. it will work. hope that helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top