GridView 控件编程的事件

我把MSDN的例子全部提出来一个一个看.这样容易更好理解

PageIndexChanged
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之后发生。此事件通常用于以下情形:在用户定位到该控件中的另一页之后,您需要执行某项任务。

PageIndexChanging
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之前发生。此事件通常用于取消分页操作。

RowCancelingEdit
在单击某一行的“取消”按钮时,但在 GridView 控件退出编辑模式之前发生。此事件通常用于停止取消操作。

RowCommand
当单击 GridView 控件中的按钮时发生。此事件通常用于在控件中单击按钮时执行某项任务。

RowCreated
当在 GridView 控件中创建新行时发生。此事件通常用于在创建行时修改行的内容。

RowDataBound
在 GridView 控件中将数据行绑定到数据时发生。此事件通常用于在行绑定到数据时修改行的内容。

RowDeleted
在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之后发生。此事件通常用于检查删除操作的结果。

RowDeleting
在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之前发生。此事件通常用于取消删除操作。

RowEditing
发生在单击某一行的“编辑”按钮以后,GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。

RowUpdated
发生在单击某一行的“更新”按钮,并且 GridView 控件对该行进行更新之后。此事件通常用于检查更新操作的结果。

RowUpdating
发生在单击某一行的“更新”按钮以后,GridView 控件对该行进行更新之前。此事件通常用于取消更新操作。

SelectedIndexChanged
发生在单击某一行的“选择”按钮,GridView 控件对相应的选择操作进行处理之后。此事件通常用于在该控件中选定某行之后执行某项任务。

SelectedIndexChanging
发生在单击某一行的“选择”按钮以后,GridView 控件对相应的选择操作进行处理之前。此事件通常用于取消选择操作。

Sorted
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之后发生。此事件通常用于在用户单击用于列排序的超链接之后执行某个任务。

Sorting
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之前发生。此事件通常用于取消排序操作或执行自定义的排序例程。

1.PageIndexChanged 事件

下面的代码示例演示如何使用 PageIndexChanged 事件显示用户从页导航行中选择的页码
<%@ Page language="C#" %>
<script runat="server">
 
 void CustomersGridView_DataBound(Object sender, EventArgs e)
 {
 if (!IsPostBack)
 {
 DisplayCurrentPage();
 }
 }

 void CustomersGridView_PageIndexChanged(Object sender, EventArgs e)
 {
 DisplayCurrentPage();
 }

 void DisplayCurrentPage()
 {
 int currentPage = CustomersGridView.PageIndex + 1;
 Message.Text = "Page " + currentPage.ToString() + " of " +
 CustomersGridView.PageCount.ToString() + ".";
 }
</script>
<html>
 <body>
 <form id="Form1" runat="server">
 <h3>GridView PageIndexChanged Example</h3>
 
 <asp:label id="Message"
 forecolor="Red"
 runat="server"/>
 
 <br/>

 <asp:gridview id="CustomersGridView"
 datasourceid="CustomersSource"
 autogeneratecolumns="true"
 emptydatatext="No data available."
 allowpaging="true"
 ondatabound="CustomersGridView_DataBound"
 onpageindexchanged="CustomersGridView_PageIndexChanged"
 runat="server">
 
 <pagersettings mode="Numeric"
 position="Bottom"
 pagebuttoncount="10"/>
 
 <pagerstyle backcolor="LightBlue"/>
 
 </asp:gridview>
 <asp:sqldatasource id="CustomersSource"
 selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
 connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
 runat="server"/>
 
 </form>
 </body>
</html>

uploads/200605/21_213700_snap1.gif


2,PageIndexChanging 事件

下面的代码示例演示如果用户在 GridView 控件处于编辑模式时尝试导航到另一个页面,此时如何使用 PageIndexChanging 事件取消分页操作
<%@ Page language="C#" %>

<script runat="server">

 void CustomersGridView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
 {
 
 // Cancel the paging operation if the user attempts to navigate
 // to another page while the GridView control is in edit mode.
 if (CustomersGridView.EditIndex != -1)
 {
 // Use the Cancel property to cancel the paging operation.
 e.Cancel = true;
 
 // Display an error message.
 int newPageNumber = e.NewPageIndex + 1;
 Message.Text = "Please update the record before moving to page " +
 newPageNumber.ToString() + ".";
 }
 else
 {
 // Clear the error message.
 Message.Text = "";
 }
 
 }

 void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
 {
 // Clear the error message.
 Message.Text = "";
 }

