如何在ASP.NET中获得RowIndex
创始人
2024-04-25 06:50:58
0

为什么需要在RowCommand event获得RowIndex呢?通常一个Table的PK或FK并不会显示在GridView上,而会设定在DataKeyNames property,然后再RowCommand event根据RowIndex读出该row的PK或FK,所以第一步,必须先能在RowCommand获得RowIndex。

ASP.NET 1.x DataGrid

在ASP.NET 1.x的DataGrid,若要使用LinkButton,一样得放在TemplateColumn内,且ItemCommand event的e.Item.ItemIndex就可抓到RowIndex。

当在DataGrid点下FirstName后,会在下方的Label显示LastName,LastName是此例的DataKey。

  1. <%@ Import Namespace="System.Data" %> 
  2. <%@ Import Namespace="System.Data.SqlClient" %> 
  3.  
  4. <%@ Page Language="C#" %> 
  5.  
  6. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  7.  
  8.  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.  
  54.  
  55.  xmlns="http://www.w3.org/1999/xhtml"> 
  56.  runat="server"> 
  57. </FONT></STRONG>Untitled Page<STRONG><FONT color=#006699> 
  58.  
  59.  
  60.  id="form1" runat="server"> 
  61.  ID="DataGrid1" runat="server" OnItemCommand="DataGrid1_ItemCommand"> 
  62.  
  63.  HeaderText="First Name"> 
  64.  
  65.  ID="LinkButton1" runat="server" CommandName="Select" 
    Text='<%#DataBinder.Eval(Container.DataItem,"fname")%>'> 
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  ID="Label1" runat="server"> 
  72.  
  73.  
  74.  

只需在ItemCommand event的e.Item.ItemIndex就可以轻松的抓到RowIndex。

ASP.NET 2.0 GridView

ASP.NET 2.0就改用SqlDataSource和GridView了,LinkButtom一样得放在TemplateField,但GridView没有ItemCommand event,取而代之的是RowCommand event。

  1. <%@ Page Language="C#" %> 
  2.  
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4.  
  5.  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.  
  37.  
  38.  xmlns="http://www.w3.org/1999/xhtml"> 
  39.  runat="server"> 
  40. </FONT></STRONG>Untitled Page<STRONG><FONT color=#006699> 
  41.  
  42.  
  43.  id="form1" runat="server"> 
  44.  
  45.  ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"> 
  46.  
  47.  HeaderText="First Name"> 
  48.  
  49.  ID="LinkButton1" runat="server" CommandName="Select" 
    Text='<%#Eval("fname")%>'> 
  50.  
  51.  
  52.  
  53.  
 
  •  ID="Label1" runat="server"> 
  •  ID="SqlDataSource1" runat="server"> 
  •  
  •  
  •  
  • CommandSource传的是按下去的LinkButton,不过由于传回的是Object,就得自行转成LinkButton,但由于我们想知道的是RowIndex,而LinkButton是包含在GridViewRow内,所以透过NamingContainer传回目前的 GridViewRow,但传回的是Control,所以需在转成GridViewRow后才能有RowIndex property。

    GridView是DataGrid的继承人,但不少写法和DataGrid并不一样,GridView很多地方比DataGrid更强更好用,但这个例子却发现GridView比DataGrid麻烦些,或许我没找到好最好的方法,若有人有更好的方式,欢迎指正,谢谢。以上介绍如何在ASP.NET中获得RowIndex。

    【编辑推荐】

    1. ASP.NET开发技巧之Theme功能浅析
    2. 详解ASP.NET动态编译
    3. Apache支持ASP.NET方法浅析
    4. 浅谈ASP.NET服务器标准控件
    5. ASP.NET中SQL Server数据库备份恢复浅析

    相关内容

    热门资讯

    如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
    如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
    施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
    20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
    德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
    为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
    《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
    2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...