【问题标题】:Select All checkbox not firing back end code全选复选框不触发后端代码
【发布时间】:2015-06-16 09:21:04
【问题描述】:

我在页面中添加了一个全选复选框,并使用 JQuery 为它编写了逻辑:

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js">
</script>
<script>
    $(document).ready(function () {

        var chkAll = $("#<%= chkSelectAll.ClientID %>");

        var domCheckbox1 = chkAll[0];
        if (typeof domCheckbox1 !== "undefined") { var isChecked = domCheckbox1.checked; }


        if (isChecked == true) {
            $("#<%= GVAccounts.ClientID %>").find("[type='checkbox']").prop('checked', true);
        }
        else{
            $("#<%= GVAccounts.ClientID %>").find("[type='checkbox']").prop('checked', false);
        }
    }); 
</script>

全选代码检查了我的gridview子网格中的所有复选框(下面代码中的chkSelect):

    <asp:Label ID="lblSelectAll" runat="server" Text="Select All : "></asp:Label>
    <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" />
    <asp:GridView ID="GVAccounts" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333"
        GridLines="Horizontal"
        style="position: relative; margin-top: 10px;">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="branch" HeaderText="Branch" 
                SortExpression="branch" />
            <asp:BoundField DataField="no" HeaderText="Account" 
                SortExpression="account" />
            <asp:TemplateField HeaderText="Name" SortExpression="name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="LabelName" runat="server" Text='<%# Eval("name") + " " + Eval("surname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
            <asp:BoundField DataField="sub" HeaderText="Sub" SortExpression="sub" />
            <asp:BoundField DataField="TotalAmount" HeaderText="Hold"
                SortExpression="TotalAmount" />
            <asp:BoundField DataField="loc" HeaderText="LOC" SortExpression="loc" />

            <asp:BoundField DataField="locstatus" HeaderText="LOC status" 
                SortExpression="locstatus" />

            <asp:BoundField DataField="HoldCalc" HeaderText="Balance" SortExpression="HoldCalc" />
            <asp:BoundField DataField="odtimes" HeaderText="#OD" SortExpression="odtimes" HeaderStyle-HorizontalAlign="Center" />
            <asp:BoundField DataField="nfCount" HeaderText="/ #NF" SortExpression="nfCount" HeaderStyle-HorizontalAlign="Center" />
            <asp:TemplateField>
                <ItemTemplate>
                    <tr>

                    <td colspan="100%">
                        <div id="gridChild" style="display: inline; position: relative; left: 15px; overflow: auto">

                        <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" 
                                BorderStyle="Double"  BorderColor="#5D7B9D" Width="95%" DataKeyNames="id">

                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />

                            <RowStyle BackColor="#E1E1E1" />

                            <AlternatingRowStyle BackColor="White" />

                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />

                            <Columns>

                                <asp:TemplateField HeaderText="Select">
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true"
                                            oncheckedchanged="chkSelect_CheckedChanged" />
                                    </ItemTemplate>  
                                </asp:TemplateField>

                                <asp:BoundField DataField="effective" HeaderText="Effective" >
                                <ItemStyle Width="75px" />
                                </asp:BoundField>
                                <asp:BoundField DataField="desc_" HeaderText="Desc" >
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle Width="150px" />
                                </asp:BoundField>
                                <asp:BoundField DataField="code" HeaderText="TC" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="amount" HeaderText="Amount" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="tracer" HeaderText="Cheq #" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="empl" HeaderText="Empl" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="balance" HeaderText="OD/EXT Amt">
                                <ItemStyle Width="125px" />
                                </asp:BoundField>
                                <asp:TemplateField HeaderText="Note">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="DropDownNote" runat="server" 
                                           OnSelectedIndexChanged="DropDownNote_SelectedIndexChanged"
                                            AutoPostBack="True">
                                            <asp:ListItem></asp:ListItem>
                                            <asp:ListItem>MWC</asp:ListItem>
                                            <asp:ListItem>CBM</asp:ListItem>
                                            <asp:ListItem>Return</asp:ListItem>
                                            <asp:ListItem>TSF</asp:ListItem>
                                            <asp:ListItem>OK NO S/C</asp:ListItem>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Note1">
                                    <ItemTemplate>
                                            <asp:DropDownList ID="ddNote2" runat="server" 
                                            OnSelectedIndexChanged="DropDownNote_SelectedIndexChanged" AutoPostBack="True">
                                                <asp:ListItem Selected="True">NSF</asp:ListItem>
                                                <asp:ListItem>Funds Not Clear</asp:ListItem>
                                                <asp:ListItem>Post Dated</asp:ListItem>
                                                <asp:ListItem>Stale Dated</asp:ListItem>
                                                <asp:ListItem>Stop Payment</asp:ListItem>
                                                <asp:ListItem>Encoding Incorrect</asp:ListItem>
                                            </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Branch">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddBranch" runat="server" DataSourceID="BranchDataSource" 
                                            DataTextField="branch" DataValueField="branch" >
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="BranchDataSource" runat="server" 
                                            ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>" 
                                            SelectCommand="SELECT [branch] FROM [branch]"></asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Account">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TextNo" runat="server" ></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Type">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddType" runat="server" DataSourceID="typeSource" 
                                            DataTextField="Type" DataValueField="Type">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="typeSource" runat="server" 
                                            ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>" 
                                            SelectCommand="SELECT [Type] FROM [DMDType]"></asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Sub">
                                    <ItemTemplate><asp:TextBox ID="TextSub" Width="25px" runat="server"></asp:TextBox></ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="ID" Visible="false">
                                    <ItemTemplate><asp:TextBox ID="ID" Width="25px" runat="server" Text ='<% Eval("id") %>'></asp:TextBox></ItemTemplate>
                                </asp:TemplateField>

                            </Columns>
                            <RowStyle HorizontalAlign="Center" />
                        </asp:GridView>

                        </div> 
                    </td>

                    </tr>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

