簡體   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