TextChanged事件未在Web应用程序中触发

时间:2014-01-29 09:09:03

标签: c# asp.net textchanged

在下面的代码中,我一直在使用asp.net web应用程序。在我的情况下,textchange事件没有触发。请帮助我解决问题。 代码:

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;
    }
}

设计代码:

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

3 个答案:

答案 0 :(得分:2)

设置AutoPostBack="true"进行回发。你最好不要这样做,因为它可能导致不必要的回发。

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

答案 1 :(得分:0)

移动事件绑定行

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

到页面oninit事件处理程序。

答案 2 :(得分:0)

试试这个

您需要将AutoPostBack属性设置为True才能触发TextChange事件

  

设计代码:

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

背后的代码

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;
    }
}