Javascript __doPostBack 函数是什么意思,什么时候用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5376055/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:55:37  来源:igfitidea点击:

What is the meaning of __doPostBack function, and when is it used?

javascriptasp.netpostbacksubmit

提问by mustafabar

I had problem triggering server side button click events so I found a solution on the net that I should do something like

我在触发服务器端按钮点击事件时遇到问题,所以我在网上找到了一个解决方案,我应该做一些类似的事情

  <input type="submit" name="button" id="loginButton" value="Submit" 
                            class="button-orange" alt="Register" title="Register" runat = "server" onclick ="this.disabled=true;__doPostBack('loginButton','')"/>

I did it, and it worked, but I would like to know what is going on!

我做到了,它奏效了,但我想知道发生了什么!

采纳答案by Robert

simply said, it is used mainly by controls with AutoPostBack property

简单的说,它主要被具有 AutoPostBack 属性的控件使用

http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx

http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx

if you want to implement autopostback for your custom control, then you need to implement IPostBackDataHandler

如果要为自定义控件实现自动回发,则需要实现IPostBackDataHandler

回答by Jakub Konecki

Check this article:

检查这篇文章:

Understanding the JavaScript __doPostBack Function

理解 JavaScript __doPostBack 函数

This method is used to submit (post back) a form to the server and allows ASP.NET framework to call appropriate event handlers attached to the control that raised the post back.

此方法用于向服务器提交(回发)表单,并允许 ASP.NET 框架调用附加到引发回发的控件的适当事件处理程序。

You usually (in simple scenarios) don't use the method directly - it is internally used by the controls you drop on the page.

您通常(在简单场景中)不直接使用该方法 - 它由您放置在页面上的控件内部使用。

The parameters passed to this function are stored in a hidden field and picked up by ASP.NET framework on the server-side in order to find the control that raised the post back.

传递给此函数的参数存储在一个隐藏字段中,并由服务器端的 ASP.NET 框架选取,以便找到引发回帖的控件。

回答by Shadow Wizard is Ear For You

The solution might be working but it's not a real fix.. better way will be to find why the button events are not triggering and fix the core of the problem.

解决方案可能有效,但不是真正的解决方案。更好的方法是找出按钮事件没有触发的原因并解决问题的核心。

Now to answer your questions.. PostBackis the term used to describe when the form is being submitted (posted) back to the same page. Simple as that.

现在回答您的问题。PostBack是用于描述表单何时提交(发布)回同一页面的术语。就那么简单。

Ordinary submit button would have been enough, but part of PostBack is the ability to identify which control triggered it, meaning what button or link was clicked.

普通的提交按钮就足够了,但 PostBack 的一部分是能够识别哪个控件触发了它,这意味着点击了哪个按钮或链接。

To do such a thing ASP.NETis automatically adding hidden fields to the form and when clicking on element that should cause PostBack, JavaScript code is used to update the values of those hidden fields to the proper values indicating what was clicked - the argument you pass.

要做这样的事情ASP.NET是自动向表单添加隐藏字段,当单击应该导致回发的元素时,JavaScript 代码用于将这些隐藏字段的值更新为正确的值,指示单击的内容 - 您传递的参数。

The name Microsoft chose to give to the JS function doing the above is __doPostBack- it's just a name of a function, ordinary JavaScript function that ASP.NETautomatically writes to the browser.

微软为执行上述操作的 JS 函数选择的名称是__doPostBack——它只是一个函数的名称,普通的 JavaScript 函数会ASP.NET自动写入浏览器。

Hope things are bit more clear now.

希望事情现在更清楚了。