I'm trying to develop a control for a questionnaire application. In this control, it receives a string array as the answers. Then it creates an associated radio button for each answer. The problem is that when I want to use this control on a page, I also have a button on the page so it posts back to itself. When it posts back, it recreates the control, but loses it's state. I'm not sure how to persist the users selection when it's posted back. What else do I need to do? Thanks.namespace WebPEQ.Controls{
[DefaultProperty("ID"), ToolboxData("<{0}:RangeGroup runat=server></{0}:RangeGroup>")] public class RangeGroup : System.Web.UI.WebControls.WebControl, IAnswer, INamingContainer { private string[] _answers; private HorizontalAlign _horAlign = HorizontalAlign.Left; private VerticalAlign _vertAlign = VerticalAlign.Top; private Alignment _placement = Alignment.Left; private System.Web.UI.WebControls.Table OutputTable; private bool _required = false; private string _errMessage = string.Empty; private System.Collections.ArrayList AnswerRadios;
#region Constructor public RangeGroup() { OutputTable = new Table(); } #endregion
#region Ranges public string[] Answers { get{ return _answers; } set { if (value != null && value.Length > 0) { _answers = new string[value.Length]; value.CopyTo(_answers, 0); AnswerRadios = new System.Collections.ArrayList(); for (int i = 0; i < _answers.Length; i++) AnswerRadios.Add(new RadioButton()); } } } #endregion
#region Properties public string Css { get { return OutputTable.CssClass; } set{ OutputTable.CssClass = value.Trim(); } }
public bool Required { set{ _required = value; } get{ return _required; } }
public string ErrorMessage { set{ _errMessage = value.Trim(); } get{ return _errMessage; } }
#region Alignment Properties public VerticalAlign VAlign { set { _vertAlign = value; } }
public HorizontalAlign TextAlign { set { _horAlign = value; } }
public Alignment TextPlacement { set { _placement = value; } } #endregion #endregion
#region Render /// <summary> /// Render this control to the output parameter specified. /// </summary> /// <param name="output"> The HTML writer to write out to </param> protected override void Render(HtmlTextWriter output) { switch (_placement) { case Alignment.Bottom: BuildVerticalTable(false); break; case Alignment.Left: BuildHorizontalTable(true); break; case Alignment.Right: BuildHorizontalTable(false); break; default: BuildVerticalTable(true); break; } this.Controls.Add(OutputTable); OutputTable.RenderControl(output); } #endregion
#region BuildHorizontalTable private void BuildHorizontalTable(bool TextOnLeft) {
for (int i = 0; i < this._answers.Length; i++) { TableRow row = new TableRow(); TableCell c1 = new TableCell(); TableCell c2 = new TableCell();
if (TextOnLeft) { c1.Text = this._answers[i].ToString().Trim(); RadioButton r = (RadioButton)AnswerRadios[i]; r.GroupName = this.ID; c2.Controls.Add(r); } else { RadioButton radio = new RadioButton(); RadioButton r = (RadioButton)AnswerRadios[i]; r.GroupName = this.ID; c1.Controls.Add(r); c2.Text = this._answers[i].ToString().Trim(); }
c1.VerticalAlign = _vertAlign; c1.HorizontalAlign = this._horAlign; c2.VerticalAlign = _vertAlign; c2.HorizontalAlign = this._horAlign; row.Cells.Add(c1); row.Cells.Add(c2);
OutputTable.Rows.Add(row); } } #endregion
#region BuildVerticalTable private void BuildVerticalTable(bool TextOnTop) { TableRow TextRow = new TableRow(); TableRow RadioRow = new TableRow();
for (int i = 0; i < this._answers.Length; i++) { TableCell c = new TableCell(); c.VerticalAlign = this._vertAlign; c.HorizontalAlign = this._horAlign; c.Text = _answers[i].ToString().Trim(); TextRow.Cells.Add(c); c.Dispose(); c = null;
c = new TableCell(); c.VerticalAlign = this._vertAlign; c.HorizontalAlign = this._horAlign; RadioButton r = (RadioButton)AnswerRadios[i]; r.GroupName = this.ID; c.Controls.Add(r); RadioRow.Cells.Add(c); c.Dispose(); c = null; }
if (TextOnTop) { OutputTable.Rows.Add(TextRow); OutputTable.Rows.Add(RadioRow); } else { OutputTable.Rows.Add(RadioRow); OutputTable.Rows.Add(TextRow); } } #endregion
#region Validate public bool Validate() { if (_required) { int SelectedCount = 0;
for (int i =0; i < AnswerRadios.Count; i++) { RadioButton r = AnswerRadios[i] as RadioButton; if (r.Checked) { SelectedCount = 1; break; } }
if (SelectedCount == 0) { TableRow row = new TableRow(); TableCell c = new TableCell();
if (_errMessage != string.Empty) c.Text = "<font color=\"red\">" + _errMessage + "</font>"; else c.Text = "<font color=\"red\">Required Field</font>";
c.ColumnSpan = OutputTable.Rows[0].Cells.Count; row.Cells.Add(c); OutputTable.Rows.AddAt(0,row); return false; } else return true; } else return true; } #endregion
protected override void LoadViewState(object savedState) { base.LoadViewState (savedState); }
}}