Validation Error asp c #

1

I commented that I am doing as a validation on my form, where it says lbldiasfaltantes is saving a value any number, it can be from 1 to -31 and I want it if in my label is < = 5 the fields are enabled and if they are greater they are disabled.

That's my code but I get an underlined error of red

if ((lbldiasfaltantessss.Text) <= 5)
{
    TxtMontoPagado.Enabled = true;
    TxtFecha.Enabled = true;
    BtnActualizar.Enabled = true;
    BtnBuscar.Enabled = true;
    BtnnoConforme.Enabled = true;
    dprAgencia.Enabled = true;
    dprTarea.Enabled = true;
    lblagencia.Enabled = true;
    lblTarea.Enabled = true;
    lblagencia.Enabled = true;
    txtNumeroRecibo.Enabled = true;
    TxtFechaPago.Enabled = true;
}
else if ((lbldiasfaltantessss.text) > 5)
{

    TxtMontoPagado.Enabled = false;
    TxtFecha.Enabled = false;
    BtnActualizar.Enabled = false;
    BtnBuscar.Enabled = false;
    BtnnoConforme.Enabled = false;
    dprAgencia.Enabled = false;
    dprTarea.Enabled = false;
    lblagencia.Enabled = false;
    lblTarea.Enabled = false;
    lblagencia.Enabled = false;
    txtNumeroRecibo.Enabled = false;
    TxtFechaPago.Enabled = false;
}
    
asked by PieroDev 15.03.2017 в 18:24
source

3 answers

5

As you are well told in the comments, the .Text property of the Label controls in asp.net they are of type string so in order to make any type of numerical comparison in this regard, you must first do some type of casting or parsing of this string . I recommend you do the following:

    //Definir una variable de tipo entero sobre la cual se 
    //establecera la cantidad de dias faltantes obtenidos si es 
    //que el usuario ingresa un valor convertible a int32
    int diasFaltantes = 0;

    //TryParse lo que hara es justamente intentar parsear el texto de lbldiasfaltantessss.Text
    //a un int32 y setearlo en la variable diasFaltantes, que establecimos en la funcion como
    //parametro de salida con la palabra reservada out            
    int.TryParse(lbldiasfaltantessss.Text, out diasFaltantes);


    //En caso de que la conversion anterior no fuera posible, el sistema no 
    //arrojará ninguna excepcion y a su vez el valor de la variable diasFaltantes seguira en 0
    //mientras que si lo puede parsear, el valor parseado correspondiente estará en la variable diasFaltantes
    if (diasFaltantes<=5)
    {
        //Aquí va el código para HABILITAR los campos
    }
    else
    {
        //Aquí va el código para DESHABILITAR los campos
    }
    
answered by 15.03.2017 / 18:49
source
2

Dear, your code should be as shown below. Greetings.

if (int.Parse(lbldiasfaltantessss.Text) <= 5)
{
    TxtMontoPagado.Enabled = false;
    TxtFecha.Enabled = false;
    BtnActualizar.Enabled = false;
    BtnBuscar.Enabled = false;
    BtnnoConforme.Enabled = false;
    dprAgencia.Enabled = false;
    dprTarea.Enabled = false;
    lblagencia.Enabled = false;
    lblTarea.Enabled = false;
    lblagencia.Enabled = false;
    txtNumeroRecibo.Enabled = false;
    TxtFechaPago.Enabled = false;
}
    
answered by 15.03.2017 в 19:23
2

You only need to parse the label that is of type string to integer type to make your comparison:

if ( int.Parse(lbldiasfaltantessss.Text) <= 5)
{ 
    //habilitados 
} else 
{ 
    //inhabilitados 
}
    
answered by 15.03.2017 в 18:50