使用jQuery获取ASP.Net Gridview的rowIndex

adr*_*nos 5 asp.net jquery gridview

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

一点背景:

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

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

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

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();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是javascript确认(删除项目?)看起来有点不对劲.

我更喜欢使用类似JQuery的对话框,但如果我这样做,我不知道如何使用这种方法获取rowindex(我可以弄清楚如何调用服务器代码).

有任何想法吗?

对不起,如果已经有人问过这个问题 - 我做了拖网搜索并搜索了它,但找不到任何有用的东西.

adr*_*nos 3

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

>>> 在 aspx 中:

隐藏字段:

<asp:HiddenField ID="hidden_gridRowIndex" runat="server" />
Run Code Online (Sandbox Code Playgroud)

在脚本标签中:

    $(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');
                    }
Run Code Online (Sandbox Code Playgroud)

在网格视图上:

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

>>> 在代码隐藏中

在页面加载上:

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;
                }
            }
Run Code Online (Sandbox Code Playgroud)

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();
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

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