updatepanel 内的文本框出现问题 - 未导致 OnTextChanged 事件

发布于 2024-07-20 06:55:38 字数 1276 浏览 2 评论 0原文

我有以下情况:我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

送你一个梦 2024-07-27 06:55:38

我不确定您的问题与 UpdatePanel 有什么关系。

事实上,TextChanged 事件在输入时不会触发。 它只会在文本框失去焦点后触发。 如果 AutoPostBack 设置为 True,或者在下一次回发发生时,就会直接发生这种情况。 请参阅 AutoPostBack 属性<的文档/a> 和 TextChanged 事件。

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');
        }
    });
});

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 if AutoPostBack is set to True, 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:

$(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');
        }
    });
});
み青杉依旧 2024-07-27 06:55:38

将 txtMyTexbox AsyncPostBackTriggerEventName 属性设置为 TextChanged

<Triggers>             
    <asp:AsyncPostBackTrigger ControlID="txtMyTexbox" EventName="TextChanged" />            
</Triggers>

其他建议:

您是否尝试过查看 AutoComplete 控件是 AjaxControlToolKit 的一部分吗? 它的行为方式与您希望解决方案的行为方式相同。

Set the EventName property for your txtMyTexbox AsyncPostBackTrigger to TextChanged

<Triggers>             
    <asp:AsyncPostBackTrigger ControlID="txtMyTexbox" EventName="TextChanged" />            
</Triggers>

Other 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.

初懵 2024-07-27 06:55:38

值得一提的是,即使添加更新面板/ AsyncPostBackTrigger 后, TextBox ChangeEvent 也无法正常工作。 有时它有效,有时则无效。由于它是异步调用,我们需要一些时间刷新,或者等待或不可预测,希望微软能拿出一个称职的方法。下面是检查用户名的简单方法

- ----- 在 Page_Load 下 - aspx.cs --------------------------

this.TextBox1.Attributes.Add("onKeyUp", "fnUNameSubmit(this );");

-------在 aspx -add 脚本中 -------------------------------------- -

<script language="javascript" type="text/javascript">

function fnUNameSubmit(urInput) {   
var inpt= urInput.value;
if (inpt.length > 21) { 
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "green";
document.form1.submit();  // This is only trick we use here..
}
else {
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "red";
}
  }
</script>

----------在aspx中添加脚本------------------------------------ --

----------------aspx.cs -------------------
if (TextBox1.Text.Length > 21)
{
检查用户名();
Label2.Text = "";
}
别的
{
Label2.Text = "长度小于21"; //让我们做一些事情..bla..bla
}
------------------------------------------------- 检查用户名()

公共无效 CheckUsrName()
{

  Call dB values

}

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

<script language="javascript" type="text/javascript">

function fnUNameSubmit(urInput) {   
var inpt= urInput.value;
if (inpt.length > 21) { 
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "green";
document.form1.submit();  // This is only trick we use here..
}
else {
document.getElementById('<%= TextBox1.ClientID %>').style.backgroundColor = "red";
}
  }
</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()
{

  Call dB values

}
岛徒 2024-07-27 06:55:38

您不应该对 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

嘿嘿嘿 2024-07-27 06:55:38

解决方法是检查文本框 - Causesvalidation 属性并将其设置为 true

a workaround check textbox - causesvalidation property and set it to true

夜空下最亮的亮点 2024-07-27 06:55:38

AsyncPostBackTrigger 中使用的 id 控件必须位于更新面板之外(导致触发异步调用),如下所示:

<tr>
    <td colspan="4"><asp:Label ID="lblEnter_Successfully"  Text="Enter Record SuccessFully" runat="server" Visible ="false" ForeColor ="Blue" Font-Size ="Larger" Font-Bold ="true"></asp:Label>
    </td>
</tr>                                           
</table>                
</ContentTemplate>

  <Triggers>
     <asp:AsyncPostBackTrigger ControlID = "Button_Save" EventName ="Click"/>
  </Triggers>                       

</asp:UpdatePanel>
            <table>
              <tr>
                <td width = "472px" align ="right">             
                    <asp:Button ID="Button_Save" runat="server" Text="Save"  OnClientClick ="return URLValidation();"/>
                    <asp:Button ID="Button_Clear" runat="server" Text="Clear"/>
                </td>
              </tr>
            </table>

The Control which id is used in AsyncPostBackTrigger must be outside the update Panel(that cause to fire the Async call) like this:

<tr>
    <td colspan="4"><asp:Label ID="lblEnter_Successfully"  Text="Enter Record SuccessFully" runat="server" Visible ="false" ForeColor ="Blue" Font-Size ="Larger" Font-Bold ="true"></asp:Label>
    </td>
</tr>                                           
</table>                
</ContentTemplate>

  <Triggers>
     <asp:AsyncPostBackTrigger ControlID = "Button_Save" EventName ="Click"/>
  </Triggers>                       

</asp:UpdatePanel>
            <table>
              <tr>
                <td width = "472px" align ="right">             
                    <asp:Button ID="Button_Save" runat="server" Text="Save"  OnClientClick ="return URLValidation();"/>
                    <asp:Button ID="Button_Clear" runat="server" Text="Clear"/>
                </td>
              </tr>
            </table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文