Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a function to get the value from user in a drop down and based on the number I should create few text boxes.

I have did created dynamic textboxes in a for loop by creating a panel in aspx page.

Now how can i get the values that entered in the text boxes?

I have tried the below code but that doesn't works..

// code to create dynamic text boxes

public void users(int cnt)
{

for (int i = 1; i <= cnt; i++)
{
TextBox txtuserAlias = new TextBox();
txtuserAlias.Text = "";
txtuserAlias.ID = "txtUserAlias" + i;
pnldisplayform.Controls.Add(txtuserAlias);
}
}

//code to call the value that entered in the text box
public void adduser()
{
int cnt1 = Int32.Parse(DropDownNoOfUsers.SelectedValue.ToString());
for (int i = 1; i <= cnt1; i++)
{
TextBox txtuserAlias = new TextBox();
txtuserAlias.ID = "txtUserAlias" + i;
some method = txtuserAlias.Text;
}
}

Could you please help with the same.
Thanks in advance.
Posted

Hi,

If you adding your TextBoxes to pnldisplayform, then you should be able to find them again using the pnldisplayform.FindControl[^] method.

Something like this should get you started.

C#
for (int i = 1; i <= cnt1; i++)
{
  //Find TextBox in Panel Container ... 
  TextBox txtuserAlias = (TextBox)pnldisplayform.FindControl("txtUserAlias" + i);
  //Check we have found a TextBox ... 
  if (txtuserAlias != null)
  {
    someMethod(txtuserAlias.Text);
  }
  //Clear control reference for next iteration ...
  txtuserAlias=null; 
}


... hope it helps.
 
Share this answer
 
Comments
Malvin687 10-Jun-15 10:33am    
Hi thanks for your quick response.
I tried that but the value seems passing as null and the code terminates after the if statement.
hypermellow 10-Jun-15 10:42am    
You need to find out if your controls (still) exists in your panel.
.. if you enable tracing, and run something like this, then you'll be closer to finding out where the problem exists.

//Write a trace message containing the ID of each TextBox found in pnldisplayform ...
foreach (Control ctl in pnldisplayform.Controls)
{
if (ctl is TextBox)
{
Trace.Write((TextBox)ctl).ID);
}
}
Mohamed El khames Bakir 10-Jun-15 10:40am    
if you are trying to read values after a postback so propably your textBoxes are already disposed as they are dynamically created ,so you have to stock value before the postback itself.
try this code may be it will help u :

View:

XML
<asp:DropDownList ID="DropDownNoOfUsers" runat="server" AutoPostBack="True">
       <asp:ListItem>1</asp:ListItem>
       <asp:ListItem>2</asp:ListItem>
       <asp:ListItem>3</asp:ListItem>
       <asp:ListItem>4</asp:ListItem>
       <asp:ListItem>5</asp:ListItem>
   </asp:DropDownList>
   <br />
   <asp:Panel ID="pnldisplayform" runat="server">
   </asp:Panel>
   <asp:Button runat="server" ID="Btn_Value" OnClick="Btn_Value_Click" Text="GetValue"/>

   <br />
   <asp:ListBox ID="ListUser" runat="server" Width="200"></asp:ListBox>



Code Behind :
C#
private List<string> _users;
 protected void Page_Load(object sender, EventArgs e)
 {
     var cnt = Int32.Parse(DropDownNoOfUsers.SelectedValue);
     Users(cnt);
 }
 public void Users(int cnt)
 {

     for (var i = 1; i <= cnt; i++)
     {
         var txtuserAlias = new TextBox { Text = "", ID = "txtUserAlias" + i };
         txtuserAlias.Attributes.Add("runat", "Server");
         txtuserAlias.EnableViewState = true;
         pnldisplayform.Controls.Add(txtuserAlias);
     }
 }
 public List<string> GetUsers()
 {
     return pnldisplayform.Controls.OfType<TextBox>().Select(box => box.Text).ToList();
 }


 protected void Btn_Value_Click(object sender, EventArgs e)
 {
     _users = GetUsers();
     foreach (var user in _users)
     {
         ListUser.Items.Add(user);
     }

 }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900