-1

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>
Abbas
  • 14,186
  • 6
  • 41
  • 72
user3224577
  • 37
  • 1
  • 1
  • 9
  • Possible duplicate of [TextChanged event function not working](http://stackoverflow.com/questions/5029804/textchanged-event-function-not-working) – M Y May 17 '17 at 16:37

3 Answers3

2

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>
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Is it possible when textchanged in the textbox it should move to that method. – user3224577 Jan 29 '14 at 09:33
  • Typing into a textbox will not fire the postback, only when you exit the control. You could use a client side "onclick" eventhandler that then initiates a "__doPostback" but this would put quite a significant overhead on typing as you would block the user typing whilst the postback was running. What are you trying to achieve? – Chris Hammond Jan 29 '14 at 09:34
  • @Chris Hammond when user types in the text box and leave the focus it should move to that method txtField_Leave – user3224577 Jan 29 '14 at 09:42
  • So what you are saying is that you need to do something as soon as you leave the text box, then you need to add `AutoPostBack="true"`. I am not sure you would then need to bind specific event handlers as ASP.Net will automatically issue a postback for you – Chris Hammond Jan 29 '14 at 09:50
0

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.

Karan
  • 3,265
  • 9
  • 54
  • 82
0

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;
    }
}
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115