【问题标题】:Using same control instance in different views of an ASP.net MultiView在 ASP.net MultiView 的不同视图中使用相同的控件实例
【发布时间】:2012-03-02 10:40:37
【问题描述】:

我有一个带有两个视图和两个用户控件的 asp:MultiView 控件。 在第一个视图中,我想将每个控件放在 ASPxTabControl 的不同选项卡中。 在第二个视图中,我想一个接一个地连续显示这两个控件。

当我在视图之间切换时,如何在每个视图中使用/看到具有完全相同视图状态的相同控件实例?

或者有没有比使用 MutliView 更好的方法?

非常感谢您的回复。

干杯

【问题讨论】:

    标签: asp.net devexpress


    【解决方案1】:

    在你的 ASPX 页面上试试这个:

        <asp:MultiView ID="mvTest" ActiveViewIndex="0" runat="server">
            <asp:View ID="vwFirst" runat="server">
                <asp:PlaceHolder ID="phFirstContainer" runat="server" ></asp:PlaceHolder>
            </asp:View>
            <asp:View ID="vwSecond" runat="server">
                <asp:PlaceHolder ID="phSecondContainer" runat="server" ></asp:PlaceHolder>
            </asp:View>
        </asp:MultiView>
        <asp:Button runat="server" ID="btnSwitchViews" Text="Switch Views" OnClick="btnSwitchViews_Click"/>
    

    这在您的 ASPX.CS 代码隐藏页面上:

        protected void Page_PreRender(object sender, EventArgs e)
        {
            var myControl = new Label() {ID = "MyTextControl"};
    
            if (mvTest.GetActiveView().ID == "vwFirst")
            {
                myControl.Text = "I'm the same controller on the first view";
                phFirstContainer.Controls.Add(myControl);
            }
            else if(mvTest.GetActiveView().ID == "vwSecond")
            {
                myControl.Text = "I'm the same controller on the second view";
                phSecondContainer.Controls.Add(myControl);
            }
        }
    
        protected void btnSwitchViews_Click(object sender, EventArgs e)
        {
            mvTest.ActiveViewIndex = mvTest.ActiveViewIndex == 0 ? 1 : 0;
        }
    

    为了简单起见,我使用 ASP.NET 标签,但您可以通过使用相关用户控件的类型以编程方式对其进行实例化来轻松使用用户控件。

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多