</script>

<html>
 <body>
 <form id="Form1" runat="server">
 
 <h3>GridView PageIndexChanging Example</h3>
 
 <asp:label id="Message"
 forecolor="Red"
 runat="server"/>
 
 <br/>

 <asp:gridview id="CustomersGridView"
 datasourceid="CustomersSource"
 autogeneratecolumns="true"
 emptydatatext="No data available."
 allowpaging="true"
 autogenerateeditbutton="true"
 datakeynames="CustomerID"
 onpageindexchanging="CustomersGridView_PageIndexChanging"
 onrowcancelingedit="CustomersGridView_RowCancelingEdit"
 runat="server">
 
 <pagersettings mode="Numeric"
 position="Bottom"
 pagebuttoncount="10"/>
 
 <pagerstyle backcolor="LightBlue"/>
 
 </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="CustomersSource"
 selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
 updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
 connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
 runat="server"/>
 
 </form>
 </body>
</html>

当处于更新状态没更新就导航到其他分页时,显示红字
uploads/200605/21_214047_snap2.gif


3,RowCancelingEdit 事件 单击编辑模式中某一行的“取消”按钮以后,在该行退出编辑模式之前发生

下面的代码示例演示当用户取消 GridView 控件的更新操作时,如何使用 RowCancelingEdit 事件显示取消消息。
<%@ Page language="C#" %>

<script runat="server">

 void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
 {
 
 GridViewRow row = CustomersGridView.Rows[e.RowIndex];
 Message.Text = "Update for item " + row.Cells[1].Text + " Canceled.";
 
 }

</script>

<html>
 <body>
 <form id="Form1" runat="server">
 
 <h3>GridView RowCancelingEdit Example</h3>
 
 <asp:label id="Message"
 forecolor="Red"
 runat="server"/>
 
 <br/>
 <asp:gridview id="CustomersGridView"
 datasourceid="CustomersSqlDataSource"
 autogeneratecolumns="true"
 autogenerateeditbutton="true"
 allowpaging="true"
 datakeynames="CustomerID"
 onrowcancelingedit="CustomersGridView_RowCancelingEdit"
 runat="server">
 </asp:gridview>
 <asp:sqldatasource id="CustomersSqlDataSource"
 selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
 updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
 connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
 runat="server">
 </asp:sqldatasource>
 
 </form>
 </body>
</html>

在点击取消按钮后显示红字

uploads/200605/21_214616_snap3.gif


4,RowCommand 事件 单击 GridView 控件中的某个按钮时,会引发 RowCommand 事件

下面的示例演示如何使用传递到事件处理方法的 GridViewCommandEventArgs 对象确定引发事件的按钮的命令名
<%@ Page language="C#" %>

<script runat="server">

 void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
 {
 // If multiple buttons are used in a GridView control, use the
 // CommandName property to determine which button was clicked.
 if(e.CommandName=="Add")
 {
 // Convert the row index stored in the CommandArgument
 // property to an Integer.
 int index = Convert.ToInt32(e.CommandArgument);
 
 // Retrieve the row that contains the button clicked
 // by the user from the Rows collection.
 GridViewRow row = CustomersGridView.Rows[index];
 
 // Create a new ListItem object for the customer in the row.
 ListItem item = new ListItem();
 item.Text = Server.HtmlDecode(row.Cells[2].Text);
 
 // If the customer is not already in the ListBox, add the ListItem
 // object to the Items collection of the ListBox control.
 if (!CustomersListBox.Items.Contains(item))
 {
 CustomersListBox.Items.Add(item);
 }
 }
 }

 void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
 {
 
 // The GridViewCommandEventArgs class does not contain a
 // property that indicates which row's command button was
 // clicked. To identify which row's button was clicked, use
 // the button's CommandArgument property by setting it to the
 // row's index.
 if(e.Row.RowType == DataControlRowType.DataRow)
 {
 // Retrieve the LinkButton control from the first column.
 LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
 
 // Set the LinkButton's CommandArgument property with the
 // row's index.
 addButton.CommandArgument = e.Row.RowIndex.ToString();
 }

 }
 
</script>

