WebControl & Viewstate

Discussion in 'ASP .Net Web Controls' started by Chris, Jan 23, 2004.

  1. Chris

    Chris Guest

    I'm trying to create a WebControl that creates additional WebControls at runtime and persists their data across the PostBack.

    The basic idea is this:

    public class WebControl1: System.Web.UI.WebControls.WebControl
    {
    // Member Variables, Properties, etc...

    protected override void Render(HtmlTextWriter output)
    {
    RadioButton myRadioButton = new RadioButton();
    myRadioButton.ID = "RadioButton1";
    myRadioButton.GroupName = "RadioButtonGroup1";
    Page.Controls.Add(myRadioButton);
    myRadioButton.RenderControl(output);
    }
    }

    When this control is used on a page such as:

    <tag:webcontrol id="WebControl1" runat="server"></tag:webcontrol>

    ..... everything loads up nicely (i.e. a RadioButton is rendered to the page). Obviously, because the control (myRadioButton) is created each time the class is loaded and the HTML output is rendered, if the RadioButton is toggled (checked), it will not retain its state.

    Rather than go into all of the things I've *tried* to do to get this RadioButton to persist its Checked property across PostBack's, I'll just pose my question directly which is what do I need to add to this (very simple) code to get that RadioButton to persist its state across PostBack's?

    Obviously, at some point I need to *save* the value the user sets it to and at some point I need to check that value and restore it when the control is re-created on PostBack but where? Override some methods (SaveViewState/LoadViewState)? Implement an Interface (IPostBackDataHandler, etc...)?

    Much thanks in advance -

    - Chris
     
    Chris, Jan 23, 2004
    #1
    1. Advertising

  2. Hi Chris,

    The radiobutton is not saving its state because it is being created too
    late; at Render lots of important key events have already fired (Init,
    viewstate loading, etc) and your control was not there yet to be called by
    the fx to update its state/etc. Please take a look at the Control Execution
    Lifecycle to learn more about how it will affect dynamically created
    controls. Here[1] you will find a link to the docs and some insights I've
    written about the topic.

    [1] http://weblogs.asp.net/vga/archive/2003/08/11/23498.aspx

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Chris" <> wrote in message
    news:...
    > I'm trying to create a WebControl that creates additional WebControls at

    runtime and persists their data across the PostBack.
    >
    > The basic idea is this:
    >
    > public class WebControl1: System.Web.UI.WebControls.WebControl
    > {
    > // Member Variables, Properties, etc...
    >
    > protected override void Render(HtmlTextWriter output)
    > {
    > RadioButton myRadioButton = new RadioButton();
    > myRadioButton.ID = "RadioButton1";
    > myRadioButton.GroupName = "RadioButtonGroup1";
    > Page.Controls.Add(myRadioButton);
    > myRadioButton.RenderControl(output);
    > }
    > }
    >
    > When this control is used on a page such as:
    >
    > <tag:webcontrol id="WebControl1" runat="server"></tag:webcontrol>
    >
    > ... everything loads up nicely (i.e. a RadioButton is rendered to the

    page). Obviously, because the control (myRadioButton) is created each time
    the class is loaded and the HTML output is rendered, if the RadioButton is
    toggled (checked), it will not retain its state.
    >
    > Rather than go into all of the things I've *tried* to do to get this

    RadioButton to persist its Checked property across PostBack's, I'll just
    pose my question directly which is what do I need to add to this (very
    simple) code to get that RadioButton to persist its state across PostBack's?
    >
    > Obviously, at some point I need to *save* the value the user sets it to

    and at some point I need to check that value and restore it when the control
    is re-created on PostBack but where? Override some methods
    (SaveViewState/LoadViewState)? Implement an Interface (IPostBackDataHandler,
    etc...)?
    >
    > Much thanks in advance -
    >
    > - Chris
    >
     
    Victor Garcia Aprea [MVP], Jan 23, 2004
    #2
    1. Advertising

  3. Chris

    CMA Guest

    r u creating the control in page load event???
    if so, u r creating it again and again. u can make sure it with a break
    point. if so, create it withing this code.

    if(!IsPostBack)
    {
    // create control here
    }

    in this way, u r creating the control only once. it will remain the state by
    default.

    if u r not creating the controll every time, my guess is wrong.

    hope this helps.
    regards,
    CMA




    "Chris" <> wrote in message
    news:...
    > I'm trying to create a WebControl that creates additional WebControls at

    runtime and persists their data across the PostBack.
    >
    > The basic idea is this:
    >
    > public class WebControl1: System.Web.UI.WebControls.WebControl
    > {
    > // Member Variables, Properties, etc...
    >
    > protected override void Render(HtmlTextWriter output)
    > {
    > RadioButton myRadioButton = new RadioButton();
    > myRadioButton.ID = "RadioButton1";
    > myRadioButton.GroupName = "RadioButtonGroup1";
    > Page.Controls.Add(myRadioButton);
    > myRadioButton.RenderControl(output);
    > }
    > }
    >
    > When this control is used on a page such as:
    >
    > <tag:webcontrol id="WebControl1" runat="server"></tag:webcontrol>
    >
    > ... everything loads up nicely (i.e. a RadioButton is rendered to the

    page). Obviously, because the control (myRadioButton) is created each time
    the class is loaded and the HTML output is rendered, if the RadioButton is
    toggled (checked), it will not retain its state.
    >
    > Rather than go into all of the things I've *tried* to do to get this

    RadioButton to persist its Checked property across PostBack's, I'll just
    pose my question directly which is what do I need to add to this (very
    simple) code to get that RadioButton to persist its state across PostBack's?
    >
    > Obviously, at some point I need to *save* the value the user sets it to

    and at some point I need to check that value and restore it when the control
    is re-created on PostBack but where? Override some methods
    (SaveViewState/LoadViewState)? Implement an Interface (IPostBackDataHandler,
    etc...)?
    >
    > Much thanks in advance -
    >
    > - Chris
    >
     
    CMA, Jan 23, 2004
    #3
  4. Chris

    Chris Guest

    Thank you!!! I've got it working now.

    I can't believe all this time I should have just been creating these things in Page_Load()... Doh! There are so many methods, events, interfaces, etc... it all starts to mash together!

    - Chris
     
    Chris, Jan 23, 2004
    #4
  5. Glad I could help :)

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Chris" <> wrote in message
    news:...
    > Thank you!!! I've got it working now.
    >
    > I can't believe all this time I should have just been creating these

    things in Page_Load()... Doh! There are so many methods, events, interfaces,
    etc... it all starts to mash together!
    >
    > - Chris
     
    Victor Garcia Aprea [MVP], Jan 23, 2004
    #5
    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. Guest
    Replies:
    2
    Views:
    756
  2. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    221
    Luis Ramírez
    Jul 6, 2004
  3. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    215
    Kasabaarde Sumta
    Dec 13, 2004
  4. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    206
    Fabricio
    Sep 21, 2005
  5. Peter Morris [Air Software Ltd]

    WebControl with non-WebControl property

    Peter Morris [Air Software Ltd], Dec 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    150
    Peter Morris [Air Software Ltd]
    Dec 7, 2004
Loading...

Share This Page