Dynamic EventHandler Firing Every Postback

S

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top