Dynamicly created Radio Buttons

Discussion in 'ASP .Net' started by Wayne Sepega, Jan 5, 2006.

  1. Wayne Sepega

    Wayne Sepega Guest

    I have the following Code:

    foreach (MyClass d in myClassCollection)
    {
    curRadioBtn = new RadioButton();
    curRadioBtn.Text = d.ToString();
    curRadioBtn.GroupName = "Contact";
    curRadioBtn.ID = d.Name;
    curRadioBtn.EnableViewState = true;
    curRadioBtn.CheckedChanged += RadioButton_CheckedChanged;
    curPanel = new Panel();
    curPanel.Height = curRadioBtn.Height;
    curPanel.Controls.Add(curRadioBtn);
    panel1.Controls.Add(curPanel);
    }

    and I have two other RadioButtons on the form already, they are on Panel1.
    The ones on the form cause RadioButton_CheckedChanged to be fired on a post
    back, but the dynamiclly created ones don't.

    The questions I have are:
    1) How do I cause the dynamic radio buttons to fire the event?
    2) How do I tell which one is selected, out side of firing the event?
    I know I can get the ID in the event from the sender, just curious
    on how to do it without the event.
    3) When I do the post back all the dynamicly created RadioButtons go away,
    how do I force the page to remember them when it's redisplayed without
    having to re-create them?

    Thanks
    Wayne Sepega
     
    Wayne Sepega, Jan 5, 2006
    #1
    1. Advertising

  2. Wayne Sepega

    Teemu Keiski Guest

    Hi,

    there would be answers to those questions, however, have you considered
    using easier approach, such as creating them with a Repeater?

    Sample (though with textBoxes)
    http://forums.asp.net/1158998/ShowPost.aspx

    But quick replies:

    1. Dynamic controls need to be added on every request and on request
    (postback) expecting the events to be raised, they need to be added to
    Controls collection at Load event, because postback data loading happens
    last time (fixed stage) right after Load.

    2. There's not very practical ways. It would need inspecting Request.Form
    collection, and locating the control with key there (matching control's
    UniqueID), and then getting reference to the instance with Page.FindControl

    3. Either with repeater as I demonstrated (Repeater handles control
    recreation dirty work), or then just you need to create them on postback,
    because postback events won't be raised otherwise.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke



    "Wayne Sepega" <> wrote in message
    news:...
    >I have the following Code:
    >
    > foreach (MyClass d in myClassCollection)
    > {
    > curRadioBtn = new RadioButton();
    > curRadioBtn.Text = d.ToString();
    > curRadioBtn.GroupName = "Contact";
    > curRadioBtn.ID = d.Name;
    > curRadioBtn.EnableViewState = true;
    > curRadioBtn.CheckedChanged += RadioButton_CheckedChanged;
    > curPanel = new Panel();
    > curPanel.Height = curRadioBtn.Height;
    > curPanel.Controls.Add(curRadioBtn);
    > panel1.Controls.Add(curPanel);
    > }
    >
    > and I have two other RadioButtons on the form already, they are on Panel1.
    > The ones on the form cause RadioButton_CheckedChanged to be fired on a
    > post back, but the dynamiclly created ones don't.
    >
    > The questions I have are:
    > 1) How do I cause the dynamic radio buttons to fire the event?
    > 2) How do I tell which one is selected, out side of firing the event?
    > I know I can get the ID in the event from the sender, just curious
    > on how to do it without the event.
    > 3) When I do the post back all the dynamicly created RadioButtons go away,
    > how do I force the page to remember them when it's redisplayed without
    > having to re-create them?
    >
    > Thanks
    > Wayne Sepega
    >
     
    Teemu Keiski, Jan 5, 2006
    #2
    1. Advertising

  3. Wayne Sepega

    Jeff Guest

    Here are a few articles that will help to explain the sequence of things and
    WHY controls need to be reloaded on each postback - and which event to use -
    and other concepts relevant to your question.

    http://aspnet.4guysfromrolla.com/articles/081402-1.aspx

    http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

    http://aspalliance.com/642

    http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.asp

    http://www.15seconds.com/issue/020102.htm

    http://www.eggheadcafe.com/articles/20050609.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspdatastructures.asp

    -HTH


    "Wayne Sepega" <> wrote in message
    news:...
    >I have the following Code:
    >
    > foreach (MyClass d in myClassCollection)
    > {
    > curRadioBtn = new RadioButton();
    > curRadioBtn.Text = d.ToString();
    > curRadioBtn.GroupName = "Contact";
    > curRadioBtn.ID = d.Name;
    > curRadioBtn.EnableViewState = true;
    > curRadioBtn.CheckedChanged += RadioButton_CheckedChanged;
    > curPanel = new Panel();
    > curPanel.Height = curRadioBtn.Height;
    > curPanel.Controls.Add(curRadioBtn);
    > panel1.Controls.Add(curPanel);
    > }
    >
    > and I have two other RadioButtons on the form already, they are on Panel1.
    > The ones on the form cause RadioButton_CheckedChanged to be fired on a
    > post back, but the dynamiclly created ones don't.
    >
    > The questions I have are:
    > 1) How do I cause the dynamic radio buttons to fire the event?
    > 2) How do I tell which one is selected, out side of firing the event?
    > I know I can get the ID in the event from the sender, just curious
    > on how to do it without the event.
    > 3) When I do the post back all the dynamicly created RadioButtons go away,
    > how do I force the page to remember them when it's redisplayed without
    > having to re-create them?
    >
    > Thanks
    > Wayne Sepega
    >
     
    Jeff, Jan 5, 2006
    #3
    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. Scott D Johnson
    Replies:
    1
    Views:
    1,096
    Scott D Johnson
    Sep 27, 2006
  2. dotNet

    Problem with dynamicly added buttons in PlaceHolder

    dotNet, Feb 15, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    162
    dotNet
    Feb 15, 2005
  3. mortb
    Replies:
    2
    Views:
    155
    Grant Wagner
    Jul 2, 2004
  4. Dr. Leff
    Replies:
    3
    Views:
    312
    David Mark
    Oct 15, 2007
  5. Dr. Leff
    Replies:
    3
    Views:
    240
    David Mark
    Oct 15, 2007
Loading...

Share This Page