Composite Control MultiView and Postback

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}:poll runat=server></{0}:poll>")]

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", "", "");

}



}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top