我怎么能从Gridview-RowDataBound返回值在链接中使用它.c#

本文关键字:链接 怎么能 Gridview-RowDataBound 返回值 | 更新日期: 2023-09-27 18:11:03

我试图删除Gridview行按钮单击。我想将点击行的id保存到一个变量中。并在超链接中使用此变量。

这里是我的RowDataBound代码

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onmouseover", "this.style.cursor='Pointer';this.style.backgroundColor='Yellow'");
       e.Row.RowIndex.ToString())); 
        string id = DataBinder.Eval(e.Row.DataItem, "ProductionOrderId").ToString();
    // somthing like
    // return id ;
    }
}

这里是我需要的超链接这里是选定行的id

<asp:HyperLink  runat="server" NavigateUrl="~/Producter/Delete?id= id" ID="HyperLink1"> Delete</asp:HyperLink>

我怎么能从Gridview-RowDataBound返回值在链接中使用它.c#

第一:在母版页的标题部分添加jQuery的引用

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

其次:将ProductionOrderId作为属性添加到数据row中(如下所示),这样就可以通过jQuery在客户端访问它

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.cursor='Pointer';this.style.backgroundColor='Yellow'");
                e.Row.RowIndex.ToString();
                string id = DataBinder.Eval(e.Row.DataItem, "ProductionOrderId").ToString();
                //save ProductionOrderId as datarow attribute
                e.Row.Attributes.Add("rowid", id);
            }
}

第三:

在.aspx文件主体中的

添加以下脚本标记。每次单击一行时,它都会用要删除的行id修改您的"删除"链接。为了清晰和完整,我还附上了你的链接。

<a href='<%=ResolveUrl("~/Producter/Delete?id=" ) %>' ID="HyperLink1">Delete</a>
 <script language="javascript">
    //every time a row is clicked this script will perform the following actions:
        $("tr").click(function () {
            var clicked = $(this);
            //get the row id from the currently cliked row
            var rowid = clicked.attr("rowid");
            //get the value of href attribute from the link with id 'HyperLink1'
            var link = $("#HyperLink1").attr("href");
            //remove any previously appended values
            var linkTokens = link.split("=");
            linkTokens[1] = "";
            link = linkTokens.join("=");
            //append the current row id to the link
            link = link + rowid;
            //set the href attribute of your link to the new value
            $("#HyperLink1").attr("href", link);
        });
    </script>

如果您需要进一步的帮助,请随时与我联系。

免责声明:通常最好使用cdn来传递js文件,因为它们很可能已经被用户浏览器缓存了。

按照要求,这里是如何把jquery库2.0放在你的内容文件夹:

  1. 备份您的工作解决方案
  2. 用鼠标右键点击此链接并选择另存为。
  3. 将其保存到磁盘上的内容文件夹。
  4. 从Visual studio选择"add existing item"
  5. 浏览到您的内容文件夹
  6. 选择jquery文件
  7. 单击add。
  8. 现在拖动你的jquery文件在你的页面的标题部分
  9. 删除旧标签(链接到ajax.googleapis.com)

修改RowDataBound以保存GridView记录id到客户端:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onmouseover", "this.style.cursor='Pointer';this.style.backgroundColor='Yellow'");
       e.Row.RowIndex.ToString())); 
        string id = DataBinder.Eval(e.Row.DataItem, "ProductionOrderId").ToString();
        // store the id at the client side
        e.Row.Attributes.Add("id", id);
    }
}

在你的javascript代码中(使用jQuery):当GridView记录被点击时,这将改变超链接的href值。

<script type="text/javascript">
  $('#<%=GridView1.ClientID %> tr[id]').click(function () {
      idToDelete = $('#<%=GridView1.ClientID %> tr[id]').val();
      $("a.HyperLink1").attr("href", "~/Producter/Delete?id=" + idToDelete);
  });
</script>

虽然我还没有测试代码,但我希望它应该按要求工作。

编辑:

您还应该在head部分添加jQuery.js文件的引用,如下所示:

<script src="js/jquery-1.6.1.min.js" type="text/javascript"></script>

更简单的方法是创建一个helper方法:

string getLink(MyObject obj)
{
   return "~/Producter/Delete?id" + obj.ID;
}

在视图中:

<asp:HyperLink  runat="server" NavigateUrl="<%# getLink(Container.DataItem) %>" ID="HyperLink1"> Delete</asp:HyperLink>