1

I have the following aspx code

 <asp:TextBox ID="uname" runat="server" AutoPostBack="True" 
            ontextchanged="uname_TextChanged"></asp:TextBox>

in code behind file

protected void uname_TextChanged(object sender, EventArgs e)
    {
        Response.Write("Called on postback");
    }

As per my assumption due to autopostback if I write anything in the textbox a postback will occur, but its nor occuring now, what could be the reason?

  • Normally there are more than 1 event handler for an event like for a control, which event will be triggered if autopostback occurs.?
1
  • 1
    Did you try debugging? And did you try it taking the focus out of the textbox? because it says automatic postback to the server occurs when the TextBox control loses focus Commented Apr 9, 2014 at 8:47

3 Answers 3

2

You said As per my assumption due to autopostback if I write anything in the textbox a postback will occur.

That is wrong. It will fire text changed event when your focus from textbox will be out. So when you type something and press Tab key then only your TextChanged event will be triggered.

If you want to trigger TextChanged event when you type something then you should call it from javascript using OnKeyDown event. see below code sample :

<asp:TextBox ID="uname" runat="server" AutoPostBack="True"
    OnKeyDown="TextChanged(this)" OnTextChanged="uname_TextChanged"></asp:TextBox>


<script type="text/javascript">
    function TextChanged(control) {
        $(control).change();
    }
</script>

Now when you type anything in your textbox it will call TextChanged method of javascript and this method will trigger uname_TextChanged event.

Assumption:

If your textbox control is inside UpdatePanel then also your change event cannot be triggered. In such a case you should define trigger for text box. as mentioned below :

<asp:UpdatePanel runat="server" ID="up1">
    <ContentTemplate>
        <asp:TextBox ID="uname" runat="server" AutoPostBack="True"
            OnKeyDown="TextChanged(this)" OnTextChanged="uname_TextChanged"></asp:TextBox>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="uname" />
    </Triggers>
</asp:UpdatePanel>

Q : which event will be triggered if autopostback occurs ?

Answer: Normally, for all the events for which contents are changed between the post server, AutoPostBack requires. But it wont be triggered until you define the event for that.

For example if you set the AutoPostBack property of the DropDownList to true, and if you don't specify the OnSelectedIndexChanged event then it will not trigger this event. But your page will be post back when you change the value.

Same thing happens in case of TextBox, CheckBox, RadioButton etc...

Hope it is enough to understand.

Sign up to request clarification or add additional context in comments.

1 Comment

can you address the second part as well? so I can accept it as answer
1

Try by changing the ontextchanged to OnTextChanged and try. and after entering the text in textbox try to click the mouse on the page sure it works.

1 Comment

it doesn't matter whether it is in camel case or small case
1

MSDN states that the AutoPostBack is only raised when the control loses focus:

Gets or sets a value that indicates whether an automatic postback to the server occurs when the TextBox control loses focus.

...

Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.

However, a test showed that you need to enter a text first for the PostBack to occur. After I entered some text and tabbed out of the TextBox, the PostBack was done and the TextChanged event was raised. After clearing the text and tabbing out of the TextBox, the AutoPostBack was also done, so it does not depend on whether the TextBox is empty or not.

To address also the second part of your question: which event is raised during the PostBack is decided upon by the ASP.NET framework during the initialization phase of the PostBack. E.g. if the text of the TextBox that is contained in the Form values differs from that stored in the ViewState, the TextChanged event is raised. This explains why the TextChanged handler is called even if the AutoPostback was initiated by the losing the focus.

1 Comment

I didn't get the second part, which event wil be called in case of postback is determined at runtime? so developer is not sure which event will be called in case of button, textbox, selectbox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.