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

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top