ASP.NET OnTextChanged not firing from inside an update panel

心不动则不痛 提交于 2019-11-30 23:23:44

问题


I am using an ASP.NET update panel to retrieve user info using the on TextChanged for the textbox, here is my code:

<asp:UpdatePanel runat="server" ID="up1" ChildrenAsTriggers="true">
<ContentTemplate>
    <asp:TextBox runat="server" ID="loginEmail" Text="Email" 
        CssClass="textBoxes" OnTextChanged="userInfo" AutoPostBack="true"></asp:TextBox>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="loginEmail" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>

and the code behind:

string url, emailInfo;
emailInfo = loginEmail.Text;
url = Membership.GetUserNameByEmail(emailInfo);

emailText.InnerText = "Email: " + emailInfo;
urlText.InnerText = "Webiste: http://www.Elwazefa.com/User/" + url ; 

the code wont fire on textchanged but it wil, on ButtonClick or PageLoad.

What is the problem?

Using ASP.NET 4.0


回答1:


<asp:TextBox AutoPostBack="true" OnTextChanged="thingId_TextChanged" ID="thingId" runat="server"></asp:TextBox>

AutoPostBack on text-box will trigger post-back when the focus is lost from the text box. TextChanged event will be fired on any subsequent post-back (can be due to button-click or text box focus change). So you need to make certain

  1. After text is changed, you are moving out of text-box
  2. Whatever controls that you are change are part of update-panel (can be different update panel). If there are not part of any update panel then those changes won't get reflected on client side.

My guess is you are probably suffering from #2. You can use tool such as Fiddler (or Firebug on FireFox) to check if browser is firing AJAX (XHR) request when the focus is lost from the text-box.




回答2:


As @VinayC posted, AutoPostBack means that the page will postback to the server when your TextBox loses focus. No built-in event causes postback on every character added to a text input, and for good reason. UpdatePanel postbacks don't cause the page to flicker, but they can be just as heavy as a full postback.

If you want to work around this, you can give your textbox a client onchanged event handler, the JavaScript of which will be built from Page.ClientScript.GetPostBackEventReference().

The correct solution would be to use an AJAX method call from your JavaScript code rather than an UpdatePanel partial postback in onchanged.



来源:https://stackoverflow.com/questions/8696904/asp-net-ontextchanged-not-firing-from-inside-an-update-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!