Retrive value of dynamic control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satiss7pwr
    New Member
    • Jan 2010
    • 41

    Retrive value of dynamic control

    i am creating dynamic text box and dropdownlist on one DropDownList7_S electedIndexCha nged event like this code
    Code:
    public partial class AddMethod : System.Web.UI.Page
    {
        
        DropDownList drp=null ;
        TextBox txt=null  ;
        int z;// it store store DropDownList7.SelectedValue
    .
    .
    .
    
    protected void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)//this dropdown having static mebers like 1,2,3,...so on and as its value i want to create no of textbox and dropdowns
        {
           
            z = int.Parse(DropDownList7.SelectedValue);
            while(z > 0)
                { 
                    drp = new DropDownList();
                    txt = new TextBox();
                    drp.ID=Convert.ToString(z); 
                    txt.ID="txt"+Convert.ToString(z);
                   
                    drp.Items.Add(new ListItem("int"));
                    drp.Items.Add(new ListItem("float"));
                    drp.Items.Add(new ListItem("string"));
                    drp.Items.Add(new ListItem("boolean"));
                    
                    
                    Panel1.Controls.Add(drp);
                   
                    Panel1.Controls.Add(txt);
                   
                  
                    z--;
                }
     }
    And how i can get values of this dynamic controls.
    i have try this code but it not worked,i try to get this values on button clik event like this
    Code:
    protected void Button6_Click(object sender, EventArgs e)
        {
           
            for (int t = 1; t <= z; t++)
            {
                string tn = "txt" + t;
               // TextBox txt = (TextBox)Panel1.FindControl("txt" +t);
                //TextBox txt = Panel1.FindControl(tn) as TextBox;
                TextBox txt = PlaceHolder1.FindControl(tn) as TextBox;
                 string tname=txt.Text;
                
            }
    }

    plese any one help me
    i f im going wrong to writing code than pls suggest me
    im reaching final step to my project plsssss
    Last edited by Frinavale; Apr 21 '10, 03:33 PM. Reason: Please post code in [CODE] ... [/CODE] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You are doing something wrong.
    You cannot create dynamic ASP.NET controls in a method that handles an Event. You have to create them before this point (in the Page Init event) or else you will not be able to retrieve the values in subsequent postbacks to the server.

    Please review this article on how to use dynamic controls in ASP.NET.

    -Frinny

    PS.

    Please post code in code tags. It makes code easier for us to read and it provides us with line numbers that we can refer to when helping you.

    Comment

    Working...