http://hi.baidu.com/xiaohaikong/blog/item/d1b478ffe8b8223c5c60088a.html
本文詳解GridView控件獲取當前行的索引值的
- < 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 >
方法,下面結合實例介紹幾種獲得GridView當前行索引值的方法。
實例:
① 目的:獲取GridView中RowCommand的當前索引行。
② 前臺頁面:在GridView中添加一模版列,裏面添加一個LinkButton控件。
代碼:
小提示:如果在後臺代碼中用e.CommandArgument取值的話,前臺代碼就必須在按鈕中設置CommandArgument的值,值爲綁定的數據庫字段。如:
- //因爲在客戶端中就已經將LinkButton的CommandArgument與主鍵Id給綁定了所以在此可以直接用e.CommandArgument得出主鍵ID的值
- int id = Convert.ToInt32(e.CommandArgument.ToString());
③ 在GridView裏已經設置了LinkButton爲事件處理按鈕,將通過以下方法獲取索引:
【方法一】
- 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屬性進行設置,此例中設置爲主鍵字段。
- }
- }
【方法二】
- protected void gv_Company_RowCommand( object sender, GridViewCommandEventArgs e){
- if (e.CommandName == "QianRu" )
- {
- GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
- //此得出的值是表示那行被選中的索引值
- int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text);
- //此獲取的值爲GridView中綁定數據庫中的主鍵值,取值方法是選中的行中的第一列的值,drv.RowIndex取得是選中行的索引
- }
- }
此外,還有一些方法可以實現獲得當前行索引值。
【方法三】
在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控件,並開啓其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);
第一句用來獲取觸發事件的DropDownList控件。
第二句就利用該控件的NamingContainer屬性,獲取其容器,也就是GridViewRow對象。
提示:由於DropDoweList與button不同,無法指定其CommandName,所以,通過用NamingContainer屬性來解決問題。
先來看看微軟對該NamingContainer屬性的解釋:
獲取對服務器控件的命名容器的引用,此引用創建唯一的命名空間,以區分具有相同 Control.ID 屬性值的服務器控件。
ASP.NET Web 應用程序的每一頁均包含控件的層次結構。此層次結構與控件是否生成用戶可見的 UI
無關。給定控件的命名容器是層次結構中該控件之上的父控件,此父控件實現 INamingContainer
接口。實現此接口的服務器控件爲其子服務器控件的 ID 屬性值創建唯一的命名空間。
當針對列表 Web 服務器控件(如 Repeater 和 DataList
服務器控件)進行數據綁定時,爲服務器控件創建唯一的命名空間尤其重要。當數據源中的多個項創建服務器控件的多個實例,且該服務器控件是重複控件的子級
時,命名容器確保這些子控件的每個實例具有不衝突的 UniqueID 屬性值。頁的默認命名容器是請求該頁時生成的 Page 類的實例。
可以使用此屬性確定特定服務器控件所在的命名容器。
【方法六】
如果模板列中有CheckBox控件的情況,通過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 >
【方法八】
控件的ID和Name命名可以如上方法,我需要通過RowCommand()方法判斷選中的是哪一列,而要使用這個方法的前提
是,e.CommandArgument這麼一個屬性(首先必須知道在GridView裏,行索引是被放在CommandArgument裏面的),現在
的任務就是獲得這麼一個屬性。查資料可以知道,在創建GridView控件中每一行時,都將引發一個RowCreated事件,藉此這麼個方法,可以把
linkButton所選擇的行號寫入CommandArgument中。
- protected void gvInfo_RowCreated( object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- LinkButton lk1 = (LinkButton)e.Row.FindControl("lkbtn" ); //LinkButton的ID
- lk1.CommandArgument = e.Row.RowIndex.ToString();
- }
- }
- protected void gvInfo_RowCommand( object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName == "ADD" ) //我LinkButton的CommandName
- {
- int index = Convert.ToInt32(e.CommandArgument);
- string aa = gvInfo.Rows[index].Cells[1].Text.ToString(); //獲取當前行列號爲一的值,列號從0開始
- }
- }