1

I tryed trigger the TextChange event with ajax but it dosnt realy work how i do it. Hope you guys can help me.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="0">
            </asp:ScriptManager>
            <asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        </ContentTemplate>


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


    </asp:UpdatePanel>
magicmike
  • 83
  • 5

1 Answers1

3

The ScriptManager should appear before the UpdatPanel, not inside of it. And you don't need to define TextBox1 as a trigger. The TextBox being inside the UpdatePanel and the property ChildrenAsTriggers being true by default, the panel will be updated when TextBox1 loses focus after its content has been modified.

UPDATE

I originally misread the question. The following posts describes a method to trigger a postback and refresh the UpdatPanel every time a key is pressed in the TextBox: How do I make a Textbox Postback on KeyUp?.

Community
  • 1
  • 1
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
  • still dont work, im sad :( it just should make the code in the TextChange event when i type in something, lable2.text = textbox1.text for example but in c# – magicmike Apr 18 '16 at 19:34
  • It worked when I tested it. What do you in the `TextChanged` event handler? How do you know that it doesn't work? – ConnorsFan Apr 18 '16 at 19:35
  • i use the TextChange event for a bigger name filter method but the simple lable2.text = textbox1.text should be anoth for testing the triggering – magicmike Apr 18 '16 at 19:46
  • Oh sorry! You say "without leaving focus" in your question. I read it wrong. The event fires when the TextBox loses focus. – ConnorsFan Apr 18 '16 at 19:55
  • If you just want to do something simple like updating a Label while typing in the TextBox, you could do it with client-side code (Javascript). – ConnorsFan Apr 18 '16 at 19:56
  • is it possible without leaving focus? i filter the name with the textbox, type in something and it should drigger the method, i want use it like a textbox in win forms – magicmike Apr 18 '16 at 19:59
  • There is a good example here: http://stackoverflow.com/questions/1758240/how-do-i-make-a-textbox-postback-on-keyup. I will update my answer to fit your exact request but I guess you can already see how it works. – ConnorsFan Apr 18 '16 at 20:01
  • The main challenge is to put the focus back in the TextBox after the panel update so that you can keep typing. – ConnorsFan Apr 18 '16 at 20:07
  • I don't know if you got something working. I will update soon. It is quite tricky. :-) – ConnorsFan Apr 18 '16 at 20:34
  • yes it work, just copy paste the exampe from the link and it trigger without losing focus - just adjust for my code and its done - but thats no problem :) – magicmike Apr 18 '16 at 20:38
  • OK, so you use a TextBox outside of the UpdatePanel, right? The focus problem is when the TextBox is inside, like in your original code. – ConnorsFan Apr 18 '16 at 20:40
  • the javascript part with the keydown was important, i try it short without all this updatepanel – magicmike Apr 18 '16 at 20:53
  • When you said that you did it without the UpdatePanel, I thought you were just using Javascript, without a postback. I did not think that you considered doing a full page refresh. :-) – ConnorsFan Apr 18 '16 at 21:12
  • so, the textbox have to be outside the update panel and than all works with the javascript part, dont saw yesterday that the focus get lost otherwise - the trigger on the textbox is also important, else focus lost – magicmike Apr 19 '16 at 14:47
  • I did not test it, but I think that having the TextBox outside of the UpdatePanel is the only way to keep the focus on it. If the TexBox is inside the UpdatePanel, I can confirm that it loses focus. There is a way to put it back but it does not work 100%. And I am convinced that the focus is lost if you don't use the UpdatePanel at all. – ConnorsFan Apr 19 '16 at 14:53
  • Can you run some event handler in code-behind without losing the focus? I cannot get that behavior: the TextBox loses the focus every time there is a postback (complete or partial). – ConnorsFan Apr 19 '16 at 15:06
  • i dont know how to paste code in comment corectly but better than nothing - this is working for me – magicmike Apr 19 '16 at 15:24
  • – magicmike Apr 19 '16 at 15:26
  • When I test that code, the TextBox loses the focus after typing the first character. Maybe it is OK if the TextBox is the first input control in the form, but if there is another TextBox before, then the focus is lost. – ConnorsFan Apr 19 '16 at 15:30
  • ceap focus and dont refresh full site - i test it with some more textboxes – magicmike Apr 19 '16 at 15:30