1.
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);
2.
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.
3.
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.

本文讲述了在客户端遇到__doPostBack未定义问题时,如何通过在页面预加载事件中添加JavaScript代码以及使用jQuery来临时解决。作者提供了创建隐藏HTML元素和自定义__doPostBack函数的方法,以避免升级到.NETFramework4.5可能带来的额外问题。
172

被折叠的 条评论
为什么被折叠?



