locked
Add Dropdownlist Dynamically according to the items checked in checkboxlist RRS feed

  • Question

  • User1552454131 posted

    Hello friends,

                      I have a requirement, i have checkboxlist  from which i will check some items, depending on the number of items checked in the checkboxlist i have to create same number of dropdownlist dynamically with value binding from datatable, i have created one, but the problem which i am facing on postback if i dont regenerate the control on pageload than control is lost, if regenerate the control than the selected value is lost, i am posting my code below, any immediate help on this will be appericiated. 

                                <asp:PlaceHolder runat="server" ID="pldMultiYearColumns" ></asp:PlaceHolder>
                                
                                 
      
       private void MultiYearColumnControl()
        {
            if (chkMultiYear != null)
            {
    
                //Add New Row
                TableRow trMain = new TableRow();
                int cnt = 0;
                foreach (ListItem itemMultiYearSelected in chkMultiYear.Items)
                {
                    if (itemMultiYearSelected.Selected)
                    {
                        ITReturnBLL itReturnBLL = new ITReturnBLL();
                        DataTable _dtReturnTable = new DataTable();
                        //Get the Selected ComputationId
                        int _computationId = Convert.ToInt32(itemMultiYearSelected.Value);
                        
                        _dtReturnTable = itReturnBLL.GetTableGeneratedForReturn(_computationId);
                        //Create a Datatable to Hold the ColumnName From the Return DataTable
                        DataTable dtCol = GetTableWithColumnName(_dtReturnTable, itemMultiYearSelected.Text);
    
    
                        //Create an Instance For the RadComboBoxList
                        DropDownList drpMutiYearCol = new DropDownList();
                       
                            drpMutiYearCol.ID = "ddlDynamic-" + Convert.ToString(cnt);
                      
                        
                        drpMutiYearCol.Width = new Unit("250px");
                        drpMutiYearCol.AutoPostBack = true;
                        
                        
                        drpMutiYearCol.DataSource = dtCol;
                        //drpMutiYearCol.DataTextField = "ColumnName";
                        //drpMutiYearCol.DataValueField = "FinancialYear";
                        //drpMutiYearCol.DataBind();
                        drpMutiYearCol.Items.Insert(0, new ListItem("--Select--", ""));
                        foreach (DataRow row in dtCol.Rows)
                        {
                            int index = 1;
                            drpMutiYearCol.Items.Insert(index, new ListItem(Convert.ToString(row["ColumnName"]), Convert.ToString(row["FinancialYear"])));
                            index = index + 1;
                        }
                        
    
    
    
                        //drpMutiYearCol.SelectedIndexChanged += drpMutiYearCol_SelectedIndexChanged;
                        drpMutiYearCol.SelectedIndexChanged += drpMutiYearCol_SelectedIndexChanged;
                        //drpMutiYearCol.TextChanged += drpMutiYearCol_TextChanged;
                        pldMultiYearColumns.Controls.Add(drpMutiYearCol);
                        cnt = cnt + 1;
                        _FlagIsGenerated = "T";
                    }
                }
    
    
            }
        }
    
    
      protected void Page_Load(object sender, EventArgs e)
        {
    
            if (_FlagIsGenerated == "T")
            {
                MultiYearColumnControl();
            }
           
            
        }
    protected void btnGetColumnList_Click(object sender, EventArgs e)
        {
            
            MultiYearColumnControl();
            
        }
        protected void drpMutiYearCol_SelectedIndexChanged(object sender, EventArgs e)
        {
            MultiYearColumnControl();
            //Get the Dropdownlist which raises the event
            DropDownList drp = (DropDownList)sender;
            //Add the selected value and text to the dataTabel
            if (_dtMultiYearReport.Columns.Count == 0)
            {
                DataColumn colFY = new DataColumn("FinancialYear", typeof(string));
                DataColumn colColname = new DataColumn("ColumnName", typeof(string));
                DataColumn colComputationID = new DataColumn("ComputationID", typeof(int));
                DataColumn colParticulars = new DataColumn("Particulars", typeof(string));
                DataColumn colAmount = new DataColumn("Amount", typeof(Double));
                _dtMultiYearReport.Columns.Add(colFY);
                _dtMultiYearReport.Columns.Add(colColname);
                _dtMultiYearReport.Columns.Add(colComputationID);
                _dtMultiYearReport.Columns.Add(colParticulars);
                _dtMultiYearReport.Columns.Add(colAmount);
            }
            //Add the rows to the Table
            DataRow row = _dtMultiYearReport.NewRow();
            row["FinancialYear"] = Convert.ToString(drp.SelectedValue);
            row["ColumnName"] = Convert.ToString(drp.SelectedItem);
            ListItem listItem = this.chkMultiYear.Items.FindByText(drp.SelectedValue);
            row["ComputationID"] = Convert.ToString(listItem.Value);
    
            //Add column to the DataTable
            _dtMultiYearReport.Rows.Add(row);
    
            _FlagIsGenerated = "T";
        }

    Thanks and Regards

    Harnek Singh

    Wednesday, October 19, 2016 11:29 AM

All replies

  • User-691209617 posted

    Hi Harnek11,

    There are couple of ways you can achieve your task.

    1- you can use update panel in this case.

    2- you can store selected values in session or hidden variables and when form post back you can get values and set it again.

    3- Using java script approach and I am posting working example for you.

    http://dotnetspeaks.com/DisplayArticle.aspx?ID=63

    Hope it helps.

    Wednesday, October 19, 2016 2:06 PM