按钮点击/页面从 JavaScript 提交

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

Button click/page submit from JavaScript

c#javascriptasp.net

提问by rauland

I'd like to understand how the asp.net framework knows when a button has been clicked and therefore once it receives the request fires its click event on the server.

我想了解 asp.net 框架如何知道按钮何时被点击,因此一旦它收到请求,就会在服务器上触发它的点击事件。

I need to understand how it works as I would like to trigger the button's server click event from javascript.

我需要了解它是如何工作的,因为我想从 javascript 触发按钮的服务器点击事件。

I am able to perform a submit of the page from JavaScript:

我能够从 JavaScript 执行页面提交:

 document.forms[0].submit();

But how could do the same so that asp.net thinks that a button click has happened, so on the server its click event fires once the request is received.

但是如何做同样的事情,以便 asp.net 认为发生了按钮点击,因此在服务器上,一旦收到请求,它的点击事件就会触发。

Thanks

谢谢

UPDATE 1

更新 1

Thanks everybody for the replies. Doing a few tests if I add one asp.net button and by looking at the source code, I cant seem to find a generated JavaScript "__doPostBack" function.

谢谢大家的回复。如果我添加一个 asp.net 按钮并通过查看源代码进行一些测试,我似乎无法找到生成的 JavaScript“__doPostBack”函数。

This is the test I've tried.

这是我尝试过的测试。

I have one normal HTML button (button1) and an asp.net button.(button2)

我有一个普通的 HTML 按钮 (button1) 和一个 asp.net 按钮。(button2)

From the HTML button's click event (button1), I've added a client call to the javascript __doPostback method passing in the asp.net button2 id.

从 HTML 按钮的单击事件 (button1) 中,我添加了对传入 asp.net button2 id 的 javascript __doPostback 方法的客户端调用。

So I am trying to fire the server side button2 click event by calling the html button (button1.)

所以我试图通过调用 html 按钮 (button1.) 来触发服务器端 button2 单击事件。

<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <input id="Button1" type="button" 
         value="Raise Button2 server side click event"
         onclick="javascript:__doPostBack('Button2', '')" /> 

        <asp:Button ID="Button2" runat="server" 
        Text="asp.net_Button2" 
        OnClick="button2Click" />  
    </div>  
    </form>  
</body>  
</html> 

And the generated HTML:

以及生成的 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
</head>
<body>
    <form name="form1" method="post" action="Default6.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
value="/wEPDwUKMjA0OTM4MTAwNGRke29WUkB+FLhQArxd1ehT98cWw1Y=" />
</div>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" 
value="/wEWAgKLs+3sDgK7q7GGCBs2MBvLk9CJNnA02t08R1LKdiJU" />
</div>
    <div>
        <input id="Button1" type="button" value="Raise Button2 server side click event" 
onclick="javascript:__doPostBack('Button2', '')" />
        <input type="submit" name="Button2" value="asp.net_Button2" id="Button2" />
    </div>
    </form>
</body>
</html>  

UPDATE 2

更新 2

If I set the buttons "UseSubmitBehavior" attribute to false, then it does generated the __doPostBack method and by calling it from the client it does raise the event on the server.

如果我将按钮“UseSubmitBehavior”属性设置为 false,那么它会生成 __doPostBack 方法,并通过从客户端调用它确实在服务器上引发事件。

So what would be the difference between submit and a postback? (I'll start a new thread for this).
Thanks for the help!

那么提交和回发有什么区别呢?(我将为此开始一个新线程)。
谢谢您的帮助!

采纳答案by Akram Shahda

The javascript__doPostBackfunction is called whenever the page is needed to be posted back.

JavaScript的__doPostBack任何需要的页面调回函数被调用。

To learn about __doPostBack, read thisarticle.

要了解__doPostBack,请阅读这篇文章。



EDIT

编辑

Use the UseSubmitBehaviorproperty to specify whether a Buttoncontrol uses the client browser's submit mechanismor the ASP.NET postback mechanism. By default the value of this property is true, causing the Buttoncontrol to use the browser's submit mechanism. If you specify false, the ASP.NET page frameworkadds client-side script to the page to post the form to the server.

使用该UseSubmitBehavior属性指定Button控件是使用客户端浏览器的提交机制还是ASP.NET 回发机制。默认情况下,此属性的值为true,导致Button控件使用浏览器的提交机制。如果指定false,则ASP.NET 页面框架会将客户端脚本添加到页面以将表单发布到服务器。

When the UseSubmitBehaviorproperty is false, control developers can use the GetPostBackEventReferencemethod to return the client postback event for the Button. The string returned by the GetPostBackEventReferencemethod contains the text of the client-side function call and can be inserted into a client-side event handler.

当该UseSubmitBehavior属性为false 时,控件开发人员可以使用该GetPostBackEventReference方法为 返回客户端回发事件Button。该GetPostBackEventReference方法返回的字符串包含客户端函数调用的文本,可以插入到客户端事件处理程序中。



Refer to:

参考:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx

http://www.w3schools.com/ASPNET/prop_webcontrol_button_usesubmitbehavior.asp

http://www.w3schools.com/ASPNET/prop_webcontrol_button_usesubmitbehavior.asp

回答by Shadow Wizard is Ear For You

You can perfectly imitate button click with client side code:

您可以使用客户端代码完美模拟按钮点击:

document.getElementById("<%=Button1.ClientID%>").click();

Button1is the ID of the "server side" button.

Button1是“服务器端”按钮的 ID。

回答by Shadow Wizard is Ear For You

Here is the code responsible for executing the proper server side method:

这是负责执行正确的服务器端方法的代码:

private void RaisePostBackEvent(NameValueCollection postData)
{
    if (this._registeredControlThatRequireRaiseEvent != null)
    {
        this.RaisePostBackEvent(this._registeredControlThatRequireRaiseEvent, null);
    }
    else
    {
        string str = postData["__EVENTTARGET"];
        bool flag = !string.IsNullOrEmpty(str);
        if (flag || (this.AutoPostBackControl != null))
        {
            Control control = null;
            if (flag)
            {
                control = this.FindControl(str);
            }
            if ((control != null) && (control.PostBackEventHandler != null))
            {
                string eventArgument = postData["__EVENTARGUMENT"];
                this.RaisePostBackEvent(control.PostBackEventHandler, eventArgument);
            }
        }
        else
        {
            this.Validate();
        }
    }
}

This is taken from the Pageclass source code, .NET 4.0

这取自Page类源代码,.NET 4.0

As you see and as SLaks already explained the code is looking for control with ID equal to the ID passed as hidden form field called __EVENTTARGETand then invoke the handler PostBackEventHandlerif exists.

如您所见,正如 SLaks 已经解释的那样,代码正在寻找 ID 等于作为隐藏表单字段传递的 ID 的控件,__EVENTTARGET然后调用处理程序(PostBackEventHandler如果存在)。

回答by Oleg Grishko

回答by SLaks

ASP.Net adds a client-side onclickhandler to the button that calls the client-side __doPostbackmethod with the button name as a parameter.
This method inserts the button name in a hidden field and submits the form.
(You can see all of this in the page source)

ASP.Netonclick向按钮添加了一个客户端处理程序,该处理程序以__doPostback按钮名称作为参数调用客户端方法。
此方法在隐藏字段中插入按钮名称并提交表单。
(您可以在页面源中看到所有这些)

On the server, ASP.Net looks up the control and event names from the hidden field and raises the appropriate server-side name.

在服务器上,ASP.Net 从隐藏字段中查找控件和事件名称,并生成适当的服务器端名称。