sometime we need use multiview to show some many content, this is a demo for it. Let's go!
client code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>This is a practice for MultiView and View</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<h1>MultiView and View</h1><br />
<asp:RadioButtonList ID="rdlView" runat="server" AutoPostBack="true"
RepeatDirection="Horizontal"
onselectedindexchanged="rdlView_SelectedIndexChanged">
<asp:ListItem Value="-1">Nothing</asp:ListItem>
<asp:ListItem Selected=True Value="0">First View</asp:ListItem>
<asp:ListItem Value="1">Second View</asp:ListItem>
<asp:ListItem Value="2">Third View</asp:ListItem>
<asp:ListItem Value="3">Last View</asp:ListItem>
</asp:RadioButtonList><br />
Current Index:
<asp:Label ID="lblCurrentIndex" runat="server"></asp:Label>
<br />
<asp:MultiView ID="MultiView1" runat="server"
onactiveviewchanged="MultiView1_ActiveViewChanged" ActiveViewIndex=0>
<asp:View ID="vwFirst" runat="server" OnActivate="ActivateView"
ondeactivate="DeactivateView">
<h2>First View</h2>
<asp:TextBox ID="txtFirstView" runat="server" />
<asp:Button ID="btnNext1" runat="server" CommandName="NextView" Text="Go to Next" />
<asp:Button ID = "btnLast" runat="server" CommandName="SwitchViewByID" CommandArgument="vwLast" Text = "Go to Last" />
</asp:View>
<asp:View ID="vwSecond" runat="server" OnActivate="ActivateView" OnDeactivate="DeactivateView">
<h2>Second View</h2>
<asp:TextBox ID="txtSecondView" runat="server"></asp:TextBox>
<asp:Button ID="btnNext2" runat="server" CommandName="NextView" Text="Go to Next" />
<asp:Button ID="btnPrevious2" runat="server" CommandName="PrevView" Text="Go to Previous" />
</asp:View>
<asp:View ID="vwThird" runat="server" OnActivate="ActivateView" OnDeactivate="DeactivateView">
<h2>Third View</h2>
<br />
<asp:Button ID="btnNext3" runat="server" CommandName="NextView" Text="Go to Next" />
<asp:Button ID="btnPrev3" runat="server" CommandName="PrevView" Text="Go to Previous" />
</asp:View>
<asp:View ID="vwLast" runat="server" OnActivate="ActivateView" OnDeactivate="DeactivateView">
<h2>Last View</h2>
<br />
<asp:Button ID="btnPrev4" runat="server" CommandName="PrevView" Text="Go to Previous" />
<asp:Button ID="btnFirst" runat="server" CommandName="SwitchViewByIndex" CommandArgument="0" Text="Go to First" />
</asp:View>
</asp:MultiView>
<br /><br />
First TextBox:
<asp:Label ID="lblFirstTextBox" runat="server"></asp:Label>
<br />
Second TextBox:
<asp:Label ID="lblSecondTextBox" runat="server"></asp:Label>
<br /><br />
<strong>
<span style="text-decoration:underline">
View Activation History:
</span>
</strong>
<br />
<asp:Label ID="lblViewActivation" runat="server"></asp:Label>
</center>
</div>
</form>
</body>
</html>
code behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreRender(object sender, EventArgs e)
{
lblCurrentIndex.Text = MultiView1.ActiveViewIndex.ToString();
}
protected void rdlView_SelectedIndexChanged(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = Convert.ToInt32(rdlView.SelectedValue);
}
protected void ActivateView(object sender, EventArgs e)
{
string str = lblViewActivation.Text;
View v = (View)sender;
str += "View " + v.ID + " activated
";
lblViewActivation.Text = str;
}
protected void DeactivateView(object sender, EventArgs e)
{
string str = lblViewActivation.Text;
View v = (View)sender;
str += "View " + v.ID + " deactivated
";
lblViewActivation.Text = str;
}
protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
{
lblFirstTextBox.Text = txtFirstView.Text;
lblSecondTextBox.Text = txtSecondView.Text;
rdlView.SelectedIndex = MultiView1.ActiveViewIndex + 1;
}
}
comments:
1. in order to save you work, you can create a event group--ActivateView,DeactivateView, these group implement the same code for you.
2. please don't write the lblCurrentIndex.Text in the Page_Load() statement, because the page will be loaded before the label updated.
Just put the lblCurrentIndex.Text code in the Page_PreRender, this is a page event handler.
such as:
protected void Page_PreRender(object sender, EventArgs e)
{
lblCurrentIndex.Text = MultiView1.ActiveViewIndex.ToString();
}
Sunday, November 23, 2008
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment