jmpena
jmpena

Reputation: 1419

__doPostBack is not defined

Im getting that error when try to call a __doPostBack on one of my pages, every page that i have in the project use __doPostBack function but in this particular page im getting that Javascript error.

i was looking in the internet and the only thing i read is that this error happends when i have a unclose tag but i review the site and its ok.

Error: __doPostBack is not defined Source File: htt://localhost:99/ProjectName/Disable.aspx Line: 1

Upvotes: 35

Views: 89771

Answers (26)

Spinone
Spinone

Reputation: 21

For me it was the fact that I was using a custom User-Agent Header of User-Agent: This is a Test, please ignore.

This is because IIS ASP.NET Webforms generates web pages depending on the User-Agent string.

To fix it, I included the browser types, plus a message at the end, i.e.

User-Agent: Mozilla/5.0 (X11; Linux x86_64) Chrome/75.0.3770.80 (This is a Test, please ignore)

And the page started running correctly again.

Upvotes: 2

Jamshaid K.
Jamshaid K.

Reputation: 4587

Wrapping my server side controls in a form element with runat="server" attribute worked for me. According to asp environment there should be only one form element with runat="server" attribute in it otherwise some serious issue may happen.

<form runat="server" id="form1">
   <!-- Server side controls go here -->
</form>

Upvotes: 0

Atul Pawar
Atul Pawar

Reputation: 11

lnkDeactivate.Attributes("onclick") = ClientScript.GetPostbackClientHyperlink(lnkDeactivate, "deactivate")

Upvotes: 0

Keivan Sabil...
Keivan Sabil...

Reputation: 95

i had this error and i solved it by inserting :

<form runat="server" style="display: none"></form>

Upvotes: 0

Shathar Khan
Shathar Khan

Reputation: 1

It is __doPostBack function not found error. Put one button in page and set its usersubmitbehavior=false, then run and see viewsource, you will have __doPostBack function automatically be added in your page.

Now put your own html button which you want to make postback from and call __doPostBack function by setting its onclick="__doPostBack('mybut','save')".

Now the __doPostBack function required for your html button is given by the above button.

Upvotes: -1

Martin Sentker
Martin Sentker

Reputation: 21

Solution for my Problem: IIS ASP.NET Webforms generates web pages depending on user Agent string

My IIS had no definition for the IE11 browser so it defaulted to a very simple browser profile which doesn't even Support JavaScript.

  • short term solution: run browser in compatibility mode.
  • medium term solution: Deployment of .Net Framework 4.5.1 which contains IE11 browser profile definitions.

An alternative: set IE11 Browser definiton file

Upvotes: 2

Ali
Ali

Reputation: 469

For me "__doPostBack" did not work in all pages because I compiled my project with newer version of Visual stdio then I double clicked on properties in solution explorer and changed "Target framework " from .Net framework 4.0 to .Net framework 3.0 and after that rebuild project and it worked.

Upvotes: 0

behrooz.mh
behrooz.mh

Reputation: 1

i used this method in my script that included to asp.net form:

main form id sample:
<script src="myscript" />

<form id="frmPost" runat="server">
</form>

// myscript.js in java script function
mypostboack: function () {
$('#frmPost').context.forms[0].submit()
}

Upvotes: 0

mark robinson
mark robinson

Reputation: 1

For me it was the following was missing from the page:

<form runat="server">
</form>

Upvotes: -2

user3283145
user3283145

Reputation: 39

Essentially what's going on is that there are 2 missing html hidden elements "eventtarget" and "eventargument", as well as a missing function "__doPostBack".

These are missing from the DOM.

I tried all the fixes listed for this and none worked. However using a combination of jquery and javascript there is an unobtrusive solution. Add this to your javascript on document ready and you're off to the races (This is a much quicker alternative than installing the .net framework 4.5 on your server, although if you can install 4.5 thats the way to go):

if ($('#__EVENTTARGET').length <= 0 && $('#__EVENTARGUMENT').length <= 0) {
  $('#YOUR_ASPNET_FORMID').prepend('<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />');
}

if (typeof __doPostBack == 'undefined') {
  __doPostBack = function (eventTarget, eventArgument) { object
    var theForm = document.forms['YOUR_ASPNET_FORMID'];
    if (!theForm) {
      theForm = document.YOUR_ASPNET_FORMID;
    }
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
      theForm.__EVENTTARGET.value = eventTarget;
      theForm.__EVENTARGUMENT.value = eventArgument;
      theForm.submit();
    }
  };
}

I understand that some of said installing 4.5 fixes this. I would definitely recommend that. However, if you're like me working on an enterprise public facing site with a cms system baked in .net 4, this might just be an easier solution, as opposed to possibly introducing new bugs created from updating your platform.

Upvotes: 3

Mark Ibanez
Mark Ibanez

Reputation: 687

I had this problem and sometimes its just stupidity. I wrote it as __doPostback with a lowercase'b', where it should have been __doPostBack with an uppercase 'B'.

