MultiView.ActiveViewIndex Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
public:
virtual property int ActiveViewIndex { int get(); void set(int value); };
public virtual int ActiveViewIndex { get; set; }
member this.ActiveViewIndex : int with get, set
Public Overridable Property ActiveViewIndex As Integer
Nilai Properti
Indeks berbasis nol dari kontrol aktif View dalam MultiView kontrol. Defaultnya adalah -1, menunjukkan bahwa tidak ada tampilan yang diatur sebagai aktif.
Pengecualian
Indeks yang ditentukan diatur ke kurang dari -1, atau lebih besar dari atau sama dengan jumlah item dalam daftar.
Contoh
Contoh kode berikut menunjukkan cara menggunakan MultiView kontrol untuk membuat survei dasar. Setiap View kontrol adalah pertanyaan survei terpisah. Saat pengguna mengklik tombol Sebelumnya di halaman mana pun, ActiveViewIndex properti dikurangi untuk menavigasi ke kontrol sebelumnya View . Saat pengguna mengklik tombol Berikutnya di halaman mana pun, ActiveViewIndex properti akan dinaikkan untuk menavigasi ke kontrol berikutnya View .
Catatan
Sampel kode berikut menggunakan model kode file tunggal dan mungkin tidak berfungsi dengan benar jika disalin langsung ke file code-behind. Sampel kode ini harus disalin ke dalam file teks kosong yang memiliki ekstensi .aspx. Untuk informasi selengkapnya tentang model kode Formulir Web, lihat ASP.NET Model Kode Halaman Formulir Web.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>MultiView ActiveViewIndex Example</title>
<script runat="server">
protected void NextButton_Command(object sender, EventArgs e)
{
// Determine which button was clicked
// and set the ActiveViewIndex property to
// the view selected by the user.
if (DevPollMultiView.ActiveViewIndex > -1 & DevPollMultiView.ActiveViewIndex < 3)
{
// Increment the ActiveViewIndex property
// by one to advance to the next view.
DevPollMultiView.ActiveViewIndex += 1;
}
else if (DevPollMultiView.ActiveViewIndex == 3)
{
// This is the final view.
// The user wants to save the survey results.
// Insert code here to save survey results.
// Disable the navigation buttons.
Page4Save.Enabled = false;
Page4Restart.Enabled = false;
}
else
{
throw new Exception("An error occurred.");
}
}
protected void BackButton_Command(object sender, EventArgs e)
{
if (DevPollMultiView.ActiveViewIndex > 0 & DevPollMultiView.ActiveViewIndex <= 2)
{
// Decrement the ActiveViewIndex property
// by one to return to the previous view.
DevPollMultiView.ActiveViewIndex -= 1;
}
else if (DevPollMultiView.ActiveViewIndex == 3)
{
// This is the final view.
// The user wants to restart the survey.
// Return to the first view.
DevPollMultiView.ActiveViewIndex = 0;
}
else
{
throw new Exception("An error occurred.");
}
}
</script>
</head>
<body>
<form id="Form1" runat="Server">
<h3>MultiView ActiveViewIndex Example</h3>
<asp:Panel id="Page1ViewPanel"
Width="330px"
Height="150px"
HorizontalAlign="Left"
Font-size="12"
BackColor="#C0C0FF"
BorderColor="#404040"
BorderStyle="Double"
runat="Server">
<asp:MultiView id="DevPollMultiView"
ActiveViewIndex="0"
runat="Server">
<asp:View id="Page1"
runat="Server">
<asp:Label id="Page1Label"
Font-bold="true"
Text="What kind of applications do you develop?"
runat="Server"
AssociatedControlID="Page1">
</asp:Label><br /><br />
<asp:RadioButton id="Page1Radio1"
Text="Web Applications"
Checked="False"
GroupName="RadioGroup1"
runat="server" >
</asp:RadioButton><br />
<asp:RadioButton id="Page1Radio2"
Text="Windows Forms Applications"
Checked="False"
GroupName="RadioGroup1"
runat="server" >
</asp:RadioButton><br /><br /><br />
<asp:Button id="Page1Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat= "Server">
</asp:Button>
</asp:View>
<asp:View id="Page2"
runat="Server">
<asp:Label id="Page2Label"
Font-bold="true"
Text="How long have you been a developer?"
runat="Server"
AssociatedControlID="Page2">
</asp:Label><br /><br />
<asp:RadioButton id="Page2Radio1"
Text="Less than five years"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br />
<asp:RadioButton id="Page2Radio2"
Text="More than five years"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br /><br /><br />
<asp:Button id="Page2Back"
Text = "Previous"
OnClick="BackButton_Command"
Height="25"
Width="70"
runat= "Server">
</asp:Button>
<asp:Button id="Page2Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button>
</asp:View>
<asp:View id="Page3"
runat="Server">
<asp:Label id="Page3Label1"
Font-bold="true"
Text= "What is your primary programming language?"
runat="Server"
AssociatedControlID="Page3">
</asp:Label><br /><br />
<asp:RadioButton id="Page3Radio1"
Text="Visual Basic .NET"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br />
<asp:RadioButton id="Page3Radio2"
Text="C#"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br />
<asp:RadioButton id="Page3Radio3"
Text="C++"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br /><br />
<asp:Button id="Page3Back"
Text = "Previous"
OnClick="BackButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button>
<asp:Button id="Page3Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button><br />
</asp:View>
<asp:View id="Page4"
runat="Server">
<asp:Label id="Label1"
Font-bold="true"
Text = "Thank you for taking the survey."
runat="Server"
AssociatedControlID="Page4">
</asp:Label>
<br /><br /><br /><br /><br /><br />
<asp:Button id="Page4Save"
Text = "Save Responses"
OnClick="NextButton_Command"
Height="25"
Width="110"
runat="Server">
</asp:Button>
<asp:Button id="Page4Restart"
Text = "Retake Survey"
OnClick="BackButton_Command"
Height="25"
Width="110"
runat= "Server">
</asp:Button>
</asp:View>
</asp:MultiView>
</asp:Panel>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>MultiView ActiveViewIndex Example</title>
<script runat="server">
Sub NextButton_Command(sender As Object, e As System.EventArgs)
' Determine which button was clicked
' and set the ActiveViewIndex property to
' the view selected by the user.
If (DevPollMultiView.ActiveViewIndex > -1) AND DevPollMultiView.ActiveViewIndex < 3 Then
' Increment the ActiveViewIndex property
' by one to advance to the next view.
DevPollMultiView.ActiveViewIndex += 1
ElseIf DevPollMultiView.ActiveViewIndex = 3 Then
' This is the final view.
' The user wants to save the survey results.
' Insert code here to save survey results.
' Disable the navigation buttons.
Page4Save.Enabled = False
Page4Restart.Enabled = False
Else
Throw New Exception("An error occurred.")
End If
End Sub
Sub BackButton_Command(ByVal sender As Object, ByVal e As System.EventArgs)
If (DevPollMultiView.ActiveViewIndex > 0) And DevPollMultiView.ActiveViewIndex <= 2 Then
' Decrement the ActiveViewIndex property
' by one to return to the previous view.
DevPollMultiView.ActiveViewIndex -= 1
ElseIf DevPollMultiView.ActiveViewIndex = 3 Then
' This is the final view.
' The user wants to restart the survey.
' Return to the first view.
DevPollMultiView.ActiveViewIndex = 0
Else
Throw New Exception("An error occurred.")
End If
End Sub
</script>
</head>
<body>
<form id="Form1" runat="Server">
<h3>MultiView ActiveViewIndex Example</h3>
<asp:Panel id="Page1ViewPanel"
Width="330px"
Height="150px"
HorizontalAlign ="Left"
Font-size="12"
BackColor="#C0C0FF"
BorderColor="#404040"
BorderStyle="Double"
runat="Server">
<asp:MultiView id="DevPollMultiView"
ActiveViewIndex="0"
runat="Server">
<asp:View id="Page1"
runat="Server">
<asp:Label id="Page1Label"
Font-bold="true"
Text="What kind of applications do you develop?"
runat="Server"
AssociatedControlID="Page1">
</asp:Label><br /><br />
<asp:RadioButton id="Page1Radio1"
Text="Web Applications"
Checked="False"
GroupName="RadioGroup1"
runat="server" >
</asp:RadioButton><br />
<asp:RadioButton id="Page1Radio2"
Text="Windows Forms Applications"
Checked="False"
GroupName="RadioGroup1"
runat="server" >
</asp:RadioButton><br /><br /><br />
<asp:Button id="Page1Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat= "Server">
</asp:Button>
</asp:View>
<asp:View id="Page2"
runat="Server">
<asp:Label id="Page2Label"
Font-bold="true"
Text="How long have you been a developer?"
runat="Server"
AssociatedControlID="Page2">
</asp:Label><br /><br />
<asp:RadioButton id="Page2Radio1"
Text="Less than five years"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br />
<asp:RadioButton id="Page2Radio2"
Text="More than five years"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br /><br /><br />
<asp:Button id="Page2Back"
Text = "Previous"
OnClick="BackButton_Command"
Height="25"
Width="70"
runat= "Server">
</asp:Button>
<asp:Button id="Page2Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button>
</asp:View>
<asp:View id="Page3"
runat="Server">
<asp:Label id="Page3Label1"
Font-bold="true"
Text= "What is your primary programming language?"
runat="Server"
AssociatedControlID="Page3">
</asp:Label><br /><br />
<asp:RadioButton id="Page3Radio1"
Text="Visual Basic .NET"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br />
<asp:RadioButton id="Page3Radio2"
Text="C#"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br />
<asp:RadioButton id="Page3Radio3"
Text="C++"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br /><br />
<asp:Button id="Page3Back"
Text = "Previous"
OnClick="BackButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button>
<asp:Button id="Page3Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button><br />
</asp:View>
<asp:View id="Page4"
runat="Server">
<asp:Label id="Label1"
Font-bold="true"
Text = "Thank you for taking the survey."
runat="Server"
AssociatedControlID="Page4">
</asp:Label>
<br /><br /><br /><br /><br /><br />
<asp:Button id="Page4Save"
Text = "Save Responses"
OnClick="NextButton_Command"
Height="25"
Width="110"
runat="Server">
</asp:Button>
<asp:Button id="Page4Restart"
Text = "Retake Survey"
OnClick="BackButton_Command"
Height="25"
Width="110"
runat= "Server">
</asp:Button>
</asp:View>
</asp:MultiView>
</asp:Panel>
</form>
</body>
</html>
Keterangan
ActiveViewIndex Gunakan properti untuk mengatur View kontrol dalam MultiView kontrol sebagai tampilan aktif. Anda juga dapat menggunakan properti ini untuk mengembalikan indeks View kontrol yang saat ini ditetapkan sebagai tampilan aktif. Indeks View kontrol ditentukan oleh urutan di mana ia dideklarasikan dalam MultiView kontrol. Misalnya, kontrol pertama View yang MultiView dinyatakan dalam kontrol memiliki indeks 0.
Hanya satu View kontrol pada satu waktu yang dapat didefinisikan sebagai aktif dalam MultiView kontrol. View Kontrol yang diatur ke ActiveViewIndex properti akan dirender ke klien. ActiveViewIndex Jika properti diatur ke View yang tidak ada dalam MultiView kontrol, dinaikkan ArgumentOutOfRangeException pada durasi. Jika properti kosong, MultiView kontrol tidak merender konten apa pun ke klien.
Anda dapat mengatur ActiveViewIndex properti baik secara deklaratif pada waktu pengembangan atau secara terprogram pada durasi. ActiveViewIndex Mengatur properti secara deklaratif saat Anda menentukan MultiView kontrol menyebabkan View kontrol yang diatur sebagai tampilan aktif untuk dirender ke klien saat pertama kali MultiView kontrol dipanggil. ActiveViewIndex Mengatur properti secara terprogram memungkinkan aplikasi menentukan kontrol mana yang View akan dirender ke klien pada durasi berdasarkan kriteria seperti identitas pengguna atau preferensi mereka.
Secara bergantian, Anda dapat menggunakan SetActiveView metode dan GetActiveView untuk mengatur atau mendapatkan tampilan aktif dalam MultiView kontrol.