2009-06-03 11 views

Respuesta

7

Necesitaría un CustomValidator para lograr eso.

Aquí hay algunos códigos que demuestran el uso básico. El texto del validador personalizado se mostrará después de invocar a IsValid en la devolución de llamada de envío y se mostrará un texto de la llamada Response.Write.

ASPX Código

<asp:TextBox runat="server" ID="tb1" /> 
    <asp:TextBox runat="server" ID="tb2" /> 

    <asp:CustomValidator id="CustomValidator1" runat="server" 
     OnServerValidate="TextValidate" 
     Display="Dynamic" 
     ErrorMessage="One of the text boxes must have valid input."> 
    </asp:CustomValidator> 

    <asp:Button runat="server" ID="uxSubmit" Text="Submit" /> 

Detrás

protected void Page_Load(object sender, EventArgs e) 
    { 
     uxSubmit.Click += new EventHandler(uxSubmit_Click); 
    } 

    void uxSubmit_Click(object sender, EventArgs e) 
    { 
     Response.Write("Page is " + (Page.IsValid ? "" : "NOT ") + "Valid"); 
    } 

    protected void TextValidate(object source, ServerValidateEventArgs args) 
    { 
     args.IsValid = (tb1.Text.Length > 0 || tb2.Text.Length > 0); 
    } 
7

Pruebe CustomValidator.

Tendrá que crear un método que realiza lo siguiente para controlar el evento ServerValidate:

void ServerValidation (object source, ServerValidateEventArgs args) 
{ 
    args.IsValid = TextBox1.Text.Length > 0 || TextBox2.Text.Length > 0; 
} 
+0

Probablemente se podría simplificar a que args.IsValid = (+ TextBox1.Text TextBox2.Text) .Length> 0 o algo así. –

+0

@Fredrik: Debe agregar las longitudes juntas, no el texto en sí. Eso lo hace más claro. Aunque creo que el camino de Lance es más claro aún. – Brian

+0

Sí, ¡gracias por la limpieza! –

3

Además de crear validación del lado del servidor, puede utilizar la propiedad ClientValidationFunction en el CustomValidator para proporcionar cliente junto a la validación. Eso podría ser algo como esto:

function(sender, args) { 
    args.IsValid = document.getElementById('<%=TextBox1.ClientID%>').value != '' 
        || document.getElementById('<%=TextBox2.ClientID%>').value != ''; 
} 
-1

OnClientClick de su botón o lo somete su llamada página de una función de JavaScript como este

function valtxtbox(){ 


if (document.getElementById('<%=TextBox1.ClientID%>').value== '' && document.getElementById('<%=TextBox2.ClientID%>').value== '') 
{ 

alert('You must enter in data!'); 
return false; 
} 
+0

aquí debería poner Y condicionar en lugar de O –

Cuestiones relacionadas