Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i am creating dynamic drop down list
but unable to fire selection changed event
here below is the function for creating dynamic controls

 private void GenerateTable(int rowsCount)
        {
            foreach (Control c in pnlDynamic.Controls)
            {
                if (c is Table)
                {
                    Table tg = c as Table;
                    if (tg.ID == "Table1")
                    {
                        pnlDynamic.Controls.Remove(tg);
                        grvSearchResult.DataSource = null;
                        grvSearchResult.DataBind();
                    }
                }
            }
            //Creat the Table and Add it to the Page
            Table table = new Table();
            table.ID = "Table1";
            //The number of Columns to be generated
            const int colsCount = 1;//You can changed the value of 3 based on you requirements
            // Now iterate through the table and add your controls
            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {
                    TableCell cell = new TableCell();
                    TableCell cell1 = new TableCell();
                    TableCell cell2 = new TableCell();
                    TableCell cell3 = new TableCell();
                    TableCell cell4 = new TableCell();
                    TableCell cell5 = new TableCell();
                    //properties label
                    Label lb = new Label();
                    lb.ID = "lbl" + i + "Col_" + j;
                    lb.Text = "Select Property";
                    cell.Controls.Add(lb);
                    row.Cells.Add(cell);
                    //properties drop down list
                    DropDownList ddl = new DropDownList();
                    ddl.ID = "ddlProperty" + i;
                    BindPropertiesDropdownList(ddl);
                    ddl.AutoPostBack = true;
                    
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
cell1.Controls.Add(ddl);
                    row.Cells.Add(cell1);
                    //Criteria label
                    Label lb1 = new Label();
                    lb1.ID = "lbl11" + i + "Col_" + j;
                    lb1.Text = "Select Criteria";
                    cell2.Controls.Add(lb1);
                    row.Cells.Add(cell2);
                    //Criteria drop down list
                    DropDownList ddl1 = new DropDownList();
                    ddl1.ID = "ddlCriteria" + i;
                    BindCriteriaDropdownList(ddl1);
                    cell3.Controls.Add(ddl1);
                    row.Cells.Add(cell3);
                    //Value Label
                    Label lb2 = new Label();
                    lb2.ID = "lbl1" + i + "Col_" + j;
                    lb2.Text = "Enter Value";
                    cell4.Controls.Add(lb2);
                    row.Cells.Add(cell4);
                    //Value textBox
                    TextBox txt = new TextBox();
                    txt.ID = "txt" + i;
                    cell5.Controls.Add(txt);
                    row.Cells.Add(cell5);
                }
                // And finally, add the TableRow to the Table
                table.Rows.Add(row);
            }
            //this.pnlDynamic.Form.Controls.Add(table);
            pnlDynamic.Controls.Add(table);
            //his.Page.Form.
            //Set Previous Data on PostBacks
            //  SetPreviousData(rowsCount, colsCount);
            //Sore the current Rows Count in ViewState
            rowsCount++;
            table.EnableViewState = true;
            ViewState["table"] = true;
        }


when i run this i got an error

Delegate to an instance method cannot have null 'this'.
Posted

1 solution

Delegate to an instance method cannot have null 'this'.
Have you defined your event handler? ddl_SelectedIndexChanged

Above code looks fine and should work if you have defined the delegated event. Add,
C#
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
         // Do your stuff
}
 
Share this answer
 
Comments
sharmarun 11-May-12 1:26am    
sandeep thanks for your reply
i did same as yours
fine working
thanks
Maciej Los 11-May-12 1:29am    
If the answer was helpful, then rate it and mark as "solved". Thank you.
Maciej Los 11-May-12 1:28am    
Good answer, my 5!
Sandeep Mewara 11-May-12 5:09am    
Thanks losmac.
sharmarun 11-May-12 1:30am    
done
:)

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