1

According to this post you can intercept the post by overriding this function __doPostBack but it wont work. If I view the generated source I am not able to see the function that is meant to be auto generated and causes asp.net to make the postback.

where is this function? and how do I intercept it?

I'm using asp.net 4 and vs 2012

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script>
        //breaks here as it cant find __doPostBack
        var __original = __doPostBack;
        __doPostBack = myFunction();

        function myFunction() {

            alert('intercept');

        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>

        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim txt As String = TextBox1.Text

    End Sub
End Class
Community
  • 1
  • 1
Hello-World
  • 9,277
  • 23
  • 88
  • 154
  • __doPostBack function should be automatically generated when postback control (like asp:LinkButton) is on the page. But why do you need to intercept that? – Ihor Deyneka May 21 '13 at 13:33
  • because I cannot add return false via jquery or on the button click or on the form submit – Hello-World May 21 '13 at 14:10
  • :) It's like: -Why do you want to kill the rabit? -Because I don't want to kill the cat. Anyway did you try adding LinkButton to the page and for example alert typeof(__doPostBack). If that is function then your function was autogenerated successfully, if that is undefined then, no. So after that you can intercept this function and override it like suggested in the link you provided. What's your problem then? – Ihor Deyneka May 21 '13 at 14:15

1 Answers1

1

An ASP.NET Button renders an <input type="submit" /> to postback the form, and will never use this method. The __doPostBack method is only used for elements that are not input buttons that do not normally cause a POST operation to the server. This is a function that's inside one of the Microsoft's JavaScript file, and only gets rendered out on the client. Common uses for __doPostBack are LinkButton controls, controls that may have AutoPostBack="true", and others.

Also, there may be a call to WebForms_DoPostBack... something named like that that also posts back, but internally calls __doPostBack.

If you are trying to prevent postback on click of your button, attach to the submit event of the form, and cancel the postback operation that way.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257