Obtain a row in the RowCommand event of ASP. NET 2.0 GridView

Source: Internet
Author: User

The GRIDVIEW control in ASP. NET2.0 is a new control. In the GRIDVIEW, the row index is placed in the CommandArgument, instead of using this. MyDataGrid. DataKeys [e. Item. ItemIndex]. ToString () to conveniently retrieve the primary key value like the DataGrid.
 
The following is a GridViewSample Code:
        <Asp: GridViewID = "grdFileList"Runat = "server"
        CssClass = "listingHighlightTable"GridLines = "None"Width = "98%"DataKeyNames = "fileFullname"
        AutoGenerateColumns = "False"Onrowcommand = "grdFileList_RowCommand">
            <Columns>
                <Asp: HyperLinkFieldDataNavigateUrlFields = "fileURLPath"
                    DataNavigateUrlFormatString = "{0 }"DataTextField = "filename"HeaderText = "file name"/>
                <Asp: BoundFieldDataField = "filesize"HeaderText = "file size (KB )"/>
                <Asp: BoundFieldDataField = "createdate"HeaderText="Upload date"/>
                <Asp: TemplateFieldHeaderText = "operation">
                    <ItemTemplate>
                        <Asp: LinkButtonID = "lnkDelete"Runat = "server"CausesValidation = "false"
                            CommandName = "DeleteFile"Text = "delete"OnClientClick = "returnConfirm ('Are you sure you want to delete it? '); "> </Asp: LinkButton>
                    </ItemTemplate>
                </Asp: TemplateField>
                <Asp: ButtonFieldCommandName = "ButtonFile"Text = "Button"/>
            </Columns>
    </Asp: GridView>
 
The following code uses the default CommandField:
<Asp: CommandFieldShowDeleteButton = "True"ShowEditButton = "True"/>
Or ButtonField, the Code is as follows:
<Asp: ButtonFieldCommandName = "ButtonFile"Text = "Button"/>   
 
You can use the following code in RowCommand to retrieve the row number:
If(E. CommandName="ButtonFile ")
{
     IntIndex=Convert. ToInt32 (e. CommandArgument );// Obtain the row Index
}
 
However, CommandField/ButtonFieldIn some cases, the operation button may not meet the requirements. We can use the template column to customize the operation button. The Code is as follows:
<Asp: TemplateFieldHeaderText = "operation">
    <ItemTemplate>
                        <Asp: LinkButtonID = "lnkDelete"Runat = "server"CausesValidation = "false"
                            CommandName = "DeleteFile"Text = "delete"OnClientClick = "returnConfirm ('Are you sure you want to delete it? '); "> </Asp: LinkButton>
    </ItemTemplate>
</Asp: TemplateField>
 
NowIn Convert. ToInt32 (e. CommandArgument), e. CommandArgument is converted to an empty string. Row indexes are not correctly obtained.RowIndex.
 
There are two solutions to the above problems:
Method 1: Obtain the LinkButtonClick GridViewRow, and then obtain RowIndex. The sample code is as follows:
If(E. CommandName="DeleteFile ")
{
   //CatchingTheRowInWhichTheLinkButtonIsClicked.
   GridViewRowGvrow=(GridViewRow) (LinkButton) e. CommandSource). NamingContainer );
   //Get row IndexRowIndex
   IntIndex=Gvrow. RowIndex;
}
 
Method 2: You can use the RowCreated event to write a CommandArgument event to the LinkButton in the template column.
RenderingGridViewYou must createGridViewRowObject. In the createGridViewWhen each row in the control isRowCreatedEvent. This allows us to provide an event processing method, that is, to execute a custom routine every time this event occurs (such as adding custom content to the row, of course, you can also add e. the CommandArgument attribute is the LinkButton in the template column ).
 
The following code example demonstrates how to useRowCreatedThe event stores the index of the row being created inLinkButtonControlCommandArgumentAttribute. This allows you to clickLinkButtonThe control button contains the row index of the control.
ProtectedVoidGridView1_RowCreated (objectSender,GridViewRowEventArgsE)
   {
        If(E. Row. RowType=DataControlRowType. DataRow)
        {
            //RetrieveTheLinkButtonControlFromTheFirstColumn.
            LinkButtonLinkButton1=(LinkButton) e. Row. FindControl ("LinkButton1 ");
           //SetTheLinkButton'sCommandArgumentPropertyWithTheRow'sIndex.
            LinkButton1.CommandArgument=E. Row. RowIndex. ToString ();
        }
    }
 
Then, in the RowCommandThe preceding Method e. CommandArgument is used in the event.To obtain RowIndex. The sample code is as follows:
ProtectedVoidGridView1_RowCommand (objectSender,GridViewCommandEventArgsE)
{
IntRowIndex=Int. Parse (e. CommandArgument. ToString ());
}
 
References:
1. GettingRowIndexInRowcommandEvent
Http://www.dotnetspider.com/forum/159821-Getting-Row-index-Rowcommand-event.aspx
2.How to locate the index of the current row in the RowCommand event when using the LinkButton in TemplateField in the gridview control in ASP. NET2.0
Http://www.cnblogs.com/xh3/archive/2006/07/01/440469.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.