繁体   English   中英

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

[英]Method not firing on textbox text change

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

<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为文本框分配的OnSelectionChanged="customDateFrom_SelectionChanged

但是我的方法没有响应:

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

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

对于TextBox ,我认为它应该是;

OnTextChanged="customDateFrom_SelectionChanged"

OnSelectionChanged="customDateFrom_SelectionChanged"

我假设你想要处理TextBoxTextChanged事件。 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进行数据绑定(在Page_Load为fe)。 然后你应该检查IsPostBackif(!IsPostBack)DataBindTextBox();

你应该处理TextBox.TextChanged事件 ..

例:

 <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文本框TextChanged事件未触发

暂无
暂无

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

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