none
Unable to get values of dynamically created controls c# RRS feed

  • Question

  • I have created dynamic controls on button click and I'm not able to retrieve values of dynamically created controls. I am getting values of dynamic controls inside a panel.

    pnlDepartment is the Panel ID.

    protected void btnValues_Click(object sender, EventArgs e)
        {
                string strDDLValue = string.Empty;
                foreach (DropDownList ddl in pnlDepartment.Controls.OfType<DropDownList>())
                {
                    strDDLValue = ddlName.SelectedItem.Text + "," + ddlLocation.SelectedItem.Text;
                }
        }

    The strDDLValue has only first dropdown values and when it loops for the second time it still takes the first dropdown values and unable to get dynamic control values.

    Please correct me if I'm making a mistake somewhere.

    • Moved by CoolDadTx Monday, January 8, 2018 3:51 PM ASP.NET related
    Monday, January 8, 2018 7:33 AM

All replies


  •          

                    strDDLValue = ddlName.SelectedItem.Text + "," + ddlLocation.SelectedItem.Text; 

                   // this is the mistake

    The strDDLValue has only first dropdown values and when it loops for the second time it still takes the first dropdown values and unable to get dynamic control values.

    Please correct me if I'm making a mistake somewhere.

    bfvbvbBecause you are using wrong controls in the loop instead of the new ddl item coming in the loop, i think you need to use the new variable :

    protected void btnValues_Click(object sender, EventArgs e)
    {
        string strDDLValue = string.Empty;
        strDDlValue =ddlName.SelectedItem.Text + "," + ddlLocation.SelectedItem.Text;
        foreach (DropDownList ddl in pnlDepartment.Controls.OfType<DropDownList>())
        {
            strDDLValue = strDDLValue + "," + ddl.SelectedItem.Text;
        }
    }

    Hope it gives you idea that what you need to do.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 8, 2018 10:59 AM
  • Still the dynamic dropdown values are not coming.
    Monday, January 8, 2018 11:47 AM
  • DropDownList is a webforms control. Please post questions related to ASP.NET and web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 8, 2018 3:51 PM