hhh3112
hhh3112

Reputation: 2187

__doPostBack not working for me

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?

Upvotes: 4

Views: 31852

Answers (6)

Dinesh Sail
Dinesh Sail

Reputation: 19

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

Upvotes: 0

Harish Madaan
Harish Madaan

Reputation: 37

Change you code to this:

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

Use btnSave Id. It will work in all browsers.

Upvotes: 1

Marimuthu
Marimuthu

Reputation: 21

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.

Upvotes: 2

BornToCode
BornToCode

Reputation: 10213

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.

Upvotes: 0

Protector one
Protector one

Reputation: 7261

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.

Upvotes: 0

Akram Shahda
Akram Shahda

Reputation: 14771

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>

Upvotes: 2

Related Questions