Search code examples
asp.netvb.net

TextBox text being reverted before TextChanged event can be handled


I have to write ASP VB.net webforms for my work. I have two textboxes, txtA and txtB, and when their contents are changed I want to handle this on the server to save that change. (note: I don't want an update with every keystroke like a lot of questions here are asking. I'm just looking for when the user hits enter or clicks off the box the event is sent, which is happening so far).

On both tags within the ascx code I have included AutoPostBack="true", and I have written an event handler to include the behaviour I want. It looks something like this:

Private Sub TextUpdated(sender As Object, e As EventArgs) Handles txtA.TextChanged, txtB.TextChanged
    SaveChanges(txtA.Text, txtB.Text)
End Sub

Of course this is a simplification, but it gets the point across. I have confirmed that this handler is being called and appears to be behaving correctly, the only issue is that the values of txtA.Text and txtB.Text are being reverted before we even get to the event handler. For example, if txtA.Text was 'foo' and txtB.Text was 'bar', and the user updated the contents of txtA to be 'bar', a TextChanged event would be raised, and TextUpdated would be called, but the value of txtA.Text would still be 'foo'.

I have confirmed this by logging the values of the textboxes after entering TextUpdated. This confirms the event is being raised, and also that the contents of the textboxes are not updating.


Solution

  • Best guess without more markup and code?

    Keep in mind that for all such pages, any button click, any combo box (dropdown list), or a text box with auto post back?

    The page on load event fires each time, and runs BEFORE the event code stub in question.

    As a result, the last 200+ web pages I have designed?

    You need to place any page setup code, data loading code, and general page setup code used in the page load event inside of a Not post back stub.

    Hence, this markup:

            <h3>Text box 1</h3>
            <asp:TextBox ID="TextBox1" runat="server"
                OnTextChanged="TextBox1_TextChanged"
                AutoPostBack="true"
                ></asp:TextBox>
            <h3>Text box 2</h3>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    

    And code behind can be say this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
    
            ' the REAL first page load code goes here
    
            TextBox1.Text = "Value from setup code"
    
            TextBox2.Text = "Value from setup code"
    
        End If
    End Sub
    
    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
    
        ' copy text from Text box 1 to text box 2, but set to upper case
    
        TextBox2.Text = TextBox1.Text.ToUpper
    
    
    End Sub
    

    So, keep in mind that your page setup code (in on load event) must be placed in that all important IsPostBack code stub, since any and all and every post back as a result of some control will run page load first, and do so EACH time on a post-back, and then the code stub will run.

    Hence, the result of above looks like this:

    enter image description here

    So, check your page load code. You want to prevent the code from running again and again on each post back, of which it does.

    Hence, as a result, you can't build a working web page with setup code unless that setup code is placed inside of the [! [ Not IsPostBack code stub, which in effect is your true real first page load event. So, page load will run each and every time on any page post back.

    In the above simple example, had I not placed the setup code in that Not IsPostBack code stub? Then the values entered into the text boxes would be lost, and over written with the page setup code running again before the text changed code stub runs. As a result, then the code stub code would never see any values entered by the user, since the page load event setup code runs again, and overwrites any user input on each page post back.