Asp.net Tips: gridview method for obtaining the index of the current row

 

Asp.net Tips: gridview method for obtaining the index of the current row

When using a GridView control, we often encounter Gets the index of the current row, many operated by the index. For example, one can obtain the current control element rows; the set value of an element and the like.

Examples of several methods described below in connection with obtaining the current row index of the GridView.

Example:

① Objective: To get the GridView RowCommand current index row.

② front page: add a template column in the GridView, which add a LinkButton control.

Code:

<asp:TemplateField HeaderText="操作">

<ItemTemplate>

<asp:LinkButton ID="lbtnQianRu" runat="server" CommandName="QianRu"

CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton>

<asp:LinkButton ID="lbtnQianChu " runat="server" CommandName="QianChu">签出 </asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

Tip: If e.CommandArgument the value of the code used in the background, then you must set the value of the code reception CommandArgument of a button, the database field is bound. Such as:

// because the client has the LinkButton of CommandArgument the primary key to the Id bound so this can be drawn directly e.CommandArgument value of the primary key ID

int id = Convert.ToInt32(e.CommandArgument.ToString());

③ in the GridView LinkButton has been set for the event handling button to get indexed in the following ways:

protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){

if (e.CommandName == "QianRu")

{

【method one】

GridViewRow drv = ((GridViewRow) (((LinkButton) (e.CommandSource)) Parent.Parent).); // this is a derived value of that row is selected index value

inf id = Convert.ToInt32 (GridView1.DataKeys [drv.RowIndex] .Value); // this is acquired GridView bound primary key database

NOTE: Using this method, it is necessary to set the GridView DataKeyNames property, in this embodiment, the primary key field.

【Method Two】

GridViewRow drv = (GridViewRow) ((LinkButton) e.CommandSource) .NamingContainer; // this is a derived value of that row is selected index value

int id = Convert.ToInt32 (GridView1.Rows [drv.RowIndex] .Cells [0] .Text); // This line selected GridView bound value acquired primary key values ​​in the database, the value is the value of the first column, drv.RowIndex get is the index of the selected row

}

}


In addition, there are other ways to achieve to get the current row index value.

[Three] method in the event linkbutton Command control, the use of sender's Parent Gets the current row in the GridView.

protected void lbtnQianChu_Command(object sender, CommandEventArgs e)

{

LinkButton lb = (LinkButton)sender;

DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;

GridViewRow gvr = (GridViewRow) dcf.Parent; // this is a derived value of that row is selected index value

lbtnQianChu.SelectedIndex = gvr.RowIndex;

}

[Method IV] in the Click event linkbutton control, get the current row in the GridView.

protected void LinkButton1_Click(object sender, EventArgs e)

{

// line number

int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;

}

[Method five] If you add in the template column about the DropDownList control, and open its AutoPostback property, in the DropDownList's SelectedIndexChanged event, gets the current row in the GridView.

The following is a summary of the code 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 get the DropDownList control triggered the event.

NamingContainer second sentence on the use of the control's properties, acquiring its container, which is GridViewRow object.


Note: Due to different DropDoweList the button, you can not specify its CommandName, so, by using NamingContainer property to solve the problem.

Let's look at Microsoft's interpretation of the NamingContainer attributes:

Gets the server control's naming container, a reference to this quote to create a unique namespace for differentiating between server controls with the same Control.ID property value.

Each page ASP.NET Web application contains the hierarchy of controls. Whether this hierarchy of controls to generate user-visible UI is independent. Given control of the naming container hierarchy is parent control over the control, parent control to achieve this INamingContainer interface. Implement this server control interface creates a unique namespace ID property value for the sub-server control.

When data binding for a list of Web server controls (such as the Repeater and DataList server controls), the server control is especially important to create a unique namespace. When a plurality of items to create multiple instances of the server controls the data source and the server control is a child of the repeat control, to ensure that each container named examples of these sub-controls have UniqueID property values ​​do not conflict. The default page is an example of naming container page request generated Page class.

You can use this property to determine the specific server controls the naming container is located.


[Method six] If the template listed there CheckBox control of the situation, by CheckBox1_CheckedChanged event, get 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>

 

 

Reproduced in: https: //www.cnblogs.com/hubcarl/archive/2009/11/07/1597876.html

Guess you like

Origin blog.csdn.net/weixin_33762130/article/details/93817262