代码之家  ›  专栏  ›  技术社区  ›  adrianos

使用jquery获取ASP.NET网格视图的行索引

  •  5
  • adrianos  · 技术社区  · 15 年前

    您好,是否可以使用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的对话框,但是如果我这样做了,我不知道如何使用这种方法获取rowindex(我可以找到如何调用服务器代码)。

    有什么想法吗?

    抱歉,如果这已经被问到了-我做了一个搜索,并谷歌搜索它,但找不到任何有用的。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Tim B James    15 年前

    如果 LinkButton 是GridView中唯一的LinkButton/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>
    
        2
  •  3
  •   adrianos    15 年前

    我使用uuDoPostback方法(在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>
    

    >>>在代码后面

    关于PayHyLoad:

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

    感觉有点不舒服,所以请随意向我扔石头/建议改进。

        3
  •  2
  •   Arjun Shetty    15 年前
    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客户端ID获取自定义属性值。

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