简体   繁体   中英

TextChanged event is not firing in web application

In the below code i have been working on asp.net web application .in my case the textchange event is not firing.Pls help me to solve the issue. code:

public delegate void LeavingFocusHandler(int CurrentIndex);
public event LeavingFocusHandler LeavingFocus;

public string strValue { get; set; } 
public int ItemIndex { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    this.txtField.TextChanged += new EventHandler(txtField_Leave);
}

void txtField_Leave(object sender, EventArgs e)
{
    try
    {
        this.strValue = txtField.Text;

        if (this.LeavingFocus != null)
        {
            this.LeavingFocus(this.ItemIndex);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Design code:

<asp:TextBox ID="txtField" runat="server"></asp:TextBox>

Set AutoPostBack="true" to do the postback. You better do not do that as It may cause unnecessary postbacks.

<asp:TextBox ID="txtField" runat="server" AutoPostBack="true"></asp:TextBox>

Move the event binding line

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.txtField.TextChanged += new EventHandler(txtField_Leave);
    }

to the page oninit event handler.

Try like this

You need to set AutoPostBack property to True for firing TextChange event

Design code:

<asp:TextBox ID="txtField" runat="server" AutoPostBack="true"></asp:TextBox>

Code Behind

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   try
    {
        this.strValue = txtField.Text;

        if (this.LeavingFocus != null)
        {
            this.LeavingFocus(this.ItemIndex);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

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