Sure, first is the code for the control. 2nd is the containing page code.
namespace
WebPEQ.Controls
{
/// <summary>
/// Summary description for RangeGroup.
/// </summary>
[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(string strAnswers)
{
OutputTable =
new Table();
}
#endregion
#region
Ranges
//this is the only thing that I need to save to viewstate
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
{
// if (ViewState["CSS"] == null)
return OutputTable.CssClass;
// else
// return ViewState["CSS"];
}
set{
OutputTable.CssClass =
value.Trim();
// ViewState["CSS"] == value;
}
}
public bool Required
{
set{
// ViewState["Required"] = value;
_required =
value;
}
get{
// if (ViewState["Required"] == null)
return _required;
// else
// return ViewState["Required"];
}
}
public string ErrorMessage
{
set{
// ViewState["ErrorMessage"] = value;
_errMessage =
value.Trim();
}
get{
// if (ViewState["ErrorMessage"] != null)
// return ViewState["ErrorMessage"];
// else
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.ToString().Trim();
RadioButton r = (RadioButton)AnswerRadios;
r.GroupName =
this.ID;
r.EnableViewState =
true;
//r.Checked = i==0?true:false;
c2.Controls.Add(r);
}
else
{
RadioButton radio =
new RadioButton();
RadioButton r = (RadioButton)AnswerRadios;
r.GroupName =
this.ID;
r.EnableViewState =
true;
//r.Checked = i==0?true:false;
c1.Controls.Add(r);
c2.Text =
this._answers.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.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;
r.EnableViewState =
true;
r.GroupName =
this.ID;
//r.Checked = i==0?true:false;
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;
// int RowCount = OutputTable.Rows.Count;
//
// for (int i = 0; i < RowCount; i++)
// {
// foreach (TableCell c in OutputTable.Rows.Cells)
// {
// foreach (Control ctrl in c.Controls)
// {
// if (ctrl.GetType().ToString() == "System.Web.UI.WebControls.RadioButton")
// {
// RadioButton rd = (RadioButton)ctrl;
// if (rd.Checked)
// {
// SelectedCount = 1;
// break;
// }
// }
// if (SelectedCount > 0)
// break;
// }
// if (SelectedCount > 0)
// break;
// }
// }
for (int i =0; i < AnswerRadios.Count; i++)
{
RadioButton r = AnswerRadios
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);
}
}
}
///////////////////////////containing page code here
public
class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder plcContainer;
private void Page_Load(object sender, System.EventArgs e)
{
}
private void LoadControls()
{
RangeGroup r =
new RangeGroup();
r.ID = "33";
string[] texts = {"text1","text2","text3"};
r.Answers = texts;
r.TextPlacement = Alignment.Right;
r.TextAlign = HorizontalAlign.Left;
r.VAlign = VerticalAlign.Top;
r.ErrorMessage = "This is a required field.";
r.Required =
true;
plcContainer.Controls.Add(r);
r.Dispose(); r =
null;
}
#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);
LoadControls();
}
/// <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)
{
RangeGroup rg = (WebPEQ.Controls.RangeGroup)Page.FindControl("33"); //keeps coming back null here
}
}