回发后的多视图控件

发布于 2024-11-14 19:49:47 字数 1186 浏览 5 评论 0原文

我正在尝试使用多视图控件。我首先创建一些视图并在 preinit 事件中添加一些标签。将它们添加到 (!isPostBack) 场景中的多视图中。 我想使用“下一个”和“上一个”按钮在视图之间导航。这就是我所做的:

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--;
}

这将不起作用,就好像多视图不保存其内容一样,并且不允许我将 activeviewindex 更改为大于 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2024-11-21 19:49:47

您每次都需要重新创建所有动态控件。如果将代码更改为以下内容,按钮应该可以工作:

    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--;
    }

我将代码移至 OnInit,因为由于某种原因,MultiView 尚未在 Pre_Init 上初始化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文