Composite Control MultiView and Postback

Discussion in 'ASP .Net Building Controls' started by gizm0, Feb 16, 2006.

  1. gizm0

    gizm0 Guest

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

    }



    }
    gizm0, Feb 16, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,686
    sleigh
    Feb 12, 2004
  2. Andy B
    Replies:
    0
    Views:
    373
    Andy B
    Dec 29, 2008
  3. debartsa
    Replies:
    1
    Views:
    256
    John Saunders
    Apr 23, 2004
  4. Chad
    Replies:
    0
    Views:
    220
  5. STech

    Composite control, postback and change control tree

    STech, Nov 4, 2005, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    518
    Steven Cheng[MSFT]
    Nov 21, 2005
Loading...

Share This Page