Ira*_*tro 12 asp.net ajax updatepanel
我有以下情况:我在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>
Run Code Online (Sandbox Code Playgroud)
在服务器端,我在页面加载时写了以下内容
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(txtMyTexbox);
Run Code Online (Sandbox Code Playgroud)
和这样的方法
protected void txtMyTexbox_TextChanged(object sender, EventArgs e)
{
if (.....)
{
lblMessage.Visible = false;
}
else
{
lblMessage.Visible = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是:当用户键入文本框时,它不会导致OnTextChanged事件.
我错过了什么吗?
我不确定你的问题与这个问题有什么关系UpdatePanel
.
事实上,在打字时TextChanged
不会触发事件.它只会在文本框失去焦点后触发.如果设置为,或者发生下一次回发,则会直接发生这种情况.请参阅AutoPostBack属性和TextChanged事件的文档.AutoPostBack
True
Afaik,你最好的选择可能是在javascript中处理keyup事件.
这是一个简单的jQuery示例:
$(document).ready(function() {
$(':text[id$=YourTextBox]').keyup(function() {
if ($(this).val() === "your special value") {
$('span[id$=YourLabel]').css('visibility', 'visible');
}
else {
$('span[id$=YourLabel]').css('visibility', 'hidden');
}
});
});
Run Code Online (Sandbox Code Playgroud)
将EventName
txtMyTexbox 的属性设置AsyncPostBackTrigger
为TextChanged
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtMyTexbox" EventName="TextChanged" />
</Triggers>
Run Code Online (Sandbox Code Playgroud)
其他建议:
您是否尝试过查看属于AjaxControlToolKit 的AutoComplete控件?它的行为方式与您希望解决方案的行为方式相同.
您不应该对 TextBox 使用 RegisterAsyncPostBackControl。该方法实际上仅适用于驻留在 UpdatePanel 之外的控件。我会尝试删除该行代码并看看会发生什么。
有关详细信息,请参阅: http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerasyncpostbackcontrol.aspx
归档时间: |
|
查看次数: |
28401 次 |
最近记录: |