這篇文章主要介紹了ASP.NET使用gridview獲取當(dāng)前行的索引值的方法匯總,有需要的小伙伴可以參考下。
在用GridView控件時,我們經(jīng)常會碰到獲取當(dāng)前行的索引,通過索引進(jìn)行許多操作。例如,可以獲得當(dāng)前行某一個控件元素;設(shè)置某一元素的值等等。下面結(jié)合實例介紹幾種獲得GridView當(dāng)前行索引值的方法。
實例:
① 目的:獲取GridView中RowCommand的當(dāng)前索引行。
② 前臺頁面:在GridView中添加一模版列,里面添加一個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取值的話,前臺代碼就必須在按鈕中設(shè)置CommandArgument的值,值為綁定的數(shù)據(jù)庫字段。如:
//因為在客戶端中就已經(jīng)將LinkButton的CommandArgument與主鍵Id給綁定了所以在此可以直接用e.CommandArgument得出主鍵ID的值
int id = Convert.ToInt32(e.CommandArgument.ToString());
③ 在GridView里已經(jīng)設(shè)置了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中綁定數(shù)據(jù)庫中的主鍵值
注意:運用此方法,需要對GridView的DataKeyNames屬性進(jìn)行設(shè)置,此例中設(shè)置為主鍵字段。
【方法二】
- GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被選中的索引值
- int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); //此獲取的值為GridView中綁定數(shù)據(jù)庫中的主鍵值,取值方法是選中的行中的第一列的值,drv.RowIndex取得是選中行的索引
- }
- }
此外,還有一些方法可以實現(xiàn)獲得當(dāng)前行索引值。
【方法三】
在linkbutton控件的Command事件,利用sender的Parent獲取GridView中的當(dāng)前行。
- 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中的當(dāng)前行。
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- //行號
- int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
- }
【方法五】
如果在模板列中添加一下DropDownList控件,并開啟其AutoPostback屬性,在DropDownList 的SelectedIndexChanged事件中,獲取GridView中的當(dāng)前行。
下面是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);
第一句用來獲取觸發(fā)事件的DropDownList控件。
第二句就利用該控件的NamingContainer屬性,獲取其容器,也就是GridViewRow對象。
提示:由于DropDoweList與button不同,無法指定其CommandName,所以,通過用NamingContainer屬性來解決問題。
先來看看微軟對該NamingContainer屬性的解釋:
獲取對服務(wù)器控件的命名容器的引用,此引用創(chuàng)建唯一的命名空間,以區(qū)分具有相同 Control.ID 屬性值的服務(wù)器控件。
ASP.NET Web 應(yīng)用程序的每一頁均包含控件的層次結(jié)構(gòu)。此層次結(jié)構(gòu)與控件是否生成用戶可見的 UI 無關(guān)。給定控件的命名容器是層次結(jié)構(gòu)中該控件之上的父控件,此父控件實現(xiàn) INamingContainer 接口。實現(xiàn)此接口的服務(wù)器控件為其子服務(wù)器控件的 ID 屬性值創(chuàng)建唯一的命名空間。
當(dāng)針對列表 Web 服務(wù)器控件(如 Repeater 和 DataList 服務(wù)器控件)進(jìn)行數(shù)據(jù)綁定時,為服務(wù)器控件創(chuàng)建唯一的命名空間尤其重要。當(dāng)數(shù)據(jù)源中的多個項創(chuàng)建服務(wù)器控件的多個實例,且該服務(wù)器控件是重復(fù)控件的子級時,命名容器確保這些子控件的每個實例具有不沖突的 UniqueID 屬性值。頁的默認(rèn)命名容器是請求該頁時生成的 Page 類的實例。
可以使用此屬性確定特定服務(wù)器控件所在的命名容器。
【方法六】
如果模板列中有CheckBox控件的情況,通過CheckBox1_CheckedChanged事件中,獲取GridView中的當(dāng)前行。
- 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里面的),現(xiàn)在的任務(wù)就是獲得這么一個屬性。查資料可以知道,在創(chuàng)建GridView控件中每一行時,都將引發(fā)一個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();//獲取當(dāng)前行列號為一的值,列號從0開始
- }
- }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
新聞熱點
疑難解答
圖片精選