Click here to Skip to main content
15,896,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i am working on creating specific number of drop down list depending on number inputs by user. i had textbox(where user input number like 2) and button that create drop down list (in this case 2 dropdown lists). as below
C#
for (int i = 0; i <= Convert.ToInt16(DropDownList2.SelectedValue); i++)
{

    DropDownList dp = new System.Web.UI.WebControls.DropDownList();
    dp.ID = "ClRepList" + i;
    dp.AutoPostBack = true;
    dp.Height = 24;
    dp.Width = 102;
    dp.DataTextField = "PName";
    dp.DataValueField = "PName";
    dp.DataSource = return_data_table();
    dp.DataBind();
    ClientRepInfo.Controls.Add(dp);
}

now i want to read values from these lists to save values in database . as below
C#
protected void Button1_Click(object sender, EventArgs e)
{
    string[] Path=new string[5];
    ContentPlaceHolder MainCont =Page.Master.FindControl("MainContent") as ContentPlaceHolder;
    for (int k = 0; k <= 4; k++)
    {
        Panel Dpanel = (Panel)MainCont.FindControl("ClientRepInfo") as Panel;
        DropDownList drp = (DropDownList)Dpanel.FindControl("ClRepList" + k);
            if (drp != null)
            //  if (MainCont.FindControl("ClRepList" + k) != null)
            {
                Path[k] = drp.SelectedValue;
            }        
    }

but this now work. when i put break point on DropDown drp it reads null value !!

any help please ...
Posted
Updated 9-Jun-15 0:43am
v2

C#
foreach ( Control ctrl in panel) {

if(ctrl is DropDownList)
{
 Path[k] = ctrl .SelectedValue;
//let me know if it is giving null value
}
    //    
    }
 
Share this answer
 
v3
Comments
Badour alsamaraie 9-Jun-15 7:41am    
i did this method but still drp value = Null
sasanka sekhar panda 9-Jun-15 8:09am    
use ctrl instead of drp and see the value in ctrl.SelectedValue
Badour alsamaraie 10-Jun-15 3:50am    
i did what you told me but still the same error, ctrl do not dropdown list that created dynamically each time page is reload
You need to create the dropdowns on every postback, they won't be recreated automatically.
 
Share this answer
 
Comments
Badour alsamaraie 9-Jun-15 7:14am    
sorry, what you mean?
F-ES Sitecore 9-Jun-15 8:04am    
In the Page_Load event you have to create your DropDownList controls again and give them the same ID values.

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