Upvotes: 0

gobes
gobes

Reputation: 562

Old question but recent answer: if you have a ScriptManager, check if EnablePartialRendering is enabled. If so, disable it, I don't know why it's a problem there...

Upvotes: 0

Jose Luis
Jose Luis

Reputation: 193

Just add this code to your .aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

Upvotes: 19

Raghubir Singh
Raghubir Singh

Reputation: 227

__doPostBack Works guys

Write this code in Page Load

ClientScript.GetPostBackEventReference(this, "");

Upvotes: 8

Ted
Ted

Reputation: 2585

Another addition to this vintage thread....

I had these symptoms while developing a site using JQuery Mobile, and it turned out that it was caused by the link I followed to the affected page not having the rel="external" attribute set on it.

This meant that JQuery loaded the contents of the new page via AJAX, and 'injected' them into the current page, rather than reloading the whole thing, and hence the required postback javascript wasn't present until a full page refresh.

Upvotes: 0

rg89
rg89

Reputation: 371

I had this problem when I wrote my own page code and forgot to put runat="server" on the form element.

Upvotes: 2

Don Tato
Don Tato

Reputation: 479

The run time/client side error __doPostBack is undefined hassled me for a few hours. There was lots of misleading/incorrect help on the net. I inserted the following line of code in the Page_Load event of the default.aspx.cs file and everything worked fine, on my system and in production with GoDaddy.

ClientScript.GetPostBackEventReference(this, string.Empty);

Upvotes: 47

WhatsInAName
WhatsInAName

Reputation: 724

I was getting this when I was clicking on a LinkButton. Solved this by adding OnClick="LinkButton2_Click" to the markup.

Upvotes: 0

Josh Mouch
Josh Mouch

Reputation: 3558

I was getting this because my script was inside an Iframe that was being "disposed" (it was a Telerik "window" that was closed). The code defining _dopostback() inside my iframe was gone, but the code calling _dopostback() inside my iframe was still hanging around. Love that JavaScript!

Upvotes: 0

Troy
Troy

Reputation: 11

 this.Page.ClientScript.GetPostBackEventReference(<a control>, string.Empty); 

This trick worked for me. Still would have liked to know why it wasn't rendering...

Thank you!

Upvotes: 1

SAGExSDX
SAGExSDX

Reputation: 703

If the page doesn't have a control that causes a postback, __doPostBack() won't be output as a function definition. One way to override this is to include this line in your Page_PreRender():

this.Page.ClientScript.GetPostBackEventReference(<a control>, string.Empty);

This function returns a string calling __doPostBack(); but also forces the page to output the __doPostBack() function definition.

Upvotes: 22

Justin
Justin

Reputation: 23

I know it's been a while since this thread was active, but I'll add another tidbit for those coming along in the future.

The ClientScriptManager class has available a couple of methods that make dealing with JavaScript postbacks better. In particular is the GetPostBackClientHyperlink routine. It retuns a string that you can then just assign to the element's onclick client-side event. Any time you use this method the __doPostBack routine and any necessary hidden form fields are automatically generated, plus you don't have to write the JavaScript yourself. For example, I have this in the Page_Load:

lnkDeactivate.Attributes("onclick") = ClientScript.GetPostbackClientHyperlink(lnkDeactivate, "deactivate")

In this case, ClientScript is the ClientScriptManager object instance automatically made available by the page...I did not instantiate it. On the post-back, I use this code to detect the event:

If Not String.IsNullOrEmpty(Request("__EVENTARGUMENT")) AndAlso Request("__EVENTARGUMENT") = "deactivate") Then
  '-- do somthing
End If

I'm sure there are better/cleaner ways to hook this up, but I hope you get the idea behind it.

Upvotes: 1

Paul Keister
Paul Keister

Reputation: 13097

Here's why this was happening to me: I accidentally forgot that script tags must always have closing tags:

<script src="/Scripts/appLogic/Regions.js" />

I corrected the script tag:

<script src="/Scripts/appLogic/Regions.js" type="text/javascript" ></script>

and sanity returned.

Upvotes: 16

Colin Wiseman
Colin Wiseman

Reputation: 868

I had this problem just today, but none of the solutions I found worked, unfortunately including yours. So if someone reads this and it doesn't work for them try this

http://you.arenot.me/2010/11/03/asp-net-__dopostback-is-not-defined/

Cheers though for helping me through the process of working out what went wrong!

Upvotes: 0

Atanas Korchev
Atanas Korchev

Reputation: 30671

Try calling the RegisterRequiresPostBack method. It should make the ASP.NET runtime include the __doPostBack code.

Upvotes: 4

James Curran
James Curran

Reputation: 103575

__doPostBack() should be automatically included by any ASP.NET WebControl that could cause a post back. You sound like you are calling it manually in some Javascript you wrote. If so, you will need to include a WebControl, to make sure that function in inserted onto the page.

Upvotes: 10

Related Questions