Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamically created (and selected) radiobuttons fire CheckedChanged event on Postback

Thread Tools

Dynamically created (and selected) radiobuttons fire CheckedChanged event on Postback

Posts: n/a
Hi there,

Wondering if anyone can help as this is driving me nuts.

I have a c# page with a lot of dynamically created controls. Some are
user controls where the user can select which section to complete using
radiobuttons. I create the groups of radiobuttons (always with the
first one in the group selected) and give them event handlers (all
pointing to the same function), and then the page gets posted back
(either by a radio button or another control).

The problem is, on the postback all the selections (from the previous
creation code I guess) fire their events and all the associated code
which I really don't want. I can't see a way to distinguish the real
selection from the default selection events as they all fire. I want
it to be as if the controls were created at design time - then they
wouldn't fire events! I've tried adding the event handler before and
after the attachment to the placeholder, and in different stages of the
page lifecycle, all with the same results.

Simplified code for repeatability below. Thanks for any help you can


public class WebForm1 : System.Web.UI.Page
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder

private void Page_Load(object sender, System.EventArgs
for (int i = 0; i< 10;i++)
for (int j = 0; j< 10; j++)
RadioButton rbTemp = new
rbTemp.ID = i.ToString() + "_"
+ j.ToString();
rbTemp.GroupName =
//rbTemp.CheckedChanged += new
if (j==0 && !IsPostBack)
rbTemp.Checked = true;
rbTemp.AutoPostBack = true;

PlaceHolder1.FindControl(i.ToString() + "_" +
j.ToString())).CheckedChanged += new EventHandler(MyHandler);

LiteralControl x = new
x.Text = "<br>";


private void MyHandler(object sender,
System.EventArgs e)


// Put user code to initialize the page here

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the
ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.Button1.Click += new
this.Load += new


private void Button1_Click(object sender,
System.EventArgs e)


Reply With Quote
Posts: n/a
Add the radiobutton to the placeholder *before* setting the checked
property to true.

Partial Class Default2
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
For i As Integer = 0 To 9
For j As Integer = 0 To 9
Dim rb As RadioButton = New RadioButton
rb.ID = i.ToString + "_" + j.ToString
If j = 0 And Not IsPostBack Then
rb.Checked = True
rb.AutoPostBack = True
End If
rb.GroupName = i.ToString
'Don't add rb to panel here after rb has been checked.
AddHandler rb.CheckedChanged, AddressOf MyHandler
Dim x As New LiteralControl
x.Text = "<br />"
End Sub

Private Sub MyHandler(ByVal s As Object, ByVal e As EventArgs)
Dim rb As RadioButton = s
End Sub

Private Sub ButtonClick(ByVal s As Object, ByVal e As EventArgs)
Handles Button1.Click

End Sub
End Class


Reply With Quote
Posts: n/a
Sorted! Cheers CaffieneRush.

Be interested to know why (although it might be beyond my tiny brain!).
I would have tried it but I assumed selecting the radiobutton later
would be more likely to raise teh event, not less!

Anyway, thanks.


Reply With Quote
Posts: n/a
My pleasure.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to force CheckedChanged method to fire? K Viltersten ASP .Net 1 10-12-2009 04:30 PM
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
postback confusion - checkedchanged event and fresh data???? jobs at webdos ASP .Net 0 10-08-2006 01:35 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
CheckedChanged does not fire in Repeater Piotr Strycharz ASP .Net 3 09-14-2004 11:07 PM