C
Chris
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
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