<html>
 <body>
 <form id="Form1" runat="server">
 
 <h3>GridView RowCommand Example</h3>
 
 <table width="100%">
 <tr>
 <td width="50%">
 
 <asp:gridview id="CustomersGridView"
 datasourceid="CustomersSource"
 allowpaging="true"
 autogeneratecolumns="false"
 onrowcommand="CustomersGridView_RowCommand"
 onrowcreated="CustomersGridView_RowCreated"
 runat="server">
 
 <columns>
 <asp:buttonfield buttontype="Link"
 commandname="Add"
 text="Add"/>
 <asp:boundfield datafield="CustomerID"
 headertext="Customer ID"/>
 <asp:boundfield datafield="CompanyName"
 headertext="Company Name"/>
 <asp:boundfield datafield="City"
 headertext="City"/>
 </columns>
 
 </asp:gridview>
 
 </td>
 
 <td valign="top" width="50%">
 
 Customers: <br/>
 <asp:listbox id="CustomersListBox"
 runat="server"/>
 
 </td>
 </tr>
 </table>
 
 <!-- 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="CustomersSource"
 selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
 connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
 runat="server"/>
 
 </form>
 </body>
</html>

uploads/200605/21_215745_snap4.gif


5,RowCreated 事件同上

6,RowDataBound 事件 在 GridView 控件中将数据行绑定到数据时发生

下面的代码示例演示如何使用 RowDataBound 事件在数据源中的字段值显示在 GridView 控件中之前修改该值
<%@ Page language="C#" %>

<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>
 <body>
 <form 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>

uploads/200605/22_095727_snap1.gif


7,RowDeleted 事件 在单击某一行的“删除”按钮时,但在 GridView 控件删除该行之后发生。

下面的代码示例演示如何使用 RowDeleted 事件检查删除操作的结果。会显示一条消息向用户指示操作是否成功。
<%@ Page language="C#" %>

<script runat="server">

 void CustomersGridView_RowDeleted(Object sender, GridViewDeletedEventArgs e)
 {
 
 // Display whether the delete operation succeeded.
 if(e.Exception == null)
 {
 Message.Text = "Row deleted successfully.";
 }
 else
 {
 Message.Text = "An error occurred while attempting to delete the row.";
 e.ExceptionHandled = true;
 }
 
 }
 
</script>

<html>
 <body>
 <form id="Form1" runat="server">
 
 <h3>GridView RowDeleted Example</h3>
 
 <asp:label id="Message"
 forecolor="Red"
 runat="server"/>
 
 <br/>
 
 <asp:gridview id="CustomersGridView"
 datasourceid="CustomersSqlDataSource"
 autogeneratecolumns="true"
 autogeneratedeletebutton="true"
 datakeynames="CustomerID"
 onrowdeleted="CustomersGridView_RowDeleted"
 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]"
 deletecommand="Delete from Customers where CustomerID = @CustomerID"
 connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
 runat="server">
 </asp:sqldatasource>
 
 </form>
 </body>
</html>

uploads/200605/22_100051_snap2.gif


8,RowDeleting 事件 在单击某一行的“删除”按钮时,但在 GridView 控件删除该行之前发生。

下面的代码示例演示当用户尝试从 GridView 控件中移除最后一条记录时,如何使用 RowDeleting 事件取消删除操作。
<%@ Page language="C#" %>

<script runat="server">

 void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
 {
 
 // Cancel the delete operation if the user attempts to remove
 // the last record from the GridView control.
 if (CustomersGridView.Rows.Count <= 1)
 {
 
 e.Cancel = true;
 Message.Text = "You must keep at least one record.";
 
 }
 
 }

</script>

<html>
 <body>
 <form runat="server">
 
 <h3>GridView RowDeleting Example</h3>
 
 <asp:label id="Message"
 forecolor="Red"
 runat="server"/>
 
 <br/>
 
 <!-- The GridView control automatically sets the columns -->
 <!-- specified in the datakeynames property as read-only. -->
 <!-- No input controls are rendered for these columns in -->
 <!-- edit mode. -->
 <asp:gridview id="CustomersGridView"
 datasourceid="CustomersSqlDataSource"
 autogeneratecolumns="true"
 autogeneratedeletebutton="true"
 datakeynames="CustomerID"
 onrowdeleting="CustomersGridView_RowDeleting"
 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]"
 deletecommand="Delete from Customers where CustomerID = @CustomerID"
 connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
 runat="server">
 </asp:sqldatasource>
 
 </form>
 </body>
</html>

以下还有六个事件,我们可以看到事件有一个特点,后缀名为ed或者ing,区别在于事件发生时间的前后关系.下面事件意思跟上面的一样,没必要列出来了
posted @ 2006-06-16 13:32  Nina  阅读(7954)  评论(3编辑  收藏  举报