codice:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
args.IsValid = Not (Me.TextBox1.Text.Trim = String.Empty And Me.TextBox2.Text.Trim = String.Empty And Me.TextBox3.Text.Trim = String.Empty)
Me.Label1.Text = ""
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Validate()
If Me.IsValid Then
Me.Label1.Text = "Controllo validità superato"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function controllo_textbox(sender, args)
{
var t1 = document.getElementById("TextBox1");
var t2 = document.getElementById("TextBox2");
var t3 = document.getElementById("TextBox3");
args.IsValid = !(trim(t1.value) == "" && trim(t2.value) == "" && trim(t3.value) == "");
}
function trim(stringa)
{
stringa = stringa + "";
return stringa.replace(/^ */,"").replace(/ *$/,"");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Deve essere valorizzato almeno un campo" ClientValidationFunction="controllo_textbox" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</div>
</form>
</body>
</html>