Blog Reader RSS LoveCherry 技术无极限 GEO MVP MS Project开源技术

Gridview之RowCommand,ButtonField[add buttons and use the RowCommand event to add custom functionality to the control]

先查阅了MSDN官方网站关于GridView的相关ButtonField,RowCommand的内容。
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
先摘抄些关键的东西:
Occurs when a button is clicked in a GridView control.
Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.
Buttons within a GridView control can also invoke some of the built-in functionality of the control. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.

"Cancel"

Cancels an edit operation and returns the GridView control to read-only mode. Raises the RowCancelingEdit event.

"Delete"

Deletes the current record. Raises the RowDeleting and RowDeleted events.

"Edit"

Puts the current record in edit mode. Raises the RowEditing event.

"Page"

Performs a paging operation. Sets the CommandArgument property of the button to "First", "Last", "Next", "Prev", or a page number to specify the type of paging operation to perform. Raises the PageIndexChanging and PageIndexChanged events.

"Select"

Selects the current record. Raises the SelectedIndexChanging and SelectedIndexChanged events.

"Sort"

Sorts the GridView control. Raises the Sorting and Sorted events.

"Update"

Updates the current record in the data source. Raises the RowUpdating and RowUpdated events.


之后我又查阅了MSDN walkthrough
http://msdn2.microsoft.com/en-us/library/bb907626.aspx
You can use the CommandName property of the event argument to identify the button's function in the event handler method.

protected void GridView1_RowCommand(object sender, 
  GridViewCommandEventArgs e)
{
  
if (e.CommandName == "AddToCart")
  
{
    
// Retrieve the row index stored in the 
    
// CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    
// Retrieve the row that contains the button 
    
// from the Rows collection.
    GridViewRow row = GridView1.Rows[index];

    
// Add code here to add the item to the shopping cart.
  }


  }


其它相关参考网址:
http://msdn2.microsoft.com/en-us/library/bb498196.aspx
posted @ 2008-04-08 11:53  大宋提刑官  阅读(533)  评论(0编辑  收藏  举报