Protected Overide - Custom TextBox

0

I have a problem with a custom TextBox control that I created and to which I added several events, among which is the KeyPress in which I used currency format, until then everything works fine.

My problem is that when I want to add specific validations to the control in the projects that inherit from it, it is as if I do not recognize the events created in the control.

Eg: the control validates that the entered string is numeric: Ok

A part if I want to validate that it does not contain the number 5 in the project where it inherits it, it does not recognize it.

public class CajaDeTexto : TextBox
{
 protected  override void OnKeyPress(KeyPressEventArgs e)
        {
            try
            {
                if (IsEnter(e))
                {
                    this.SelectionStart = this.Text.Length;
                    Tab();
                }
                if (EJ_Mayúsculas) { KeyPressMayusculas(e); }
                if (EJ_SoloNumero) { KeyPressNumeros(ref e); }
                if ((EJ_Dinero) | (EJ_ValorMinimo != 0 | EJ_ValorMaximo != 0) | (EJ_Decimales)) { KeyPressDinero(ref e); }
                if (EJ_Cedula) { KeyPressIdentificacion(ref e); }
            }
            catch (Exception ex)
            { throw ex; }

        }
}

En el proyecto donde uso el control, no reconoce algo como esto:

private void cajaDeTexto1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Ejemplo
           if(e.KeyChar ==(char)Keys.Enter)
            {
                Guardar();
            }
        }
    
asked by sergio badillo 29.11.2016 в 17:43
source

2 answers

0

An alternative solution is to handle the event on the client side.

In the declaration of your TextBox put the event onkeypress="return EnterKey(event)" so it looks something like this:

<asp:TextBox runat="server" ID="txtBuscar" onkeypress="return EnterKey(event)"></asp:TextBox>

And at the beginning of your page delcarar the function EnterKey(evt) in a code segment of JavaScript so that it looks something like this:

<script>
    function EnterKey(evt) {
        if (window.event.keyCode == 13)
            __doPostBack('<%=cajaDeTexto1.UniqueID%>', "");
        return true;
    }
</script>
    
answered by 29.11.2016 в 18:37
0

Sergio, I would swear that the problem is that you are forgetting to call the implementation of the OnKeyPress() method of the base class, which is the one that will invoke all the subscribers of the KeyPress event. As you have it, the only validation that will be carried out is the one that you have implemented in CajaDeTexto .

Modify your OnKeyPress implementation and add:

base.OnKeyPress(e);
    
answered by 12.12.2016 в 13:15