G
gizm0
Hi
I'm creating composite control which is derived from MultiView control.
First view contains Label and RadioButtonList, second view contains
LiteralControl with generated code to display results.
I'm running page with this control and after selecting answer in
RadioButtonList i get the following message:
ActiveViewIndex is being set to '0'. It must be smaller than the current
number of View controls '0'. For dynamically added views, make sure they are
added before or in Page_Init event.
Parameter name: value
Anybody help me? Here's the control's code:
[DefaultProperty("ResultsColor")]
[ToolboxData("<{0}oll runat=server></{0}oll>")]
public class Poll : MultiView,
INamingContainer
{
public Poll() : base()
{}
private Polls p;
#region Poll's Composite Controls
private View vPoll;
private View vResults;
private RadioButtonList rbPoll;
private Label lblPoll;
#endregion
[Browsable(false)]
public int? SelectedOption
{
get
{
Object c = ViewState["SelectedOption"];
return (c == null) ? -1 : Convert.ToInt32(c);
}
set
{
ViewState["SelectedOption"] = value;
}
}
protected override void Render(HtmlTextWriter output)
{
EnsureChildControls();
RenderChildren(output);
}
protected override void CreateChildControls()
{
CreatePoll();
this.Controls.Clear();
vPoll = new View();
vPoll.ID = "vPoll";
vResults = new View();
vResults.ID = "vResults";
lblPoll = new Label();
lblPoll.ID = "lblPoll";
lblPoll.Text = p.GetPollQuestion();
rbPoll = new RadioButtonList();
rbPoll.ID = "rbPoll";
rbPoll.DataTextField = "answer";
rbPoll.DataValueField = "id";
rbPoll.AutoPostBack = true;
rbPoll.Visible = true;
rbPoll.SelectedIndexChanged += new
EventHandler(rbPoll_SelectedIndexChanged);
rbPoll.DataSource = p.GetPollAnswers();
rbPoll.DataBind();
rbPoll.SelectedIndex = -1;
vPoll.Controls.Add(lblPoll);
vPoll.Controls.Add(rbPoll);
this.Views.Add(vPoll);
this.Views.Add(vResults);
this.SetActiveView(vPoll);
ChildControlsCreated = true;
}
protected void rbPoll_SelectedIndexChanged(object sender, EventArgs e)
{
CreatePoll();
SelectedOption = Convert.ToInt32( rbPoll.SelectedItem.Value );
p.Vote(SelectedOption.ToString(), this.Context.Request.Cookies);
DisplayPollResults();
}
private void DisplayPollResults()
{
this.SetActiveView(vResults);
string s = p.GetDisplayView();
this.Views[1].Controls.Add(new LiteralControl(s));
}
private void CreatePoll()
{
p = new Polls("", ServerEnum.SqlServer2005, Environment.MachineName +
"\\SQLEXPRESS", "CMF_DB", "", "");
}
}
I'm creating composite control which is derived from MultiView control.
First view contains Label and RadioButtonList, second view contains
LiteralControl with generated code to display results.
I'm running page with this control and after selecting answer in
RadioButtonList i get the following message:
ActiveViewIndex is being set to '0'. It must be smaller than the current
number of View controls '0'. For dynamically added views, make sure they are
added before or in Page_Init event.
Parameter name: value
Anybody help me? Here's the control's code:
[DefaultProperty("ResultsColor")]
[ToolboxData("<{0}oll runat=server></{0}oll>")]
public class Poll : MultiView,
INamingContainer
{
public Poll() : base()
{}
private Polls p;
#region Poll's Composite Controls
private View vPoll;
private View vResults;
private RadioButtonList rbPoll;
private Label lblPoll;
#endregion
[Browsable(false)]
public int? SelectedOption
{
get
{
Object c = ViewState["SelectedOption"];
return (c == null) ? -1 : Convert.ToInt32(c);
}
set
{
ViewState["SelectedOption"] = value;
}
}
protected override void Render(HtmlTextWriter output)
{
EnsureChildControls();
RenderChildren(output);
}
protected override void CreateChildControls()
{
CreatePoll();
this.Controls.Clear();
vPoll = new View();
vPoll.ID = "vPoll";
vResults = new View();
vResults.ID = "vResults";
lblPoll = new Label();
lblPoll.ID = "lblPoll";
lblPoll.Text = p.GetPollQuestion();
rbPoll = new RadioButtonList();
rbPoll.ID = "rbPoll";
rbPoll.DataTextField = "answer";
rbPoll.DataValueField = "id";
rbPoll.AutoPostBack = true;
rbPoll.Visible = true;
rbPoll.SelectedIndexChanged += new
EventHandler(rbPoll_SelectedIndexChanged);
rbPoll.DataSource = p.GetPollAnswers();
rbPoll.DataBind();
rbPoll.SelectedIndex = -1;
vPoll.Controls.Add(lblPoll);
vPoll.Controls.Add(rbPoll);
this.Views.Add(vPoll);
this.Views.Add(vResults);
this.SetActiveView(vPoll);
ChildControlsCreated = true;
}
protected void rbPoll_SelectedIndexChanged(object sender, EventArgs e)
{
CreatePoll();
SelectedOption = Convert.ToInt32( rbPoll.SelectedItem.Value );
p.Vote(SelectedOption.ToString(), this.Context.Request.Cookies);
DisplayPollResults();
}
private void DisplayPollResults()
{
this.SetActiveView(vResults);
string s = p.GetDisplayView();
this.Views[1].Controls.Add(new LiteralControl(s));
}
private void CreatePoll()
{
p = new Polls("", ServerEnum.SqlServer2005, Environment.MachineName +
"\\SQLEXPRESS", "CMF_DB", "", "");
}
}