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

Reply
Thread Tools

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

 
 
Biguana
Guest
Posts: n/a
 
      03-29-2006
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
offer.

Tim

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

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


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

}
LiteralControl x = new
LiteralControl();
x.Text = "<br>";
PlaceHolder1.Controls.Add(x);


}
}

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.
//
InitializeComponent();
base.OnInit(e);
}

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

}
#endregion

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

}
}

 
Reply With Quote
 
 
 
 
CaffieneRush@gmail.com
Guest
Posts: n/a
 
      03-29-2006
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
Panel1.Controls.Add(rb)
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.
'Panel1.Controls.Add(rb)
AddHandler rb.CheckedChanged, AddressOf MyHandler
Next
Dim x As New LiteralControl
x.Text = "<br />"
Panel1.Controls.Add(x)
Next
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

Regards.

 
Reply With Quote
 
 
 
 
Biguana
Guest
Posts: n/a
 
      03-30-2006
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.

Tim

 
Reply With Quote
 
CaffieneRush@gmail.com
Guest
Posts: n/a
 
      03-30-2006
My pleasure.

 
Reply With Quote
 
 
 
Reply

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



Advertisments