如何在ASP.NET中獲得RowIndex
為什么需要在RowCommand event獲得RowIndex呢?通常一個(gè)Table的PK或FK并不會(huì)顯示在GridView上,而會(huì)設(shè)定在DataKeyNames property,然后再RowCommand event根據(jù)RowIndex讀出該row的PK或FK,所以第一步,必須先能在RowCommand獲得RowIndex。
ASP.NET 1.x DataGrid
在ASP.NET 1.x的DataGrid,若要使用LinkButton,一樣得放在TemplateColumn內(nèi),且ItemCommand event的e.Item.ItemIndex就可抓到RowIndex。
當(dāng)在DataGrid點(diǎn)下FirstName后,會(huì)在下方的Label顯示LastName,LastName是此例的DataKey。
- <%@ Import Namespace="System.Data" %>
- <%@ Import Namespace="System.Data.SqlClient" %>
- <%@ Page Language="C#" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">- <script runat="server">
- /**//*
- (C) OOMusou 2007 http://oomusou.cnblogs.com
- Filename : DataGrid_DataKeyField.aspx
- Compiler : Visual Studio 2005 / C# 2.0 / ASP.NET 2.0
- Description : Demo how to get RowIndex in DataGrid's LinkButton
- Release : 06/26/2007 1.0
- */
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack)
- DataGrid1_DataBind();
- }
- protected void DataGrid1_DataBind() {
- string strSQL = "SELECT TOP 10 " +
- "fname," +
- "lname " +
- "FROM employee";
- SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial
Catalog=pubs;Integrated Security=True");- SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
- DataSet ds = new DataSet();
- try {
- da.Fill(ds);
- }
- catch (Exception err) {
- Response.Write(err.ToString());
- return;
- }
- finally {
- if ((con != null) && (con.State == ConnectionState.Open))
- con.Close();
- }
- DataGrid1.DataSource = ds;
- DataGrid1.DataKeyField = "lname";
- DataGrid1.AutoGenerateColumns = false;
- DataGrid1.DataBind();
- }
- protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e) {
- if (e.CommandName == "Select")
- Label1.Text = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:DataGrid ID="DataGrid1" runat="server" OnItemCommand="DataGrid1_ItemCommand">
- <Columns>
- <asp:TemplateColumn HeaderText="First Name">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"
Text='<%#DataBinder.Eval(Container.DataItem,"fname")%>'>- </asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateColumn>
- </Columns>
- </asp:DataGrid>
- <asp:Label ID="Label1" runat="server"></asp:Label>
- </form>
- </body>
- </html>
只需在ItemCommand event的e.Item.ItemIndex就可以輕松的抓到RowIndex。
ASP.NET 2.0 GridView
ASP.NET 2.0就改用SqlDataSource和GridView了,LinkButtom一樣得放在TemplateField,但GridView沒(méi)有ItemCommand event,取而代之的是RowCommand event。
- <%@ Page Language="C#" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">- <script runat="server">
- /**//*
- (C) OOMusou 2007 http://oomusou.cnblogs.com
- Filename : GridView_RowCommand_RowIndex.aspx
- Compiler : Visual Studio 2005 / C# 2.0 / ASP.NET 2.0
- Description : Demo how to get RowIndex in GridView's LinkButton
- Release : 06/26/2007 1.0
- */
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack)
- GridView1_DataBind();
- }
- protected void GridView1_DataBind() {
- SqlDataSource1.ConnectionString = @"Data Source=.\sqlexpress;Initial
Catalog=pubs;Integrated Security=True";- SqlDataSource1.SelectCommand = "SELECT TOP 10 " +
- "fname," +
- "lname " +
- "FROM employee";
- GridView1.DataSourceID = SqlDataSource1.ID;
- GridView1.DataKeyNames = new string[] { "lname" };
- GridView1.AutoGenerateColumns = false;
- }
- protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
- if (e.CommandName == "Select") {
- int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
- Label1.Text = GridView1.DataKeys[rowIndex].Value.ToString();
- }
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
- <Columns>
- <asp:TemplateField HeaderText="First Name">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"
Text='<%#Eval("fname")%>'></asp:LinkButton>- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
- <asp:Label ID="Label1" runat="server"></asp:Label>
- <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
- </form>
- </body>
- </html>
CommandSource傳的是按下去的LinkButton,不過(guò)由于傳回的是Object,就得自行轉(zhuǎn)成LinkButton,但由于我們想知道的是RowIndex,而LinkButton是包含在GridViewRow內(nèi),所以透過(guò)NamingContainer傳回目前的 GridViewRow,但傳回的是Control,所以需在轉(zhuǎn)成GridViewRow后才能有RowIndex property。
GridView是DataGrid的繼承人,但不少寫(xiě)法和DataGrid并不一樣,GridView很多地方比DataGrid更強(qiáng)更好用,但這個(gè)例子卻發(fā)現(xiàn)GridView比DataGrid麻煩些,或許我沒(méi)找到好最好的方法,若有人有更好的方式,歡迎指正,謝謝。以上介紹如何在ASP.NET中獲得RowIndex。
【編輯推薦】