prova questo codice, collaudato sia con javascript attivo che disattivo.
pagina aspx
codice:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="a.aspx.vb" Inherits="corso_apogeo.a"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>a</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
<!--
function ControllaCheck(sender, args)
{
if(!document.getElementById("CheckBox1").checked && !document.getElementById("CheckBox2").checked)
args.IsValid = false;
else
args.IsValid = true;
}
//-->
</script>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:CheckBox id="CheckBox1" runat="server" Text="CheckBox1"></asp:CheckBox>
<asp:CheckBox id="CheckBox2" runat="server" Text="CheckBox2"></asp:CheckBox></P>
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="Bisogna selezionare almento un elemento"
ClientValidationFunction="ControllaCheck"></asp:CustomValidator></P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:Label id="lblStatus" runat="server"></asp:Label></P>
<asp:ValidationSummary id="ValidationSummary1" runat="server"></asp:ValidationSummary></P>
</form>
</body>
</HTML>
codice sottostante
codice:
Public Class a
Inherits System.Web.UI.Page
#Region " Codice generato da Progettazione Web Form "
'Chiamata richiesta da Progettazione Web Form.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
Protected WithEvents CheckBox2 As System.Web.UI.WebControls.CheckBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents CustomValidator1 As System.Web.UI.WebControls.CustomValidator
Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
'NOTA: la seguente dichiarazione è richiesta da Progettazione Web Form.
'Non spostarla o rimuoverla.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: questa chiamata al metodo è richiesta da Progettazione Web Form.
'Non modificarla nell'editor del codice.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Inserire qui il codice utente necessario per inizializzare la pagina
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'forza la validazione ti tutti i controlli
Me.Validate()
If Not Me.IsValid() Then
Dim ctrl As BaseValidator
Dim errorCount%
For Each ctrl In Me.Validators
If Not ctrl.IsValid Then errorCount += 1
Next
Me.lblStatus.Text = "Ci sono " & (errorCount) & " errori"
End If
End Sub
Private Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If (Me.CheckBox1.Checked = False And Me.CheckBox2.Checked = False) Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub
End Class