Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Asp.net C#
I am creating Three Dropdownlist Dynamic Controls on page_Preinit
On generating the controls on page_Preinit I bind the datasource for the fist time

on First dropdownlist SelectedIndexChanged Event I Change the Datasource of the Second DropDowns
And on Second dropdownlist SelectedIndexChanged Event I change the datasource of the thrid downpdown

But Problem is Second dropdownlist loses the SelectedValue

public void GenerateTheDynamicControls()
        {
            if (dtMainData != null)
            {
                Table tblData = new Table();
                tblData.Caption = "Drop Down";
                tblData.Width = Unit.Percentage(100);
                tblData.CellSpacing = 2;
                tblData.CellPadding = 4;
                tblData.BorderStyle = BorderStyle.Solid;
                foreach (DataRow dr in dtMainData.Rows)
                {
                    TableRow tr = new TableRow();
                    tr.BorderStyle = BorderStyle.Solid;
                    if (dr["ControlType"].ToString() == "DropDownList")
                    {
                        DropDownList ddl = new DropDownList();
                        TableCell cH = new TableCell();
                        System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
                        lbl.Text = "" + dr["ControlLabelName"].ToString() + " : ";
                        cH.Controls.Add(lbl);
                        tr.Cells.Add(cH);
                        TableCell cH2 = new TableCell();
                        
                       
                        ddl.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                        ddl.ID = "" + dr["ControlName"].ToString();
                        List<string> lstDdlDataSource = new List<string>();
                        foreach (var ns in dr["ControlDataSourceString"].ToString().Split(';'))
                        {
                            if (ns.Trim() != "")
                            {
                                lstDdlDataSource.Add(ns);
                            }
                        }
                        ddl.DataSource = lstDdlDataSource;
                        ddl.DataBind();
                        ddl.Items.Insert(0, "Select");
                        if (dr["ConnectedToControl"].ToString().ToLower() == "true")
                        {
                            ddl.AutoPostBack = true;
                            ddl.SelectedIndexChanged += new EventHandler(dropDowns_SelectedIndexChanged);                            
                        }
                        ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(ddl);
                        cH2.Controls.Add(ddl);
                        tr.Cells.Add(cH2);
                    }
                    tblData.Rows.Add(tr);
                }
                phCreateControls.Controls.Add(tblData);
                UpdatePanel1.Update();
            }
        }


void dropDowns_SelectedIndexChanged(object sender, EventArgs e)
        {                        
            //bc.showJavascriptMessagesForScriptManagerPages("It Works", this.Page);
            if (dtMainData != null)
            {
                DataRow[] drSelection = dtMainData.Select("ControlName='" + (sender as DropDownList).ID.ToString()+ "'"); ;
                if (drSelection != null && drSelection.Length >= 1)
                {
                    DropDownList ConnectedToControlName = phCreateControls.FindControl("" + drSelection[0]["ConnectedToControlName"]) as DropDownList;
                    string[] ConnectedToControlDataSourceValues = drSelection[0]["ConnectedToControlDataSourceValues"].ToString().Split(',');
                    List<string> lstDdlDataSource = new List<string>();
                    foreach (string str in ConnectedToControlDataSourceValues)
                    {
                        if (str.Contains((sender as DropDownList).SelectedValue.ToString()))
                        {
                            string [] valuesForData = str.Replace("[","").Replace("]","").Split('»');
                            foreach (var ns in valuesForData[1].Split(';'))
                            {
                                if (ns.Trim() != "")
                                {
                                    lstDdlDataSource.Add(ns);
                                }
                            }
                        }
                    }
                    ConnectedToControlName.DataSource = lstDdlDataSource;
                    ConnectedToControlName.DataBind();
                }
            }
            UpdatePanel1.Update();
        }
Posted
Comments
Sinisa Hajnal 20-Aug-15 2:28am    
Did you check that the code binding the event executes?
Why don't you have fixed controls on the page and just hide them if you don't need them?
[no name] 20-Aug-15 12:30pm    
Actually Code works fine for If Two Dropdown are generated and one dropdown's is mapped with second drop down
Assume 1,2,3 as dropdownlist
The Real problem arise when When I try creating three downs and 1 is mapped 2 and 2 is mapped to 3
As When SelectedIndexChanged on 2 the is fired The Event is fired for 1 and 2 both so the datasource changes and it cannot save the state

So how to resolve this issue?
The
Sinisa Hajnal 21-Aug-15 2:09am    
1. Have separate handlers - put the common code into a method
2. Don't change the datasource for all combos - have datatable.Copy in each so that each contains separate copy of the data.

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