4

Is there an error in the way i use the __doPostBack?

function displaymessage() {
  var scl = "aaaaaa";
  var pageId = '<%=  Page.ClientID %>';
  __doPostBack(pageId, 'OtherInformation');
  alert(scl);
}

<input type="button" value="Click me!" id="Button2" onclick="displaymessage()"   />

When i press the button it should call the RaisePostBackEvent in the code file, but it doesn't. If i comment the doPostBack it reaches the alert but when it is uncommented it does not. So it must be an error in the usage of the doPostBack.

I followed this post: Call ASP.NET function from JavaScript?

Community
  • 1
  • 1
hhh3112
  • 2,167
  • 10
  • 36
  • 55
  • On a side note, any specific reason you want to get this route. Why not use PageMethods (http://weblogs.asp.net/mschwarz/archive/2008/01/08/how-to-move-from-ajaxpro-to-asp-net-ajax-pagemethods.aspx) and related things from Jquery as well? – Subhash Dike Apr 25 '11 at 08:43
  • Any chance you had the string "alert" in the page's URL? – jvanstry Aug 25 '16 at 22:37

6 Answers6

2

for me the _dopostback() was not firing only on IE and chrome browser. I have resolved by adding "return false;" statement in the javascript function. for example:-

  function test()
{
    _dopostback("logout","");
return false;
}

now its working fine.

Marimuthu
  • 21
  • 2
2

Place the following script on the header section of your html file:

<script>
    function __doPostBack(eventTarget, eventArgument) {
        document.Form1.__EVENTTARGET.value = eventTarget;
        document.Form1.__EVENTARGUMENT.value = eventArgument;
        document.Form1.submit();
    }
</script>
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
1

Change you code to this:

setTimeout(function () { __doPostBack('btnSave', '') }, 500);

Use btnSave Id. It will work in all browsers.

slfan
  • 8,950
  • 115
  • 65
  • 78
0

Add EnableEventValidation="false" into your <%Page tag to solve __doPostBack problem

0

Drop your second argument of __doPostBack ('OtherInformation'), and replace with an empty string, ''. You could put that data in a hidden input field if you need it, and retrieve it using Request.Form.

Protector one
  • 6,926
  • 5
  • 62
  • 86
0

I also followed the same post you mentioned and got an error, I tried to use the other answers here but it still didn't work.

Until I've found this post: http://forums.asp.net/t/1197643.aspx (look in the 8th reply made by NC01).

1.basically the idea is that your aspx should have something like this:

<script type="text/javascript" language="javascript">
  function myfunction() {

              if ('<%=Page.IsPostBack%>' == 'False') {
                 var pageId = '<%= this.UniqueID %>';
                  __doPostBack(pageId, 'do_something_good');
              }
          }
</script>

2.then in your .cs you should add interface IPostBackEventHandler (for example:)

public partial class _default : System.Web.UI.Page, IPostBackEventHandler

3.and in your .cs in page_load add this line:

this.ClientScript.GetPostBackEventReference(this, string.Empty);

4.don't forget to implement the interface:

public void RaisePostBackEvent(string eventArgument)
  {
   if (eventArgument == "do_something_good")
   {
    //bla
   }
  }

And guess what - it even works!

@Subhash Dike - The PageMethods works only for static methods, AFAIK.

BornToCode
  • 9,495
  • 9
  • 66
  • 83
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – oers Feb 27 '12 at 08:50