VB.NET - Code Samples - Force Postback to raise the TextChanged Event

VB.NET Force Asp.net postback onChange for the TextChanged event.


By default, the asp.net textbox control does not postback any events. The textchanged event only gets raised after something else has triggered a postback. This makes it difficult to make dynamic changes based on textbox controls. To fix this I modified some code found at Mathew Nolton's Blog. All that is done here is that on the javascript onchange event of the textbox a postback is raised. I add this attribute on the page load event for all textbox controls that I need the textchanged event for. After the code in the page load event is in place the textchanged event should fire whenever the text has changed and the textbox loses focus.

The Code:

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsPostBack Then
        End If

        Dim js As String
        js = "javascript:" & Page.GetPostBackEventReference(Me, "@@@@@buttonPostBack") & ";"
        TextBox1.Attributes.Add("onchange", js)
        TextBox2.Attributes.Add("onchange", js)
        TextBox3.Attributes.Add("onchange", js)
    End Sub
About this page: