2012-03-02 40 views
3

我有一個具有兩個視圖和兩個UserControl的asp:MultiView控件。 在第一個視圖中,我想將每個控件放在ASPxTabControl的不同選項卡中。 在第二個視圖中,我想要連續顯示兩個控件。在ASP.net的不同視圖中使用相同的控件實例MultiView

如何在每個視圖中實現這一點?在切換視圖之間使用/看到相同的控件實例和完全相同的視圖狀態?

或者還有更好的方法,而不是使用MutliView?

非常感謝您的提前答覆。

乾杯

回答

0

試試這個您的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標籤爲簡單起見,但您可以使用相關用戶控件的類型以編程方式實例化用戶控件。

相關問題