Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with getting value of dynamically created textbox

coolv's Avatar
Newbie
 
Join Date: Jan 2008
Posts: 6
#1: Jan 11 '08
Hello everyone,

I m generating dynamic textbox depending on the value selected from the dropdownlist(DropDownList1_SelectedIndexChanged).e g: if i select 3 from dropdownlist,then 3 textbox is generated.

But the problem is that when i click on insert button(btnadd_Click) ,then i don't get the value those are inserted into generated textboxes.

Below is my code:


Expand|Select|Wrap|Line Numbers
  1.     protected void  btnadd_Click(object sender, EventArgs e)
  2.     {
  3.         for(int i=0; i<Convert.ToInt32(DropDownList1.SelectedValue); i++)
  4.         {
  5.  
  6.          TextBox textBox =(TextBox)Panel1.FindControl("myTextBox" + i.ToString());
  7.         if (textBox != null)
  8.         {
  9.  
  10.           touchpay.exce("Insert into card_pin_master(company_id,card_type,pin,card_id) values('" + dpwcompany.SelectedValue + "','" + dpwtype.SelectedValue + "','" + textBox.Text+ "',"+dpwcardname.SelectedValue+"')");
  11.         }
  12.         }
  13.     }
  14.     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  15.     {
  16.         for (int x = 0; x < Convert.ToInt32(DropDownList1.SelectedValue); x++)
  17.         {
  18.             TextBox textarray = new TextBox();
  19.             textarray.ID = "myTextBox" + x.ToString();
  20.             //textarray.Visible = false;
  21.            Panel1.Controls.Add(textarray);
  22.         }
  23.     }

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: Jan 11 '08

re: problem with getting value of dynamically created textbox


I've started an article on using dynamic controls in .NET. It's not complete but it covers how to dynamically load textboxes and retrieve their values. It's not complete and it's in VB.NET, but the example works and is easily translated into C#.

My guess is that you are not properly initializing your text boxes in your Page_Init method (you haven't posted this method).

Check out the rough article and then get back to me if you're still having problems.

-Frinny
Reply