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

Discussion in 'ASP .Net Web Controls' started by Biguana, Mar 29, 2006.

  1. Biguana

    Biguana Guest

    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)
    {

    }
    }
     
    Biguana, Mar 29, 2006
    #1
    1. Advertising

  2. Biguana

    Guest

    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.
     
    , Mar 29, 2006
    #2
    1. Advertising

  3. Biguana

    Biguana Guest

    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
     
    Biguana, Mar 30, 2006
    #3
  4. Biguana

    Guest

    My pleasure.
     
    , Mar 30, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotr Strycharz

    CheckedChanged does not fire in Repeater

    Piotr Strycharz, Sep 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    5,402
  2. Amelyan
    Replies:
    2
    Views:
    695
    Amelyan
    Nov 5, 2005
  3. jobs at webdos
    Replies:
    0
    Views:
    476
    jobs at webdos
    Oct 8, 2006
  4. msimmons
    Replies:
    0
    Views:
    528
    msimmons
    Jul 16, 2009
  5. K Viltersten

    How to force CheckedChanged method to fire?

    K Viltersten, Oct 11, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    773
    harborsparrow
    Oct 12, 2009
Loading...

Share This Page