GridView控件編程的事件

 

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>

 

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>

當處於更新狀態沒更新就導航到其他分頁時,顯示紅字


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>

在點擊取消按鈕後顯示紅字

 

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>

 

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>

 

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>

 

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,區別在於事件發生時間的前後關係.下面事件意思跟上面的一樣,沒必要列出來了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章