Puede utilizar validadores personalizados para esto:
<asp:Textbox id="textbox1" runat="server" />
<asp:CustomValidator id="valCustom" runat="server"
ControlToValidate="textbox1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidate"
ErrorMessage="*You can only enter 1" display="dynamic">*
</asp:CustomValidator>
<asp:Textbox id="textbox2" runat="server" />
<asp:CustomValidator id="valCustom2" runat="server"
ControlToValidate="textbox2"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidate"
ErrorMessage="*You can only enter 1" display="dynamic">*
</asp:CustomValidator>
<script language="Javascript">
function ClientValidate(source, arguments)
{
var tb1 = document.getElementById("<%=textbox1.ClientID %>").value;
var tb2 = document.getElementById("<%=textbox2.ClientID %>").value;
if (tb1 && tb2 || (!tb1 && !tb2)){
arguments.IsValid = false;
} else {
arguments.IsValid = true;
}
}
</script>
del lado del servidor:
protected void ServerValidate(object sender, ServerValidateEventArgs args)
{
if(string.isNullOrEmpty(textbox1.Text) && string.isNullOrEmpty(textbox2.Text))
args.IsValid = false;
else if(!string.isNullOrEmpty(textbox1.Text) && !string.isNullOrEmpty(textbox2.Text))
args.IsValid = false;
else
args.IsValid = true;
}
Si estás usando jQuery favor comente ... todo esto puede ser mucho más limpio.
Sólo tenga cuidado si copia el código de ejemplo - "dispaly" debería ser "pantalla" (aparece dos veces) – DJDave
Creo que esto no funciona si ambos están vacíos: si el cuadro de texto está vacío, su validador no se activa . – ChrisW
Me parece que para que esto funcione (en el lado del servidor) cuando ambos están vacíos, necesito usar una sola instancia 'CustomValidator' sin ninguna propiedad' ControlToValidate'. – ChrisW