简体   繁体   中英

Adding custom OnTextChange event handler on custom TextBox

I currently have a custom text box, it implements majority of the base implementations a normal text box has, by doing something like:

    public string Text
    {
        get { return customTextBox.Text; }
        set { customTextBox.Text = value; }
    }

I now want to implement a custom event handler to get a postback on text changed. I currently am doing the following is this correct if not then how should I go about this:

    private static readonly object EventCustomTextChanged = new Object();

    public event EventHandler TextChanged
    {
        add
        {
            Events.AddHandler(EventCustomTextChanged, value);
        }
        remove
        {
            Events.RemoveHandler(EventCustomTextChanged, value);
        }
    }

This implementation comes from: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.ontextchanged.aspx

Before someone says why don't you use the one thats already there, I will tell you it's not implented because this is a custom user control. I am trying to implement it.

Thanks in Advanced!

This is the way to do it:

public event EventHandler TextChanged
{
    add { customTextBox.TextChanged += value; }
    remove { customTextBox.TextChanged -= value; }
}

Assuming that customTextBox is the System.Web.UI.WebControls.TextBox control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM