可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
__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.
回答2:
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);
回答3:
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.
回答4:
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.
回答5:
Just add this code to your .aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
回答6:
__doPostBack Works guys
Write this code in Page Load
ClientScript.GetPostBackEventReference(this, "");
回答7:
Try calling the RegisterRequiresPostBack method. It should make the ASP.NET runtime include the __doPostBack code.
回答8:
I had this problem when I wrote my own page code and forgot to put runat="server"
on the form element.
回答9:
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.
回答10:
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
回答11:
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.
回答12:
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!
回答13:
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!
回答14:
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!
回答15:
I was getting this when I was clicking on a LinkButton. Solved this by adding OnClick="LinkButton2_Click"
to the markup.
回答16:
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.
回答17:
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...
回答18:
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'.
回答19:
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()
}
回答20:
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.
回答21:
i had this error and i solved it by inserting :
<form runat="server" style="display: none"></form>
回答22:
lnkDeactivate.Attributes("onclick") = ClientScript.GetPostbackClientHyperlink(lnkDeactivate, "deactivate")
回答23:
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>
回答24:
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.
回答25:
For me it was the following was missing from the page:
<form runat="server">
</form>