Jump to content

Ajuda com customValidator.


ki_ko
 Share

Recommended Posts

Tenho gridview onde listo tudo o que tenho numa tabela, depois dou a possibilidade de alterar e editar, e quando edito coloquei uma textbox e um customvalidator que vai a tabela ver se existe um campo ja com  o mesmo nome e se existir dispara o customValidator.

mas nao tou a conseguir por isto a funcionar, nem sequer quando carrego no update  ele entra na funcao do custamValidator, alguem me pode ajudar?

deixo em baixo o meu codigo:

funcao

protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
    {
        LabelTESTE.Text = args.Value.ToString();
         conexao = new SqlConnection();

        conexao.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TravelGuide;Data Source=.\\SQLEXPRESS;";

        conexao.Open();

        

        try
        {


            SqlCommand comando = new SqlCommand("Select Cod_aeroporto from T_Aeroporto where Cod_aeroporto='"+args.Value.ToString()+"' ", conexao);

            
            if ((String.Compare(comando.ExecuteScalar().ToString(), null)) != 0)
            {
               args.IsValid=false;
               LabelTESTE.Text = "JA EXISTENTE";
            }
           
        }
        catch (Exception ex)
        {

           args.IsValid=true;
           LabelTESTE.Text = "OK";
        }

    
    }

editar na gridview

<EditItemTemplate>
                                 <asp:TextBox ID="TextBoxEditCodArp" runat="server" MaxLength="3"></asp:TextBox><br />
                                 <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                                    ControlToValidate="TextBoxEditCodArp" ErrorMessage="expressao invalida, deve conter 3 caracteres(A-Z) maiusculos."
                                    ValidationExpression='^[A-Z]{3,3}$'></asp:RegularExpressionValidator><br />
                                <asp:CustomValidator ID="CustomValidatorCodArpEditar" runat="server" ControlToValidate="TextBoxEditCodArp"
                                    ErrorMessage="Erro ja existe arp com esse codigo" OnServerValidate="CustomValidator1_ServerValidate1"></asp:CustomValidator>
                            </EditItemTemplate>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.