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