自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在ASP.NET中獲得RowIndex

開(kāi)發(fā) 后端
本文介紹如何在ASP.NET中獲得RowIndex,首先說(shuō)明為什么需要在RowCommand event獲得RowIndex呢,然后介紹抓到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。

  1. <%@ Import Namespace="System.Data" %> 
  2. <%@ Import Namespace="System.Data.SqlClient" %> 
  3.  
  4. <%@ Page Language="C#" %> 
  5.  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    > 
  7.  
  8. <script runat="server"> 
  9. /**//*   
  10. (C) OOMusou 2007 http://oomusou.cnblogs.com  
  11.  
  12. Filename    : DataGrid_DataKeyField.aspx  
  13. Compiler    : Visual Studio 2005 / C# 2.0 / ASP.NET 2.0  
  14. Description : Demo how to get RowIndex in DataGrid's LinkButton  
  15. Release     : 06/26/2007 1.0  
  16. */  
  17. protected void Page_Load(object sender, EventArgs e) {  
  18. if (!IsPostBack)   
  19. DataGrid1_DataBind();  
  20. }  
  21.  
  22. protected void DataGrid1_DataBind() {  
  23. string strSQL = "SELECT TOP 10 " +  
  24. "fname," +  
  25. "lname " +  
  26. "FROM employee";  
  27. SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial 
    Catalog=pubs;Integrated Security=True");  
  28. SqlDataAdapter da = new SqlDataAdapter(strSQL, con);  
  29. DataSet ds = new DataSet();  
  30.  
  31. try {  
  32. da.Fill(ds);  
  33. }  
  34. catch (Exception err) {  
  35. Response.Write(err.ToString());  
  36. return;  
  37. }  
  38. finally {  
  39. if ((con != null) && (con.State == ConnectionState.Open))  
  40. con.Close();  
  41. }  
  42.  
  43. DataGrid1.DataSource = ds;  
  44. DataGrid1.DataKeyField = "lname";  
  45. DataGrid1.AutoGenerateColumns = false;  
  46. DataGrid1.DataBind();  
  47. }  
  48.  
  49. protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e) {  
  50. if (e.CommandName == "Select")  
  51. Label1.Text = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();  
  52. }  
  53. </script> 
  54.  
  55. <html xmlns="http://www.w3.org/1999/xhtml"> 
  56. <head runat="server"> 
  57. <title>Untitled Page</title> 
  58. </head> 
  59. <body> 
  60. <form id="form1" runat="server"> 
  61. <asp:DataGrid ID="DataGrid1" runat="server" OnItemCommand="DataGrid1_ItemCommand"> 
  62. <Columns> 
  63. <asp:TemplateColumn HeaderText="First Name"> 
  64. <ItemTemplate> 
  65. <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" 
    Text='<%#DataBinder.Eval(Container.DataItem,"fname")%>'> 
  66. </asp:LinkButton> 
  67. </ItemTemplate> 
  68. </asp:TemplateColumn> 
  69. </Columns> 
  70. </asp:DataGrid> 
  71. <asp:Label ID="Label1" runat="server"></asp:Label> 
  72. </form> 
  73. </body> 
  74. </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。

  1. <%@ Page Language="C#" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    > 
  4.  
  5. <script runat="server"> 
  6. /**//*   
  7. (C) OOMusou 2007 http://oomusou.cnblogs.com  
  8.  
  9. Filename    : GridView_RowCommand_RowIndex.aspx  
  10. Compiler    : Visual Studio 2005 / C# 2.0 / ASP.NET 2.0  
  11. Description : Demo how to get RowIndex in GridView's LinkButton  
  12. Release     : 06/26/2007 1.0  
  13. */  
  14. protected void Page_Load(object sender, EventArgs e) {  
  15. if (!IsPostBack)  
  16. GridView1_DataBind();  
  17. }  
  18.  
  19. protected void GridView1_DataBind() {  
  20. SqlDataSource1.ConnectionString = @"Data Source=.\sqlexpress;Initial 
    Catalog=pubs;Integrated Security=True";  
  21. SqlDataSource1.SelectCommand = "SELECT TOP 10 " +  
  22. "fname," +  
  23. "lname " +  
  24. "FROM employee";  
  25. GridView1.DataSourceID = SqlDataSource1.ID;  
  26. GridView1.DataKeyNames = new string[] { "lname" };  
  27. GridView1.AutoGenerateColumns = false;  
  28. }  
  29.  
  30. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {  
  31. if (e.CommandName == "Select") {  
  32. int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;  
  33. Label1.Text = GridView1.DataKeys[rowIndex].Value.ToString();  
  34. }  
  35. }  
  36. </script> 
  37.  
  38. <html xmlns="http://www.w3.org/1999/xhtml"> 
  39. <head runat="server"> 
  40. <title>Untitled Page</title> 
  41. </head> 
  42. <body> 
  43. <form id="form1" runat="server"> 
  44. <div> 
  45. <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"> 
  46. <Columns> 
  47. <asp:TemplateField HeaderText="First Name"> 
  48. <ItemTemplate> 
  49. <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" 
    Text='<%#Eval("fname")%>'></asp:LinkButton> 
  50. </ItemTemplate> 
  51. </asp:TemplateField> 
  52. </Columns> 
  53. </asp:GridView> 
  54. </div> 
  55. <asp:Label ID="Label1" runat="server"></asp:Label> 
  56. <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource> 
  57. </form> 
  58. </body> 
  59. </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。

【編輯推薦】

  1. ASP.NET開(kāi)發(fā)技巧之Theme功能淺析
  2. 詳解ASP.NET動(dòng)態(tài)編譯
  3. Apache支持ASP.NET方法淺析
  4. 淺談ASP.NET服務(wù)器標(biāo)準(zhǔn)控件
  5. ASP.NET中SQL Server數(shù)據(jù)庫(kù)備份恢復(fù)淺析
責(zé)任編輯:佚名 來(lái)源: 中國(guó)IT實(shí)驗(yàn)室
相關(guān)推薦

2021-01-15 05:38:28

ASPHttp端口

2021-01-13 07:33:41

API數(shù)據(jù)安全

2021-11-01 14:52:38

ElasticSear索引SQL

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-01-31 22:56:50

FromServiceASP

2021-02-03 13:35:25

ASPweb程序

2021-03-03 22:37:16

MediatR中介者模式

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache緩存框架

2021-01-28 22:39:35

LoggerMessa開(kāi)源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-01-11 05:20:05

Controller代碼數(shù)據(jù)層

2013-03-25 10:38:24

ASP.NETHttpModule

2021-03-18 07:33:54

PDF DinkToPdfC++

2021-02-07 17:29:04

監(jiān)視文件接口

2009-02-05 14:02:46

SmtpMail發(fā)送郵件ASP.NET

2009-08-03 14:22:33

什么是ASP.NET

2009-07-28 17:17:19

ASP.NET概述
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)