ASP. NET use the gridview to obtain the index value of the current row
This article mainly introduces how ASP. NET obtains the index value of the current row using the gridview method. For more information, see.
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:
?
1 2 3 4 5 6 7 |
<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:
?
1 2 3 4 |
Protected void gv_Company_RowCommand (object sender, GridViewCommandEventArgs e ){ If (e. CommandName = "QianRu ") { |
Method 1]
?
1 2 |
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]
?
1 2 3 4 |
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]
In the Command event of the linkbutton control, use the Parent of the sender to obtain the current row in the GridView.
?
1 2 3 4 5 6 7 |
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.
?
1 2 3 4 5 |
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:
?
1 2 3 4 |
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.
Each page of an ASP. NET Web application contains a 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, you can use the checkbox#checkedchanged event to obtain the current row in the GridView.
?
1 2 3 |
CheckBox chk = (CheckBox) sender; DataControlFieldCell dcf = (DataControlFieldCell) chk. Parent; GridViewRow gvr = (GridViewRow) dcf. Parent; |
Method 7]
?
1 2 3 4 5 6 7 8 9 |
<Asp: GridView ID = "gvTest" runat = "server"> <Columns> <Asp: TemplateField> <ItemTemplate> DisplayIndex: <% # Container. DisplayIndex %> | DataItemIndex: <% # Container. DataItemIndex %> <br/> </ItemTemplate> </Asp: TemplateField> </Columns> </Asp: GridView> |
Method 8]
The ID and Name of the control can be named in the preceding method. I need to use the RowCommand () method to determine which column is selected. The premise of using this method is that e. commandArgument is an attribute (you must first know that the row index in the GridView is placed in the CommandArgument). The task is to obtain this attribute. You can see that when creating each row in the GridView control, a RowCreated event is triggered. In this way, you can write the row number selected by the linkButton into the CommandArgument.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Protected void gvInfo_RowCreated (object sender, GridViewRowEventArgs e) { If (e. Row. RowType = DataControlRowType. DataRow) { LinkButton lk1 = (LinkButton) e. Row. FindControl ("lkbtn"); // the ID of the LinkButton Lk1.CommandArgument = e. Row. RowIndex. ToString (); } } Protected void gvInfo_RowCommand (object sender, GridViewCommandEventArgs e) { If (e. CommandName = "ADD") // CommandName of my LinkButton { Int index = Convert. ToInt32 (e. CommandArgument ); String aa = gvInfo. Rows [index]. Cells [1]. Text. ToString (); // obtain the value of the current row and column number, starting from 0 } } |
The above is all the content of this article. I hope you will like it.