2014-01-29 54 views
-1

在下面的代碼中,我一直在處理asp.net web應用程序。在我的情況下,textchange事件不會觸發。請幫助我解決問題。 代碼:TextChanged事件不在web應用程序中觸發

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> 
+0

的可能的複製[TextChanged事件功能無法正常工作(http://stackoverflow.com/questions/5029804/textchanged-event-function-not-working) – hellyale

回答

0

移動事件綁定線

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

頁面的OnInit事件處理程序。

+0

當我調試它不點火的textchange事件 – user3224577

2

設置AutoPostBack="true"做回發。你最好不要這樣做,因爲它可能會導致不必要的回傳。

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

是否有可能在textbox中textchanged應該移動到該方法。 – user3224577

+0

僅當您退出控件時,鍵入文本框纔會觸發回發。 您可以使用客戶端「onclick」事件處理程序,然後啓動一個「__doPostback」,但這會在打字時造成相當大的開銷,因爲您會在回發運行時阻止用戶輸入。 你想達到什麼目的? –

+0

@Chris Hammond當用戶在文本框中鍵入並離開焦點時,它應該移動到該方法txtField_Leave – user3224577

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

必須爲此TextBox1_TextChanged設置什麼事件 – user3224577

相關問題