使用jQuery获取ASP.Net Gridview的行索引

5

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

背景:

我使用模板字段中的服务器端链接按钮删除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上搜索了一下,但没有找到任何有用的信息。

3个回答

3

如果 LinkButton 是 GridView 中唯一的 LinkButton/Anchor,则您应该能够执行以下操作:

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

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

VB

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

c#

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

回复adrianos

如果您查看jQuery UI对话框,它有一个漂亮的模态确认框。

类似上面的代码,但替换确认功能,您可以使用:

<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的confirm方法,而是更喜欢使用带有“确定/取消”按钮的jQuery对话框。然后,我打算将OK按钮连接到服务器上的一个方法(这就是为什么我需要获取行索引的原因)- 谢谢。 - adrianos
嗨,Tim,这是一个不错的想法,但是一旦我点击删除按钮,它会同时执行客户端和服务器端代码 - 可能是因为我已经从链接按钮中删除了OnClientClick处理程序。 - adrianos
@adrianos,如果你在打开对话框后返回false,它将阻止按钮提交。但是,您需要想出一种使用JavaScript提交单击的按钮的方法。http://www.deviantpoint.com/post/2009/03/12/Using-jQuery-UI-Dialogs-for-confirmation-windows.aspx 是一个链接,其中提供了使用单个按钮完成此操作的解决方案。也许您可以调整它以适应您的gridview? - Tim B James
我刚刚也做了一个类似的解决方案 - 但是使用了隐藏字段和__doPostBack。我想将您的答案投票为“答案”,但这不是我所做的,我不想误导任何人。 - adrianos
将你的答案发布为回答。 - Tim B James

3
我使用Javascript中的__doPostBack方法来实现了这个功能。

>>> 在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;
                }
            }

页面加载时调用的新方法:

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隐藏输入控件,因为服务器端从不需要看到它。

这种感觉有点粗糙,欢迎向我投掷石头/建议改进。


2
  1. Add a custom attribute to your grid and set value on binding event

       <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>                
                        <a href="#" test='<%# Container.DataItemIndex %>'>content</a>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
  2. Using .net clientId get the custom attribute value.

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接