简体   繁体   English

检查是否选中了GridView中的复选框

[英]check if checkboxes in GridView are ticked

I have three checkboxes in a GridView but the user can only select one checkbox. 我在GridView中有三个复选框,但是用户只能选择一个复选框。 So if they select the first checkbox, I need to alert them them that they cannot select the other two. 因此,如果他们选择第一个复选框,我需要提醒他们他们不能选择其他两个复选框。

        <asp:GridView CssClass="tblResults" runat="server" ID="dgDetails" 
              OnRowDataBound="dgDetails_ItemDataBound" 
              DataKeyField="ID" AutoGenerateColumns="false"
             AlternatingRowStyle-BackColor="#EEEEEE">
             <HeaderStyle CssClass="tblResultsHeader" />
                <Columns> 
                    <asp:TemplateField HeaderText="Approved1">
                        <ItemTemplate>
                           <asp:CheckBox runat="server" ID="chkApproved1" />
                        </ItemTemplate>
                    </asp:TemplateField>
                   <asp:TemplateField HeaderText="Approved2">
                        <ItemTemplate>
                           <asp:CheckBox runat="server" ID="chkApproved2" />
                        </ItemTemplate>
                    </asp:TemplateField>
                   <asp:TemplateField HeaderText="Approved3">
                        <ItemTemplate>
                           <asp:CheckBox runat="server" ID="chkApproved3" />
                        </ItemTemplate>
                    </asp:TemplateField>
                  </Columns>
          </asp:GridView> 

code behind: 后面的代码:

protected void dgDetails_ItemDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv = sender as GridView;
            Quote.QuoteDetails qd = e.Row.DataItem as Quote.QuoteDetails;

            CheckBox chkApproved1 = e.Row.FindControl("chkApproved1") as CheckBox;
            CheckBox chkApproved2 = e.Row.FindControl("chkApproved2") as CheckBox;
            CheckBox chkApproved3 = e.Row.FindControl("chkApproved3") as CheckBox;
       }
    }

I tried using something like this in query: 我尝试在查询中使用类似这样的内容:

$('#<%= chkApproved1.ClientID %>').change(function () {
                if($(this).is(":checked")) {

                    }  
            });

But that causes the error:The name 'chkApproved1' does not exist in the current context. 但这会导致错误:名称“ chkApproved1”在当前上下文中不存在。

So how can I check if the checkboxes in the GridView have been ticked? 那么,如何检查GridView中的复选框是否已选中?

if you want to give alert if first checkbox is checked use the below script 如果您想在选中第一个复选框时发出警报,请使用以下脚本

    $(document).ready(function () {       

        $("table[id$='dgDetails']").find("input[id*='chkApproved1']").change(function () {
                    if ($(this).is(":checked")) {
                        alert('Hi');
                    }
                });
     });

Here is the code that will help you to prevent other checkbox to check if first is checked. 这是将帮助您防止其他复选框检查是否首先选中的代码。

              if ($(this).is(":checked")) {                    
                    if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 1 }
                    if ($(this).attr("id").indexOf("chkApproved2") != -1) { var2 = 1 }
                    if ($(this).attr("id").indexOf("chkApproved3") != -1) { var3 = 1 }
                    if (var1 == 1 && var2 == 1 && var3 == 1) {
                        var2 = 0; var3 = 0;
                        $("table[id$='dgDetails']").find("input[id*='chkApproved2']").attr("checked", false);
                        $("table[id$='dgDetails']").find("input[id*='chkApproved3']").attr("checked", false);
                    }
                    else if (var1 == 1 && var2 == 1) {
                        if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 0 }
                        if ($(this).attr("id").indexOf("chkApproved2") != -1) { var2 = 0 }
                        alert('You can\'t select this checkbox2');
                        $(this).attr("checked", false);
                    }
                    else if (var1 == 1 && var3 == 1) {
                        if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 0 }
                        if ($(this).attr("id").indexOf("chkApproved3") != -1) { var3 = 0 }
                        alert('You can\'t select this checkbox3');
                        $(this).attr("checked", false);
                    }
                }
                else {
                    if ($(this).attr("id").indexOf("chkApproved1") != -1) { var1 = 0 }
                    if ($(this).attr("id").indexOf("chkApproved2") != -1) { var2 = 0 }
                    if ($(this).attr("id").indexOf("chkApproved3") != -1) { var3 = 0 }
                }

Hope this help 希望这个帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM