方法不会在文本框文本更改时触发

时间:2013-10-15 14:25:09

标签: c# asp.net

我有一个带有压延扩展器的文本框:

<asp:TextBox ID="customDateTo" runat="server" AutoPostBack="true" OnSelectionChanged="customDateFrom_SelectionChanged"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="toCalendarExtender" TargetControlID="customDateTo" runat="server"></ajaxToolkit:CalendarExtender>

如您所见,我为文本框分配了AutoPostBack="true"OnSelectionChanged="customDateFrom_SelectionChanged

然而,在我的方法中没有任何响应:

protected void customDateFrom_SelectionChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Do Something");
}

当我更改文本时,我确实得到了回发,但方法中没有任何内容正在执行。为什么会发生这种情况?如何解决?

3 个答案:

答案 0 :(得分:8)

对于 TextBox ,我认为应该是;

OnTextChanged="customDateFrom_SelectionChanged"

OnSelectionChanged="customDateFrom_SelectionChanged"

答案 1 :(得分:3)

我假设你想要处理TextBox'TextChanged事件。 SelectionChanged是一个winforms事件,在ASP.NET中不存在。

<asp:TextBox ID="customDateTo" runat="server" 
   AutoPostBack="true" OnTextChanged="customDateFrom_TextChanged">
</asp:TextBox>

当用户更改其中的文本并离开TextBox时会引发此事件。

protected void customDateFrom_TextChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Do Something");
}

不触发事件的另一个原因是,如果您在触发事件之前对TextBox进行了数据绑定(f {。在Page_Load中)。然后,您应该检查IsPostBackif(!IsPostBack)DataBindTextBox();

答案 2 :(得分:1)

您应该处理TextBox.TextChanged Event ..

示例:

 <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" 
            ontextchanged="TextBox1_TextChanged">

//服务器端事件:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   Label1.Text = Server.HtmlEncode(TextBox1.Text);
}

参见: Asp.Net textbox TextChanged event is not firing