詳解GridView控件獲取當前行的索引值的

 

http://hi.baidu.com/xiaohaikong/blog/item/d1b478ffe8b8223c5c60088a.html

 

 

本文詳解GridView控件獲取當前行的索引值的

  1. < asp:TemplateField HeaderText = "操作" >
  2.             < ItemTemplate >
  3.                     < asp:LinkButton ID = "lbtnQianRu" runat = "server" CommandName = "QianRu"   
  4.                      CommandArgument = '<%# Eval("Id") %>' > 簽入 </ asp:LinkButton >    
  5.                      < asp:LinkButton ID = "lbtnQianChu " runat = "server" CommandName = "QianChu" > 簽出                       </ asp:LinkButton >
  6.                     </ ItemTemplate >
  7. </ asp:TemplateField >

方法,下面結合實例介紹幾種獲得GridView當前行索引值的方法。
實例:
① 目的:獲取GridView中RowCommand的當前索引行。
② 前臺頁面:在GridView中添加一模版列,裏面添加一個LinkButton控件。
代碼:

小提示:如果在後臺代碼中用e.CommandArgument取值的話,前臺代碼就必須在按鈕中設置CommandArgument的值,值爲綁定的數據庫字段。如:

  1. //因爲在客戶端中就已經將LinkButton的CommandArgument與主鍵Id給綁定了所以在此可以直接用e.CommandArgument得出主鍵ID的值
  2. int id = Convert.ToInt32(e.CommandArgument.ToString());

③ 在GridView裏已經設置了LinkButton爲事件處理按鈕,將通過以下方法獲取索引:

【方法一】

  1. protected void gv_Company_RowCommand( object sender, GridViewCommandEventArgs e){
  2. if (e.CommandName == "QianRu" )
  3. {   
  4. GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));  
  5. //此得出的值是表示那行被選中的索引值
  6. inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value);
  7. //此獲取的值爲GridView中綁定數據庫中的主鍵值注意:運用此方法,需要對GridView的DataKeyNames屬性進行設置,此例中設置爲主鍵字段。
  8.    }
  9. }

【方法二】

  1. protected void gv_Company_RowCommand( object sender, GridViewCommandEventArgs e){
  2. if (e.CommandName == "QianRu" )
  3. {   
  4. GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
  5. //此得出的值是表示那行被選中的索引值
  6. int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text);  
  7. //此獲取的值爲GridView中綁定數據庫中的主鍵值,取值方法是選中的行中的第一列的值,drv.RowIndex取得是選中行的索引
  8.    }
  9. }

此外,還有一些方法可以實現獲得當前行索引值。
【方法三】 在linkbutton控件的Command事件,利用sender的Parent獲取GridView中的當前行。

  1. protected void lbtnQianChu_Command( object sender, CommandEventArgs e)
  2.                     {
  3.                      LinkButton lb = (LinkButton)sender;
  4.                              DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
  5.                              GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被選中的索引值
  6.                              lbtnQianChu.SelectedIndex = gvr.RowIndex;
  7.                     }

【方法四】 在linkbutton控件的Click事件,獲取GridView中的當前行。

  1. protected void LinkButton1_Click( object sender, EventArgs e)
  2. {
  3.       //行號
  4.       int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
  5. }

【方法五】 如果在模板列中添加一下DropDownList控件,並開啓其AutoPostback屬性,在DropDownList 的SelectedIndexChanged事件中,獲取GridView中的當前行。
下面是SelectedIndexChanged事件的代碼摘要:

  1. DropDownList ddl = (DropDownList)sender;
  2. GridViewRow gvr = (GridViewRow)ddl.NamingContainer;
  3. int id = int .Parse(GridView1.DataKeys[gvr.RowIndex][0].ToString());
  4. 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中的當前行。

  1. CheckBox chk = (CheckBox)sender;
  2. DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;
  3. GridViewRow gvr = (GridViewRow)dcf.Parent;

【方法七】

  1. < asp:GridView ID = "gvTest" runat = "server" >          
  2.         < Columns >
  3.         < asp:TemplateField >
  4.         < ItemTemplate >
  5.          DisplayIndex : < %# Container.DisplayIndex % >    || DataItemIndex : < %# Container.DataItemIndex % > < br />
  6.         </ ItemTemplate >
  7.         </ asp:TemplateField >
  8.         </ Columns >
  9. </ asp:GridView >

【方法八】
控件的ID和Name命名可以如上方法,我需要通過RowCommand()方法判斷選中的是哪一列,而要使用這個方法的前提 是,e.CommandArgument這麼一個屬性(首先必須知道在GridView裏,行索引是被放在CommandArgument裏面的),現在 的任務就是獲得這麼一個屬性。查資料可以知道,在創建GridView控件中每一行時,都將引發一個RowCreated事件,藉此這麼個方法,可以把 linkButton所選擇的行號寫入CommandArgument中。

  1. protected void gvInfo_RowCreated( object sender, GridViewRowEventArgs e)  
  2. {  
  3. if (e.Row.RowType == DataControlRowType.DataRow)  
  4. {  
  5. LinkButton lk1 = (LinkButton)e.Row.FindControl("lkbtn" ); //LinkButton的ID
  6. lk1.CommandArgument = e.Row.RowIndex.ToString();  
  7. }  
  8. }  
  9. protected void gvInfo_RowCommand( object sender, GridViewCommandEventArgs e)  
  10. {  
  11. if (e.CommandName == "ADD" ) //我LinkButton的CommandName
  12. {  
  13. int index = Convert.ToInt32(e.CommandArgument);  
  14. string aa = gvInfo.Rows[index].Cells[1].Text.ToString(); //獲取當前行列號爲一的值,列號從0開始
  15. }  
  16. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章