当我单击全选复选框时,它会选择 gridview 中的复选框,但不会触发 gridview 中复选框的后端逻辑。逻辑是当一个复选框被选中时,它应该启用gridview中的其他控件,代码如下:

Protected Sub chkSelect_CheckedChanged(sender As Object, e As EventArgs)

    For Each row As GridViewRow In GVAccounts.Rows

        cmd = New SqlCommand("select submitted, id, effective FROM DmdOD where no = @no and entered = '" & ddDate.SelectedItem.Text & "' order by effective, time_, seqno_dmddmhi_dmhi", conn)
        cmd.Parameters.Add(New SqlParameter("@no", row.Cells(1).Text))

        Dim strRead As StringReader = New StringReader(Session("xml").ToString)

        Dim xml As XmlTextReader = New XmlTextReader(strRead)
        Dim gvChild As GridView = DirectCast(row.FindControl("gvChildGrid"), GridView)

        For Each childrow As GridViewRow In gvChild.Rows
            If chk.Checked Then

                childrow.Cells(8).Enabled = True
                childrow.Cells(9).Enabled = True
                childrow.Cells(10).Enabled = True
                childrow.Cells(11).Enabled = True
                childrow.Cells(12).Enabled = True
                childrow.Cells(13).Enabled = True

                LoadXMLFromSession(childrow, hidden)
            Else

                childrow.Cells(8).Enabled = False
                childrow.Cells(9).Enabled = False
                childrow.Cells(10).Enabled = False
                childrow.Cells(11).Enabled = False
                childrow.Cells(12).Enabled = False
                childrow.Cells(13).Enabled = False

                LoadXMLFromSession(childrow, hidden)

                'Clear out values when not selected
                ddNoteList.SelectedIndex = 0
                ddlNote.SelectedItem.Selected = False
                ddlNote.Items.FindByValue("").Selected = True
                ddBranchBox.SelectedIndex = 0
                txtAccountBox.Text = ""
                ddTypeBox.SelectedIndex = 0
                txtSubBox.Text = ""

                LoadDefaultValues()

                WriteXML()

                'LoadXMLFromSession(childrow, hidden)
            End If
        Next
    Next

    'CheckForSubmit()
End Sub

为了让它正常运行,我缺少什么?

【问题讨论】:

  • 客户端代码不行吗?
  • 试试 $("#").find("[type='checkbox']").prop('checked', true).trigger("change ");如果有效,请在“已选中,错误”处执行相同操作
  • 客户端代码工作正常,当我单击全选时,所有复选框都会被选中。问题出在后端代码
  • @JoaoPaulo 遗憾的是这没用 :(
  • 尝试将“.prop('checked', true)”改为“.click()”

标签: jquery asp.net vb.net gridview checkbox


【解决方案1】:

chkSelect_CheckedChanged 是一个 asp.net 函数,当您使用 jquery 选择复选框时,它不会被调用。

如果你想从 jquery 调用 CodeBehind 方法,那么你可以使用Ajax 和PageMethods:

http://devarchive.net/sample_calling_page_methods_from_javascript_using_ajax_net.aspx

Using jQuery to directly call ASP.NET AJAX page methods -- 500 (Internal Server Error)

【讨论】:

  • 查看我的代码,我正在为我的一个 asp.net 按钮做一些事情,我正在使用 OnClientClick= 并调用一个命名的 javascript 函数,在这种情况下会起作用吗?
  • 当Jquery中的所有复选框都被选中时,你想运行chkSelect_CheckedChanged中包含的每一行代码,是吗?
  • 谢谢,我试试看,告诉你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 2018-07-30
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多