【问题标题】:Get the rowIndex of an ASP.Net Gridview using jQuery使用 jQuery 获取 ASP.Net Gridview 的 rowIndex
【发布时间】:2011-04-18 17:50:09
【问题描述】:

您好,是否可以使用 jQuery 获取 gridview 的当前行索引?

一点背景:

我使用模板字段中的服务器端链接按钮从网格视图中删除行,如下所示:

<asp:LinkButton CausesValidation="false" CommandName="Delete" ID="lnkDelete"
              OnClientClick="javascript: return(confirm('Delete item?'));" runat="server" Text="Delete" />

提示用户确认或取消删除。如果用户点击确定,它会在代码隐藏中调用此方法:

protected void GridViewRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this.gridview_uploads.EditIndex = -1;

            if (!this.UploadsList.Count.Equals(0))
            {
                DocumentUpload upload = this.UploadsList[e.RowIndex];
                if (upload != null)
                {
                    this.UploadsList.RemoveAt(e.RowIndex);
                    this.BindInputGridview();
                }
            }
        }

但是 javascript 确认(删除项目?)看起来有点糟糕。

我更喜欢使用 JQuery 的对话框之类的东西,但如果我这样做了,我不知道如何使用这种方法获取行索引(我可以弄清楚如何调用服务器代码)。

有什么想法吗?

很抱歉,如果已经有人问过这个问题 - 我搜索了 SO 并用 Google 搜索了它,但找不到任何有用的东西。

【问题讨论】:

    标签: asp.net jquery gridview


    【解决方案1】:

    如果LinkButton 是 GridView 中唯一的 LinkBut​​ton/Anchor,那么您应该能够执行类似的操作

    $('#GridView1 a').click(function(){
         return confirm("Delete Item");
    });
    

    编辑:将#GridView1 更改为控件的.net ID。

    vb

    <%=(me.GridView1.ClientID)%>
    

    c#

    <%=(this.GridView1.ClientID)%>
    

    回复阿德里亚诺斯

    如果您查看jQuery UI Dialog,它有一个不错的模态确认框。

    与上述代码类似,但替换确认功能,您可以:

    <script type="text/javascript">
     $().ready(function(){
        $( "#dialog" ).dialog( "destroy" );
        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
                        autoOpen: false;
            buttons: {
                "Delete item": function() {
                    $( this ).dialog( "close" );
                    // Put in your return true/false here to activate button
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });
        $('#GridView1 a').click(function(){
            $('#dialog-confirm').dialog("open");
            return false;
        });
    
        )};
    </script>
    

    【讨论】:

    • 我不想使用 Javascript 的确认方法,而是更喜欢使用带有确定/取消按钮的 jQuery 对话框。然后我打算连接 OK 按钮以在服务器上触发一个方法(这就是我需要获取 rowindex 的原因) - 不过谢谢。
    • 嗨蒂姆,这是一个好主意,但只要我单击删除按钮,它就会同时执行客户端和服务器端代码 - 大概是因为我已经取出了链接按钮上的 OnClientClick 处理程序。
    • @adrianos,如果您在打开对话框后返回 false,它将阻止按钮提交。但是您需要想出一种方法,然后使用 javascript 提交单击的按钮。 deviantpoint.com/post/2009/03/12/… 是指向页面的链接,该页面具有使用单个按钮执行此操作的解决方案。也许您可以调整它以在您的网格视图中工作?
    • 我刚刚研究了一个类似的解决方案 - 但使用隐藏字段和 __doPostBack 代替。我想将您的答案投票为“答案”,但这不是我的做法,我不想误导任何人。
    • 张贴你的答案作为答案。
    【解决方案2】:

    我想出了如何使用 __doPostBack 方法(在 Javascript 中)来做到这一点

    >>> 在 aspx 中:

    隐藏字段:

    <asp:HiddenField ID="hidden_gridRowIndex" runat="server" />
    

    在脚本标签中:

        $(document).ready
        (
        function () {
          $("#div_dialog_confirmUploadDelete").dialog({
            autoOpen: false
            , title: "Delete upload"
            , buttons: {
                "OK": function () {
                                __doPostBack('GridViewRowDelete', $("#<%# hidden_gridRowIndex.ClientID %>").val());
                                $(this).dialog('close');
                            }
                 , "Cancel": function () { $(this).dialog('close'); }
                        }
                        });
    
    });
    
    
        function deleteConfirm(index) {
                            $("#<%# hidden_gridRowIndex.ClientID %>").val(index)
                            $("#div_dialog_confirmUploadDelete").dialog('open');
                        }
    

    在网格视图上:

    <asp:TemplateField>
      <ItemTemplate>
        <a href="javascript: void(0);" onclick='javascript:return deleteConfirm(<%# Container.DataItemIndex %>);'>Delete</a>
      </ItemTemplate>
    </asp:TemplateField>
    

    >>> 在代码隐藏中

    在 Page_Load 上:

    if (Request["__EVENTTARGET"] != null)
                {
                    switch (Request["__EVENTTARGET"])
                    {
                        case "GridViewRowDelete":
                            if (Request["__EVENTARGUMENT"] != null)
                            {
                                int index = -1;
                                if (int.TryParse(Request["__EVENTARGUMENT"], out index))
                                {
                                    this.GridViewRowDelete(index);
                                }
                            }
                            break;
                    }
                }
    

    page_load 调用的新方法:

    protected void GridViewRowDelete(int rowIndex)
            {
                this.gridview_uploads.EditIndex = -1;
    
                if (!this.UploadsList.Count.Equals(0))
                {
                    DocumentUpload upload = this.UploadsList[rowIndex];
                    if (upload != null)
                    {
                        this.UploadsList.RemoveAt(rowIndex);
                        this.BindInputGridview();
                    }
                }
            }
    

    考虑一下,我可能已经将 asp:HiddenField 设置为常规的 html 隐藏输入控件,因为服务器端永远不需要看到它。

    感觉有点棘手,所以请随意向我扔石头/提出改进建议。

    【讨论】:

      【解决方案3】:
      1. 将自定义属性添加到您的网格并设置绑定事件的值

           <asp:GridView ID="GridView1" runat="server">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>                
                            <a href="#" test='<%# Container.DataItemIndex %>'>content</a>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        
      2. 使用.net clientId 获取自定义属性值。

         $(document).ready(function () {
         $('#<%=(this.GridView1.ClientID)%> a').click(function () {
            return alert("Last Name : " + this.getAttribute("test") );
          })
          }
             );
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        • 2014-04-25
        • 1970-01-01
        • 2012-04-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        相关资源
        最近更新 更多