Javascript __doPostBack 对我不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5770607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
__doPostBack not working for me
提问by hhh3112
Is there an error in the way i use the __doPostBack?
我使用 __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.
当我按下按钮时,它应该调用代码文件中的 RaisePostBackEvent,但它没有。如果我评论 doPostBack 它会到达警报但是当它被取消注释时它不会。所以一定是doPostBack的使用有误。
I followed this post: Call ASP.NET function from JavaScript?
我关注了这篇文章:从 JavaScript 调用 ASP.NET 函数?
回答by Marimuthu
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:-
对我来说,_dopostback() 不仅仅在 IE 和 Chrome 浏览器上触发。我已经通过添加“return false;”解决了 javascript 函数中的语句。例如:-
function test()
{
_dopostback("logout","");
return false;
}
now its working fine.
现在它的工作正常。
回答by Akram Shahda
Place the following script on the header section of your html file:
将以下脚本放在 html 文件的标题部分:
<script>
function __doPostBack(eventTarget, eventArgument) {
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>
回答by Protector one
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
.
删除__doPostBack
('OtherInformation') 的第二个参数,并替换为空字符串''
. 如果需要,您可以将该数据放在隐藏的输入字段中,然后使用Request.Form
.
回答by BornToCode
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).
直到我找到这个帖子:http: //forums.asp.net/t/1197643.aspx(查看 NC01 的第 8 次回复)。
1.basically the idea is that your aspx should have something like this:
1.basically的想法是你的aspx应该有这样的东西:
<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:)
2.然后在你的.cs中你应该添加接口IPostBackEventHandler(例如:)
public partial class _default : System.Web.UI.Page, IPostBackEventHandler
3.and in your .cs in page_load add this line:
3.and 在你的 .cs 中的 page_load 添加这一行:
this.ClientScript.GetPostBackEventReference(this, string.Empty);
4.don't forget to implement the interface:
4.不要忘记实现接口:
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.
@Subhash Dike - PageMethods 仅适用于静态方法,AFAIK。
回答by Harish Madaan
Change you code to this:
将您的代码更改为:
setTimeout(function () { __doPostBack('btnSave', '') }, 500);
Use btnSave Id
. It will work in all browsers.
使用btnSave Id
. 它适用于所有浏览器。