|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamically created (and selected) radiobuttons fire CheckedChanged event on PostbackWondering 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) { } } 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. 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
row count in inherited GridView control
Use windows control in asp.net 2.0 Re: How to get underlying data of TreeView control? (ASP.NET 2.0) Treeview control determining if node expanded or selected stylesheet on ASP.NET 2.0 TreeView and Menu Control want to create custom control using graphics Design Time Error: Error Rendering Control ValidateEvent bug with table and radio button Panel with Header Change style of Edit-button in GridView? |
|||||||||||||||||||||||