繁体   English   中英

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

[英]TextChanged event is not firing in web application

在下面的代码中,我一直在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>

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

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

移动事件绑定线

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

到页面oninit事件处理程序。

这样尝试

您需要将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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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