zoukankan      html  css  js  c++  java
  • GridView DataKeys用法适用于多主键

    前台GridView中模板列的代码如下 ,在GridView中添加了一个模板列,模板列中放了一个ImageButton按钮,如下: 

    <asp:TemplateField HeaderText="管理">
         <ItemTemplate>
             <asp:ImageButton ID="imgBtnManage" runat="server" CommandName="manage" CommandArgument='<%# Eval("ProductID") %>' ImageUrl="~/Images/manage_25X25.gif" />
        </ItemTemplate>
     </asp:TemplateField>

    第一种方式,取得点击的行号,主键值,某行中指定列的值,如下:

    protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
    {

      int index = ((GridViewRow)((ImageButton)(e.CommandSource)).Parent.Parent).RowIndex;//获取当前点击前的行号  ,说明:ImageButton因为我                                                                                                                  // 放的按钮为ImageButton,用户可以根据放置的控件不同而不同。
         string key = this.gvProduct.DataKeys[index].Value.ToString();//获取当前选点击行的主键值
         string id = this.gvProduct.Rows[index].Cells[1].Text;//获取当前点击行,某列的值

    /*

      补充一下,因为我在上面的前台代码中放了CommandArgument='<%# Eval("ProductID") %>',ProductID为数据库中的主键,所以得到主键值也可以如下:string strProductId = e.CommandArgument.ToString(); 

    */

    //     有了上面的操作,以下操作就方便了

      if (e.CommandName == "manage")
         {

        //.......

      }

    }

    第二种方式:

    //在前台代码中,不要     CommandArgument='<%# Eval("ProductID") %>'  这个代码,即去掉这个,然后,下面操作:

    protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "manage")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string key = this.gvProduct.DataKeys[index].value.toString();
                Response.Write(key);
            }
        }

     //行数据绑定
     protected void gvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgBtn = new ImageButton ();
                imgBtn = (ImageButton )e.Row.Cells[6].FindControl("imgBtnManage");//自己清楚,此ImageButton模板列,你放在GridView中的第几列。
                imgBtn .CommandArgument = e.Row.RowIndex.ToString();
            }
    }

  • 相关阅读:
    EPUB书籍阅读器插件分享
    网页端压缩解压缩插件JSZIP库的使用
    让编辑器支持word的复制黏贴,支持截屏的黏贴
    MYSQL GTID position
    Google SRE
    MySQL大小写敏感
    SpringMVC model 多余字段 忽略
    To B Vs To C
    滴滴 CTO 架构师 业务 技术 战役 时间 赛跑 超前 设计
    Spring Boot 集成Swagger
  • 原文地址:https://www.cnblogs.com/truth/p/1783772.html
Copyright © 2011-2022 走看看