SeaSide SeaSide - 3 months ago 16
Vb.net Question

Is there another way to use onTextChanged for TextBox and avoid page refreshing every time?

I have a JavaScript which is doing some calculations from text fields and every time I pass on other text field JavaScript is called on action in TextBox 'onTextChanged' which require AutoPostBack = True but that every time doing refreshing of my page and because that I need every time to click twice on TextBox.

I have already tried to put this line of code on PageLoad but it still required 'AutoPostBack = True' and it also doing refreshing of page :

CType(Me.FV.FindControl("txtLP_1"), TextBox).Attributes.Add("onblur", "javascript:startCalc();")


This is how looks like code on PageLoad:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

If Not IsPostBack Then

End If

CType(Me.FV.FindControl("txtLP_1"), TextBox).Attributes.Add("onblur", "javascript:startCalc();")

End Sub

Answer

Instead of setting AutoPostBack="True" and invoking a post back everytime a value changes in your TextBox control why don't you just handle the text changed event through jQuery(completely client side)?

<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#' + '<%: TextBox1.ClientID %>').on('input', function (e) {
                var value = $(this).val();
                alert(value);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>