Home All Groups Group Topic Archive Search About

Dynamic EventHandler Firing Every Postback

Author
13 Sep 2007 4:21 PM
StanRajan
I am work which populates a placeholder with controls that are driven
by the database.  For specific radiobuttonlist controls I need to add
an eventhandler on the selectedindexchanged event.  The problem I am
encountering is that the event is being fired every time any postback
occurs to the page.  Also, I am using the same event handler for all
the radiobuttonlist's that I need to capture the event for and using
the sender to get the ID of which control caused the postback.

The EventHandler used for the SelectedIndexChanged event:

private void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList rbl = (RadioButtonList) sender;

    for(int r=0; r < rbl.Items.Count; r++)
    {
        if(rbl.Items[r].Selected)
        {
            // DB UPDATE
        }
    }
}


The assignment of the event handlers in the OnInit method:

override protected void OnInit(EventArgs e)
{
    // DYNAMIC CONTROLS CREATED

    // ASSIGN EVENT HANDLERS
    ((RadioButtonList) this.FindControl("rbl1")).SelectedIndexChanged +=
new EventHandler(this.rbl_SelectedIndexChanged);
    ((RadioButtonList) this.FindControl("rbl1")).AutoPostBack = true;

    ((RadioButtonList) this.FindControl("rbl2")).SelectedIndexChanged +=
new EventHandler(this.rbl_SelectedIndexChanged);
    ((RadioButtonList) this.FindControl("rbl2")).AutoPostBack = true;
}


Been stuck on this for awhile, any help would be greatly appreciated.

Thanks,
Stan

AddThis Social Bookmark Button