__doPostBack is undefined

6 篇文章 0 订阅

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.
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值