Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
one textbox and button1 and button2. enter the number in textbox and press the button1.Pressing button1 create Dynamically number of dropdownlist as entered in textbox. I have successful do it
But when i what use the seletedvalue of dropdownlist I cannot through anthor buttton.
C#
Dropdownlist  d;
Dropdownlist  dr;  this   decalre    global.

in   inside   button1    use   For  loop   
    for (int i = 0; i < a; i++)

            {
                d = new DropDownList();
                d.ID = "Text" + i;
  d.Items.Add("amit");
                d.Items.Add("asas");
   Panel3.Controls.Add(d);
}
}
thia is ok.

But inside button 2
C#
 string value = "";
 for (int i = 0; i < a; i++)
{
                
 dr = (DropDownList)Panel3.FindControl("Text" + i);
                        value = dr.SelectedValue.ToString();
                SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=IT;Integrated Security=True");
              
                con.Open();

                SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);

                cmd1.ExecuteNonQuery();
                con.Close();

            }

DropDownList d = (DropDownList)Panel3.FindControl("Text" + i);
                        value = d.SelectedValue.ToString();

The problem is probably that you're dropdownlist controls are not being rebuilt when the page loads. Before the second button's event gets fired the page will attempt to reload the page using the values saved in viewstate, because you are adding these controls dynamically they will not be available in viewstate. One option you might try is to maintain a count of the controls you add to the panel using a session variable or storing the count in a hidden control. Then during page_load add that number of dropdowns back into the panel

PLease tell how to implement session hidden control .
Posted
Updated 28-Oct-11 18:25pm
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