updatepanel 内的文本框出现问题 - 未导致 OnTextChanged 事件
我有以下情况:我在 ajax updatepanel 中有一个文本框。 无论用户在文本框中键入内容,我都必须显示一条消息(不同的消息取决于用户键入的数据)。
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:TextBox ID="txtMyTexbox" runat="server" Width="500px" OnTextChanged="txtMyTexbox_TextChanged" AutoPostBack="true"></asp:TextBox>
<br />
<asp:Label ID="lblMessage" runat="server" CssClass="errorMessage" Visible="false">Hello World</asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtMyTexbox" />
</Triggers>
</asp:UpdatePanel>
在服务器端,我在页面加载时编写了以下内容
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtMyTexbox);
以及这样的方法
protected void txtMyTexbox_TextChanged(object sender, EventArgs e)
{
if (.....)
{
lblMessage.Visible = false;
}
else
{
lblMessage.Visible = true;
}
}
我现在的问题是:当用户在文本框中键入时,它不会导致 OnTextChanged 事件。
我错过了什么吗?
I have the following situation: I have a textbox inside an ajax updatepanel. Wherever the user types in the textbox I must display a message (different message that depends on the user typed data).
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:TextBox ID="txtMyTexbox" runat="server" Width="500px" OnTextChanged="txtMyTexbox_TextChanged" AutoPostBack="true"></asp:TextBox>
<br />
<asp:Label ID="lblMessage" runat="server" CssClass="errorMessage" Visible="false">Hello World</asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtMyTexbox" />
</Triggers>
</asp:UpdatePanel>
In server side I have written the following at page load
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtMyTexbox);
and the method like this
protected void txtMyTexbox_TextChanged(object sender, EventArgs e)
{
if (.....)
{
lblMessage.Visible = false;
}
else
{
lblMessage.Visible = true;
}
}
My problem now is that: when the user types in the textbox it doesn't cause OnTextChanged event.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不确定您的问题与
UpdatePanel
有什么关系。事实上,
TextChanged
事件在输入时不会触发。 它只会在文本框失去焦点后触发。 如果AutoPostBack
设置为True
,或者在下一次回发发生时,就会直接发生这种情况。 请参阅 AutoPostBack 属性<的文档/a> 和 TextChanged 事件。Afaik,你最好的选择可能是在 JavaScript 中处理 keyup 事件。
这是一个简单的 jQuery 示例:
I'm not sure that your problem has anything to do with the
UpdatePanel
.In fact, the
TextChanged
event doesn't fire while typing. It will only fire after the textbox loses focus. This happens directly ifAutoPostBack
is set toTrue
, or when the next postback occurs. Please see the docs for the AutoPostBack property and the TextChanged event.Afaik, your best bet is probably to handle the keyup event in javascript.
Here's a simple jQuery example:
将 txtMyTexbox
AsyncPostBackTrigger
的EventName
属性设置为 TextChanged其他建议:
您是否尝试过查看 AutoComplete 控件是 AjaxControlToolKit 的一部分吗? 它的行为方式与您希望解决方案的行为方式相同。
Set the
EventName
property for your txtMyTexboxAsyncPostBackTrigger
to TextChangedOther sugguestion:
Have you tried looking at the AutoComplete control that is part of the AjaxControlToolKit? Its behaves the same way you want your solution to behave.
值得一提的是,即使添加更新面板/ AsyncPostBackTrigger 后, TextBox ChangeEvent 也无法正常工作。 有时它有效,有时则无效。由于它是异步调用,我们需要一些时间刷新,或者等待或不可预测,希望微软能拿出一个称职的方法。下面是检查用户名的简单方法
- ----- 在 Page_Load 下 - aspx.cs --------------------------
this.TextBox1.Attributes.Add("onKeyUp", "fnUNameSubmit(this );");
-------在 aspx -add 脚本中 -------------------------------------- -
----------在aspx中添加脚本------------------------------------ --
----------------aspx.cs -------------------
if (TextBox1.Text.Length > 21)
{
检查用户名();
Label2.Text = "";
}
别的
{
Label2.Text = "长度小于21"; //让我们做一些事情..bla..bla
}
------------------------------------------------- 检查用户名()
公共无效 CheckUsrName()
{
its strnage to know that even after adding update panel / AsyncPostBackTrigger , TextBox ChangeEvent doesn't work properly. Some time its works and some times it not..Since its is Asychronous call, we need to some time refresh, or wait or unpredictable , Hopes microsoft will come up with competent one.. Below are easy way to check user name pretty good
------ Under Page_Load - aspx.cs -----------------------
this.TextBox1.Attributes.Add("onKeyUp", "fnUNameSubmit(this);");
-------in aspx -add script ---------------------------------------
-------in aspx -add script ---------------------------------------
----------------aspx.cs -------------------
if (TextBox1.Text.Length > 21)
{
CheckUsrName();
Label2.Text = "";
}
else
{
Label2.Text = "Length is less than 21"; //lets do some stuff..bla..bla
}
------------------------------------------------- CheckUsername()
public void CheckUsrName()
{
您不应该对 TextBox 使用 RegisterAsyncPostBackControl。 该方法实际上仅适用于驻留在 UpdatePanel 之外的控件。 我会尝试删除该行代码并看看会发生什么。
有关详细信息,请参阅:http:// msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx
You should not be using RegisterAsyncPostBackControl for your TextBox. That method is really only for use for controls that reside outside of UpdatePanels. I would try removing that line of code and seeing what happens.
See this for more info: http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx
解决方法是检查文本框 - Causesvalidation 属性并将其设置为 true
a workaround check textbox - causesvalidation property and set it to true
AsyncPostBackTrigger 中使用的 id 控件必须位于更新面板之外(导致触发异步调用),如下所示:
The Control which id is used in AsyncPostBackTrigger must be outside the update Panel(that cause to fire the Async call) like this: