0

I'm trying to use a multiview control. I'm first creating some views and add to those some labels in the preinit event. Add those to the multiview in the (!isPostBack) scenario. I want to navigate between Views using 'next' and 'previous' buttons. This is what I've done:

protected void Page_PreInit(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        MultiView1 = (MultiView)Session["multi"];            
    }
    else
    {
        View view1 = new View();
        View view2 = new View();
        View view3 = new View();
        Label l1 = new Label(); l1.Text = "1";
        Label l2 = new Label(); l2.Text = "2";
        Label l3 = new Label(); l3.Text = "3";
        view1.Controls.Add(l1);
        view2.Controls.Add(l2);
        view3.Controls.Add(l3);
        MultiView1.Views.Add(view1);
        MultiView1.Views.Add(view2);
        MultiView1.Views.Add(view3);
        MultiView1.ActiveViewIndex = 0;
        Session["multi"] = MultiView1;
    }
}    
protected void Page_Load(object sender, EventArgs e)
{        
}
protected void Button2_Click(object sender, EventArgs e)
{
    MultiView1.ActiveViewIndex++;
}
protected void Button1_Click(object sender, EventArgs e)
{
    MultiView1.ActiveViewIndex--;
}

This will not work as if the multiview does not save it's content and won't allow me to change activeviewindex to a value greater than 0. How can i modify it such that i'll be allowed to change activeviewindex?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Florin
  • 3
  • 1
  • 3

1 Answers1

1

You need to recreate all dynamic controls each time. If you change the code to the following the buttons should work:

    protected override void  OnInit(EventArgs e)
    {
        View view1 = new View();
        View view2 = new View();
        View view3 = new View();
        Label l1 = new Label();
        Label l2 = new Label();
        Label l3 = new Label();

        l1.Text = "1";
        l2.Text = "2";
        l3.Text = "3";

        view1.Controls.Add(l1);
        view2.Controls.Add(l2);
        view3.Controls.Add(l3);
        MultiView1.Views.Add(view1);
        MultiView1.Views.Add(view2);
        MultiView1.Views.Add(view3);
        MultiView1.ActiveViewIndex = 0;

        base.OnInit();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex++;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex--;
    }

I moved the code to OnInit because for some reason the MultiView was not yet initialized on Pre_Init.

marto
  • 4,170
  • 1
  • 28
  • 39
  • 1
    If you are overriding the OnInit, do you still need to make sure you call the base.OnInit? Or should you instead use the Page_Init magic method? – drovani Jun 09 '11 at 13:55
  • @drovani you need to call base.OnInit if you want the OnInit event to fire. I guess it's best to always have it or as you said create a Page_Init method. I changed the example and added base.OnInit(). – marto Jun 09 '11 at 14:05
  • But what if i want to change some controls within the views? Isn't there a possibility to save the whole object within the session or something similar such that I don't have to add views dynamically every time i load the page? Using the method you provided, I have to save my controls each time in a session and load them back for each load, just so I can add them again in dynamically added views. Am I correct? – Florin Jun 10 '11 at 10:22
  • There is no point to save them in session. As long as you recreate them every time their state is kept using the page's viewstate. – marto Jun 10 '11 at 13:21