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.
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