GridViewRowEventHandler Delegasikan
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.
Mewakili metode yang menangani RowCreated peristiwa GridView dan RowDataBound kontrol.
public delegate void GridViewRowEventHandler(System::Object ^ sender, GridViewRowEventArgs ^ e);
public delegate void GridViewRowEventHandler(object sender, GridViewRowEventArgs e);
type GridViewRowEventHandler = delegate of obj * GridViewRowEventArgs -> unit
Public Delegate Sub GridViewRowEventHandler(sender As Object, e As GridViewRowEventArgs)
Parameter
- sender
- Object
Sumber dari peristiwa tersebut.
Objek GridViewRowEventArgs yang berisi data peristiwa.
Contoh
Contoh berikut menunjukkan cara menambahkan GridViewRowEventHandler delegasi secara terprogram ke RowDataBound peristiwa GridView kontrol.
<%@ 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 Page_Load(Object sender, EventArgs e)
{
// Create a new GridView control.
GridView customersGridView = new GridView();
// Set the GridView control's properties.
customersGridView.ID = "CustomersGridView";
customersGridView.DataSourceID = "CustomersSqlDataSource";
customersGridView.AutoGenerateColumns = true;
customersGridView.AllowPaging = true;
// Programmatically register the event-handling method.
customersGridView.RowDataBound += new GridViewRowEventHandler(this.CustomersGridView_RowDataBound);
// Add the GridView control to the Controls collection
// of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView);
}
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewRowEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewRowEventHandler Example</h3>
<asp:placeholder id="GridViewPlaceHolder"
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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Create a new GridView control.
Dim customersGridView As New GridView()
' Set the GridView control's properties.
customersGridView.ID = "CustomersGridView"
customersGridView.DataSourceID = "CustomersSqlDataSource"
customersGridView.AutoGenerateColumns = True
customersGridView.AllowPaging = True
' Programmatically register the event-handling method.
AddHandler customersGridView.RowDataBound, AddressOf CustomersGridView_RowDataBound
' Add the GridView control to the Controls collection
' of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(customersGridView)
End Sub
Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the company name in italics.
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewRowEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridViewRowEventHandler Example</h3>
<asp:placeholder id="GridViewPlaceHolder"
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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
Contoh berikut menunjukkan cara menambahkan GridViewRowEventHandler delegasi secara deklaratif ke RowDataBound peristiwa GridView kontrol.
<%@ 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 CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowDataBound Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
<!-- 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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</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 CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Display the company name in italics.
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView RowDataBound Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView RowDataBound Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
<!-- 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="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
Keterangan
GridView Sebelum kontrol dapat dirender, GridViewRow objek harus dibuat untuk setiap baris dalam kontrol. Peristiwa RowCreated dinaikkan setiap kali baris dalam GridView kontrol dibuat. Ini memungkinkan Anda untuk menyediakan metode penanganan peristiwa yang melakukan rutinitas kustom, seperti menambahkan konten kustom ke baris, setiap kali peristiwa ini terjadi.
Demikian pula, setiap baris dalam kontrol harus terikat ke rekaman di sumber data sebelum GridView kontrol dapat dirender. Peristiwa RowDataBound dimunculkan saat baris data (diwakili oleh GridViewRow objek) terikat ke GridView data dalam kontrol. Ini memungkinkan Anda untuk menyediakan metode penanganan peristiwa yang melakukan rutinitas kustom, seperti memodifikasi nilai data yang terikat ke baris, setiap kali peristiwa ini terjadi.
Saat membuat GridViewRowEventHandler delegasi, Anda mengidentifikasi metode yang akan menangani peristiwa. Untuk mengaitkan peristiwa dengan penanganan aktivitas Anda, tambahkan instans delegasi ke peristiwa. Penanganan aktivitas dipanggil setiap kali peristiwa terjadi, kecuali Jika Anda menghapus delegasi. Untuk informasi selengkapnya tentang delegasi penanganan aktivitas, lihat Menangani dan Meningkatkan Peristiwa.
Metode Ekstensi
GetMethodInfo(Delegate) |
Mendapatkan objek yang mewakili metode yang diwakili oleh delegasi yang ditentukan. |