在用GridView控件時(shí),我們經(jīng)常會(huì )碰到獲取當前行的索引,通過(guò)索引進(jìn)行許多操作。例如,可以獲得當前行某一個(gè)控件元素;設置某一元素的值等等。
下面結合實(shí)例介紹幾種獲得GridView當前行索引值的方法。
實(shí)例:
① 目的:獲取GridView中RowCommand的當前索引行。
② 前臺頁(yè)面:在GridView中添加一模版列,里面添加一個(gè)LinkButton控件。
代碼:
<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>
小提示:如果在后臺代碼中用e.CommandArgument取值的話(huà),前臺代碼就必須在按鈕中設置CommandArgument的值,值為綁定的數據庫字段。如:
//因為在客戶(hù)端中就已經(jīng)將LinkButton的CommandArgument與主鍵Id給綁定了所以在此可以直接用e.CommandArgument得出主鍵ID的值
int id = Convert.ToInt32(e.CommandArgument.ToString());
③ 在GridView里已經(jīng)設置了LinkButton為事件處理按鈕,將通過(guò)以下方法獲取索引:
protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "QianRu")
{
【方法一】
GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被選中的索引值
inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此獲取的值為GridView中綁定數據庫中的主鍵值
注意:運用此方法,需要對GridView的DataKeyNames屬性進(jìn)行設置,此例中設置為主鍵字段。
【方法二】
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被選中的索引值
int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); //此獲取的值為GridView中綁定數據庫中的主鍵值,取值方法是選中的行中的第一列的值,drv.RowIndex取得是選中行的索引
}
}
此外,還有一些方法可以實(shí)現獲得當前行索引值。
【方法三】在linkbutton控件的Command事件,利用sender的Parent獲取GridView中的當前行。
protected void lbtnQianChu_Command(object sender, CommandEventArgs e)
{
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被選中的索引值
lbtnQianChu.SelectedIndex = gvr.RowIndex;
}
【方法四】在linkbutton控件的Click事件,獲取GridView中的當前行。
protected void LinkButton1_Click(object sender, EventArgs e)
{
//行號
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
}
【方法五】如果在模板列中添加一下DropDownList控件,并開(kāi)啟其AutoPostback屬性,在DropDownList 的SelectedIndexChanged事件中,獲取GridView中的當前行。
下面是SelectedIndexChanged事件的代碼摘要:
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);
第一句用來(lái)獲取觸發(fā)事件的DropDownList控件。
第二句就利用該控件的NamingContainer屬性,獲取其容器,也就是GridViewRow對象。
提示:由于DropDoweList與button不同,無(wú)法指定其CommandName,所以,通過(guò)用NamingContainer屬性來(lái)解決問(wèn)題。
先來(lái)看看微軟對該NamingContainer屬性的解釋?zhuān)?/p>
獲取對服務(wù)器控件的命名容器的引用,此引用創(chuàng )建唯一的命名空間,以區分具有相同 Control.ID 屬性值的服務(wù)器控件。
ASP.NET Web 應用程序的每一頁(yè)均包含控件的層次結構。此層次結構與控件是否生成用戶(hù)可見(jiàn)的 UI 無(wú)關(guān)。給定控件的命名容器是層次結構中該控件之上的父控件,此父控件實(shí)現 INamingContainer 接口。實(shí)現此接口的服務(wù)器控件為其子服務(wù)器控件的 ID 屬性值創(chuàng )建唯一的命名空間。
當針對列表 Web 服務(wù)器控件(如 Repeater 和 DataList 服務(wù)器控件)進(jìn)行數據綁定時(shí),為服務(wù)器控件創(chuàng )建唯一的命名空間尤其重要。當數據源中的多個(gè)項創(chuàng )建服務(wù)器控件的多個(gè)實(shí)例,且該服務(wù)器控件是重復控件的子級時(shí),命名容器確保這些子控件的每個(gè)實(shí)例具有不沖突的 UniqueID 屬性值。頁(yè)的默認命名容器是請求該頁(yè)時(shí)生成的 Page 類(lèi)的實(shí)例。
可以使用此屬性確定特定服務(wù)器控件所在的命名容器。
【方法六】如果模板列中有CheckBox控件的情況,通過(guò)CheckBox1_CheckedChanged事件中,獲取GridView中的當前行。
CheckBox chk = (CheckBox)sender;
DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent;
【方法七】
<asp:GridView ID="gvTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
DisplayIndex : <%# Container.DisplayIndex %> || DataItemIndex : <%# Container.DataItemIndex %><br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
聯(lián)系客服