开发者

multiview controls after postback

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.Cont开发者_运维百科rols.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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