简体   繁体   English

如何从Gridview-RowDataBound返回值以在链接中使用它。 C#

[英]How can i return value from Gridview-RowDataBound to use it in link. C#

Im trying to delete Gridview row on button click. 我试图删除按钮单击上的Gridview行。 i want to save the clicked row's id into a variable. 我想将点击的行的id保存到变量中。 And use this variable in the hyperlink. 并在超链接中使用此变量。

here my RowDataBound code 这是我的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 ;

    }
}

here the hyperlink where i need i id of the selected row 这里的超链接,我需要我所选行的ID

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

Firstly: add a reference to jQuery to the head section of your Master page 首先:将jQuery的引用添加到主页面的head部分

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

Secondly: add the ProductionOrderId to the datarow as an attribute (as shown below), this way it will be accessible on the client side via jQuery 其次:将ProductionOrderId作为属性添加到数据行(如下所示),这样它就可以通过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);
            }
}

Thirdly: 第三:

in the body of your .aspx file add the following script tag. 在.aspx文件的正文中添加以下脚本标记。 Every time a row is clicked it modifies your 'Delete' link with the id of the row to delete. 每次单击一行时,它都会修改“删除”链接,并使用要删除的行的ID。 I have also included your link for the sake of clarity and completeness. 为了清晰和完整,我还包括了您的链接。

<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>

Should you need further assistance please do not hesitate to let me know. 如果您需要进一步的帮助,请随时告诉我。

Disclaimer: It is usually best to use a cdn to deliver js files since it is highly likely that they have already been cached by the user browser. 免责声明:通常最好使用cdn来传递js文件,因为很可能它们已经被用户浏览器缓存了。

As requested, here is how to put jquery library 2.0 in your content folder: 根据要求,以下是如何将jquery库2.0放入您的内容文件夹:

  1. Make a backup of your working solution. 备份您的工作解决方案。
  2. Click on this link with your right mouse button and choose save as. 用鼠标右键单击此链接 ,然后选择另存为。
  3. Save it to your content folder on disk. 将其保存到磁盘上的内容文件夹中。
  4. From Visual studio choose 'add existing item' 从Visual studio中选择“添加现有项目”
  5. Browse to your content folder 浏览到您的内容文件夹
  6. Select your jquery file 选择你的jquery文件
  7. Click add. 单击添加。
  8. Now Drag your jquery file in the header section of your page. 现在将jquery文件拖到页面的标题部分。
  9. Delete the old tag (link to ajax.googleapis.com) 删除旧标记(链接到ajax.googleapis.com)

Modified the RowDataBound to hold the GridView record ids to the client side: 修改了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);
    }
}

And in your javascript code (using jQuery): This will change the hyperlink href value whenever the GridView record would be clicked. 在您的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>

Although I haven't tested the code, but I hope it should work as required. 虽然我没有测试过代码,但我希望它能按要求运行。

EDIT: You should also have added reference for jQuery.js file in the head section like this: 编辑:您还应该在头部添加jQuery.js文件的引用,如下所示:

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

An easier way would be to create a helper method: 一种更简单的方法是创建一个辅助方法:

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

And in the view: 在视图中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM