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

Source: Internet
Author: User
ASP. the gridview control in net2.0 is really strange. I don't know how Ms considers it. In the gridview, the row index is placed in the commandargument, instead of using this as the DataGrid. mydatagrid. datakeys [E. item. itemindex]. tostring () to conveniently retrieve the primary key value,
We also noticed that if the default commandfield is used,
<Asp: commandfield showdeletebutton = "true" showeditbutton = "true"/>
You can use the following in rowcommand: Code Retrieve the row number:
Protected void gridview1_rowcommand (Object sender, gridviewcommandeventargs E)
{
// Edit the button
If (E. commandname = "edit ")
{
// Set the highlighted editing line
This. gridview1.editrowstyle. backcolor = color. fromname ("# f7ce90 ");
// String Index = This. gridview1.datakeys [convert. toint32 (E. commandargument)]. value. tostring ();
Int Index = convert. toint32 (E. commandargument );
Gridviewrow ROW = gridview1.rows [Index];
String xh3 = row. cells [3]. text;
}
}

 

however, the problem is that commandfield is very manageable. We are generally used to customizing operation buttons using template columns, as shown below:










Then, the problem arises. Run the following error: the format of the input string is incorrect. In convert. toint32 (E. commandargument), E. commandargument is converted to a string that is null. When we convert commandfield to a template column, the default commandargument attribute is lost !!!
After two days of thinking, I read the online materials and finally found in the msdn document that before rendering the gridview control, I must first create a gridviewrow object for each row in the control. When you create each row in the gridview control, the rowcreated event is triggered. 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 gridviewroweventargs object will be passed to the event processing method, so that we can access the attributes of the row being created. To access a specific cell in the row, you can use the cells attribute of the gridviewroweventargs object. You can use the rowtype attribute to determine which row type is being created (header row, data row, and so on ).
We can use the rowcreated event to write a commandargument event to the linkbutton in the template column.
The following code example demonstrates how to use the rowcreated event to store the index of the row being created in the commandargument attribute of the linkbutton control contained in the row. This allows you to confirm that the row index of the linkbutton control is included when you click the button.

/// <Summary>
/// Write a commandargument event to the linkbutton of the template Column
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>

Protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
/Retrieve the linkbutton control from the first column.
Linkbutton linkbutton1 = (linkbutton) E. Row. findcontrol ("linkbutton1 ");
// Set the linkbutton's commandargument property with the row's index.
Linkbutton1.commandargument = E. Row. rowindex. tostring ();
}

}

All right, the remaining code remains unchanged, and the code in gridview1_rowcommand remains unchanged.

Related Article

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.