国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET使用gridview獲取當前行的索引值

2024-07-10 13:29:05
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了ASP.NET使用gridview獲取當前行的索引值的方法匯總,有需要的小伙伴可以參考下。

在用GridView控件時,我們經常會碰到獲取當前行的索引,通過索引進行許多操作。例如,可以獲得當前行某一個控件元素;設置某一元素的值等等。下面結合實例介紹幾種獲得GridView當前行索引值的方法。

實例:

① 目的:獲取GridView中RowCommand的當前索引行。

② 前臺頁面:在GridView中添加一模版列,里面添加一個LinkButton控件。

代碼:

 

 
  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>  

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

//因為在客戶端中就已經將LinkButton的CommandArgument與主鍵Id給綁定了所以在此可以直接用e.CommandArgument得出主鍵ID的值

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. {  

【方法一】

 

 
  1. GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被選中的索引值  
  2. inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此獲取的值為GridView中綁定數據庫中的主鍵值  

注意:運用此方法,需要對GridView的DataKeyNames屬性進行設置,此例中設置為主鍵字段。

【方法二】

 

 
  1. GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被選中的索引值  
  2. int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); //此獲取的值為GridView中綁定數據庫中的主鍵值,取值方法是選中的行中的第一列的值,drv.RowIndex取得是選中行的索引  
  3. }  
  4. }  

此外,還有一些方法可以實現獲得當前行索引值。

【方法三】

在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. }  

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吕梁市| 河间市| 阿拉尔市| 六盘水市| 穆棱市| 柳河县| 顺昌县| 北京市| 峨眉山市| 建宁县| 横峰县| 襄城县| 东丰县| 太湖县| 波密县| 荃湾区| 屏南县| 修水县| 德州市| 北安市| 勐海县| 于都县| 平利县| 扶风县| 淮南市| 慈利县| 交城县| 公安县| 巴南区| 北宁市| 台东市| 洛扎县| 祁连县| 曲沃县| 子洲县| 荣成市| 西宁市| 清徐县| 湖口县| 鄱阳县| 鄱阳县|