Click here to Skip to main content
14,920,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        TableRow tb = new TableRow();
        TableCell tc=new TableCell();
        DropDownList db = new DropDownList();
        db.Items.Add("Bangalore");
        db.Items.Add("Mandya");
        db.Items.Add( "Hassan");
        tc.Controls.Add(db);
        tb.Controls.Add(tc);
        Table1.Controls.Add(tb);
        db.SelectedIndexChanged += db_SelectedIndexChanged;
        db.AutoPostBack = true;

    }
    private void db_SelectedIndexChanged(object sender,EventArgs e)
    {
       label.text="welcome";
    }

But when I place the same code block of button_click in page load event db_SelectedIndexChanged execute.
But when I use code in Button click db_SelectedIndexChanged doesn't execute.

What may be the reason behind this?
Posted
Updated 28-Nov-11 4:56am
v4

1 solution

Try to call the db_SelectedIndexChanged on button click event
C#
protected void Button1_Click(object sender, EventArgs e)
    {
       db_SelectedIndexChanged(this, e);
    }
   
Comments
LanFanNinja 28-Nov-11 12:11pm
   
+5 This line db.SelectedIndexChanged += db_SelectedIndexChanged;
in your Button1_Click event handler is only registering the db_SelectedIndexChanged event handler with the SelectedIndexChanged event of your drop down list.

in order the call the db_SelectedIndexChanged event handler you will need to do something like is shown above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900