如何检查至少一个复选框?

时间:2012-03-12 09:46:29

标签: c# asp.net gridview

我有ascx页面,我有一个网格,我已经添加了单选按钮,我想检查至少应该检查一个复选框,然后再卸载页面或点击btn save。

<asp:GridView ID="grdTest" runat="server" DataKeyNames="Id" AutoGenerateColumns="False" HorizontalAlign="Center" GridLines="None" onrowcreated="grdTest_RowCreated">
  <asp:TemplateField >
      <ItemTemplate>
        <asp:TextBox ID="txtText" Rows="10" Columns="40" runat="server" TextMode="MultiLine"   Height="55px" Text='<%# Eval("Text") %>'></asp:TextBox>

        <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Save"/>            
        <span id="checkbox">
        <asp:RadioButton ID="rbtnTest"  runat="server" Checked='<%# Eval("Correct") %>' TextAlign="Left"  Height="28px" />
        </span>
      </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtText" Rows="10" Columns="40" runat="server" TextMode="MultiLine" />
        <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Save"/>
        <asp:CheckBox ID="rbtnTest" runat="server"  Enabled="true" TextAlign="Left"/>
    </EditItemTemplate>
  </asp:TemplateField>
</asp:GridView>

这有什么可能帮助我?

1 个答案:

答案 0 :(得分:2)

<script type="text/javascript" language="javascript">
function Validate_Checkbox()
{

    var chks=document.getElementsByTagName('input');   
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++)
        {
            if (chks[i].checked)
                {
                    hasChecked = true;
                    break;
                }
        }
      if (hasChecked == false)
            {
                alert("Please select at least one checkbox..!");

                return false;
            }

            return true;
}     
</script>

在提交按钮上你必须写

OnClientClick="return Validate_Checkbox()"