Large set of methods for obtaining the index of the current row in the gridview

Source: Internet
Author: User
Note: I have not found out where the original works come from, and people turn around, so please forgive me for seeing the original works!

 

When using the gridview control, we often encounter the index of the current row, and perform many operations through the index. For example, you can obtain a control element of the current row and set the value of an element.

The following describes several methods to obtain the index value of the current row of the gridview.

Instance:

① Purpose: to obtain the current index row of rowcommand in the gridview.

② Foreground page: Add a template column in The gridview to add a linkbutton control.

Code:

<Asp: templatefield headertext = "operation">

<Itemtemplate>

<Asp: linkbutton id = "lbtnqianru" runat = "server" commandname = "qianru"

Commandargument = '<% # eval ("ID") %>'> check in </ASP: linkbutton>

<Asp: linkbutton id = "lbtnqianchu" runat = "server" commandname = "qianchu"> check out </ASP: linkbutton>

</Itemtemplate>

</ASP: templatefield>

TIPS: if you use the e. commandargument value in the background code, the foreground code must set the commandargument value in the button. The value is the bound database field. For example:

// Because the commandargument and primary key ID of the linkbutton have been bound to the client, you can use E. commandargument to obtain the value of the primary key ID.

Int id = convert. toint32 (E. commandargument. tostring ());

③ If linkbutton is set as the event processing button in the gridview, the index will be obtained using the following methods:

Protected void gv_company_rowcommand (Object sender, gridviewcommandeventargs e ){

If (E. commandname = "qianru ")

{

Method 1]

Gridviewrow DRV = (gridviewrow) (linkbutton) (E. commandsource). Parent. Parent); // the value obtained indicates the index value selected for the row.

INF id = convert. toint32 (gridview1.datakeys [DRV. rowindex]. Value); // The obtained value is the primary key value bound to the database in the gridview.

Note: to use this method, you must set the datakeynames attribute of the gridview. In this example, It is set as the primary key field.

Method 2]

Gridviewrow DRV = (gridviewrow) (linkbutton) E. commandsource). namingcontainer; // the value obtained here indicates the selected index value of the row.

Int id = convert. toint32 (gridview1.rows [DRV. rowindex]. cells [0]. text); // The obtained value is the value of the primary key bound to the database in the gridview. The value is the value of the first column in the selected row, DRV. rowindex retrieves the index of the selected row

}

}

 

In addition, there are some ways to obtain the index value of the current row.

Method 3: Use the parent of the sender to obtain the current row in the gridview in the command event of the linkbutton control.

Protected void lbtnqianchu_command (Object sender, commandeventargs E)

{

Linkbutton lB = (linkbutton) sender;

Datacontrolfieldcell DCF = (datacontrolfieldcell) lb. parent;

Gridviewrow gvr = (gridviewrow) DCF. parent; // The value is the index value selected for the row.

Lbtnqianchu. selectedindex = gvr. rowindex;

}

Method 4: In the Click Event of the linkbutton control, obtain the current row in the gridview.

Protected void linkbutton#click (Object sender, eventargs E)

{

// Row number

Int ROW = (gridviewrow) (linkbutton) sender). namingcontainer). rowindex;

}

[Method 5] If the dropdownlist control is added to the template column and Its autopostback attribute is enabled, the current row in the dropdownlist selectedindexchanged event is obtained.

The following is the code summary of the selectedindexchanged event:

Dropdownlist DDL = (dropdownlist) sender;

Gridviewrow gvr = (gridviewrow) DDL. namingcontainer;

Int id = int. parse (gridview1.datakeys [gvr. rowindex] [0]. tostring ());

Int num = int. parse (DDL. Text );

The first sentence is used to obtain the dropdownlist control that triggers the event.

The second sentence is to use the namingcontainer attribute of the control to obtain its container, that is, the gridviewrow object.

 

Tip: The commandname cannot be specified because dropdowelist is different from button. Therefore, the namingcontainer attribute is used to solve the problem.

Let's take a look at Microsoft's explanation of the namingcontainer attribute:

Gets a reference to the name container of the server control. This reference creates a unique namespace to distinguish the server control with the same control. ID attribute value.

ASP. NET web applicationsProgramEach page contains the control hierarchy. This hierarchy is irrelevant to whether the control generates a user-visible UI. The name container of the given control is the parent control on the control in the hierarchy. This parent control implements the inamingcontainer interface. The server control that implements this interface creates a unique namespace for the ID attribute value of its subserver control.

When you bind data to a list web server control (such as a repeater and datalist Server Control), it is particularly important to create a unique namespace for the server control. When multiple items in the data source create multiple instances of the server control and the server control is a child level of the repeated control, the naming container ensures that each instance of these child controls has a uniqueid attribute value that does not conflict with each other. The default naming container of the page is the page class instance generated when the page is requested.

You can use this property to determine the name container of a specific server control.

 

Method 6: If the template column contains the checkbox control, use the checkbox#checkedchanged event to obtain the current row in the gridview.

Checkbox chk = (checkbox) sender;

Datacontrolfieldcell DCF = (datacontrolfieldcell) chk. parent;

Gridviewrow gvr = (gridviewrow) DCF. parent;

 

Method 7]

<Asp: gridview id = "gvtest" runat = "server">

<Columns>

<Asp: templatefield>

<Itemtemplate>

Displayindex: <% # container. displayindex %> | dataitemindex: <% # container. dataitemindex %> <br/>

</Itemtemplate>

</ASP: templatefield>

</Columns>

</ASP: gridview>

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.