DetailsView.CurrentMode 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.
Mendapatkan mode DetailsView entri data kontrol saat ini.
public:
property System::Web::UI::WebControls::DetailsViewMode CurrentMode { System::Web::UI::WebControls::DetailsViewMode get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.DetailsViewMode CurrentMode { get; }
[<System.ComponentModel.Browsable(false)>]
member this.CurrentMode : System.Web.UI.WebControls.DetailsViewMode
Public ReadOnly Property CurrentMode As DetailsViewMode
Nilai Properti
Salah DetailsViewMode satu nilai.
- Atribut
Contoh
Contoh kode berikut menunjukkan cara menggunakan CurrentMode properti untuk menentukan apakah DetailsView kontrol dalam mode edit, sisipkan, atau baca-saja. Jika pengguna mencoba menavigasi ke rekaman yang berbeda saat DetailsView kontrol berada dalam mode edit, operasi halaman dibatalkan.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomerDetailView_ItemCommand(Object sender,
DetailsViewCommandEventArgs e)
{
// Clear the error message if the user cancels the edit
// operation.
if (e.CommandName == "Cancel")
{
ErrorMessageLabel.Text = "";
}
}
protected void CustomerDetailView_PageIndexChanging(
object sender, DetailsViewPageEventArgs e)
{
// Cancel the paging operation if the user tries to
// navigate to another record while in edit mode.
if (CustomerDetailView.CurrentMode == DetailsViewMode.Edit)
{
e.Cancel = true;
// Display an error message.
ErrorMessageLabel.Text =
"You cannot navigate to another record while in edit mode.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
DetailsView CurrentMode Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView CurrentMode Example</h3>
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="true"
AutoGenerateEditButton="true"
DataKeyNames="CustomerID"
GridLines="Both"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
runat="server"
OnPageIndexChanging="CustomerDetailView_PageIndexChanging">
<HeaderStyle BackColor="Navy" ForeColor="White" />
</asp:DetailsView>
<br />
<asp:Label ID="ErrorMessageLabel"
ForeColor="Red" runat="server" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</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">
<script runat="server">
Sub CustomerDetailView_ItemCommand(ByVal sender As Object, _
ByVal e As DetailsViewCommandEventArgs)
' Clear the error message if the user cancels the edit
' operation.
If e.CommandName = "Cancel" Then
ErrorMessageLabel.Text = ""
End If
End Sub
Protected Sub CustomerDetailView_PageIndexChanging( _
ByVal sender As Object, ByVal e As DetailsViewPageEventArgs)
' Cancel the paging operation if the user tries to navigate
' to another record while in edit mode.
If CustomerDetailView.CurrentMode = DetailsViewMode.Edit Then
e.Cancel = True
' Display an error message.
ErrorMessageLabel.Text = _
"You cannot navigate to another record while in edit mode."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
DetailsView CurrentMode Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView CurrentMode Example</h3>
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="true"
AutoGenerateEditButton="true"
DataKeyNames="CustomerID"
GridLines="Both"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
runat="server"
OnPageIndexChanging="CustomerDetailView_PageIndexChanging">
<HeaderStyle BackColor="Navy" ForeColor="White" />
</asp:DetailsView>
<br />
<asp:Label ID="ErrorMessageLabel"
ForeColor="Red" runat="server" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the web.config file. -->
<asp:SqlDataSource ID="DetailsViewSource" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
Keterangan
CurrentMode Gunakan properti untuk menentukan apakah DetailsView kontrol dalam mode edit, sisipkan, atau baca-saja. Tabel berikut mencantumkan nilai mode yang berbeda.
| Modus | Deskripsi |
|---|---|
DetailsViewMode.Edit |
Kontrol DetailsView berada dalam mode edit, yang memungkinkan pengguna memperbarui nilai rekaman. |
DetailsViewMode.Insert |
DetailsView Kontrol berada dalam mode sisipkan, yang memungkinkan pengguna untuk menambahkan rekaman baru ke sumber data. |
DetailsViewMode.ReadOnly |
Kontrol DetailsView berada dalam mode baca-saja, yang merupakan mode tampilan normal. |
Nilai ini biasanya diatur secara otomatis oleh DetailsView kontrol saat tombol perintah Baru, Perbarui, Sisipkan, Hapus, atau Batal diklik. Anda juga dapat mengubah mode secara terprogram dengan menggunakan ChangeMode metode .
DetailsView Saat kontrol berubah mode, peristiwa dalam tabel berikut dinaikkan. Ini memungkinkan Anda membuat penanganan aktivitas kustom yang melakukan rutinitas saat peristiwa terjadi.
| Event | Deskripsi |
|---|---|
| ModeChanged | Terjadi ketika tombol perintah yang mengubah mode DetailsView kontrol diklik, tetapi setelah mode berubah. |
| ModeChanging | Terjadi ketika tombol perintah yang mengubah mode DetailsView kontrol diklik, tetapi sebelum mode berubah. |