C# 在ajax调用中从javascript引发服务器端按钮单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8771097/
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
Raise Server side button click event from javascript in ajax call
提问by Shantanu Gupta
I have a submit button on a page.
我在页面上有一个提交按钮。
<asp:Button ID="btnSubmit" runat="server" Text="Save Test" OnClick="btnSubmit_Click"
OnClientClick="return ValidateSaveTest(this);" />
On Javascript, ValidateSaveTest function is called which validates all the fields.
在 Javascript 上,调用 ValidateSaveTest 函数来验证所有字段。
function ValidateSaveTest(Sender) {
//do some validation, if fails return false from here. else move forward
var parameters = {};
parameters["parametersName"] = $("#" + hidTestId).val();
var succeededAjaxFn = function(result) {
if (result== true) {
var isNewVersion = confirm("Confirmation message");
if(isNewVersion)
{
//Raise server side button click event. Dont call click side event anymore.
$("#" + "<%=btnSubmit.ClientID %>").click();
}
return false;
}
}
var failedAjaxFn = function(result) { return false; }
//jquery ajax call
CallWebMethod("../Service.asmx", "IsTestUsed", parameters, succeededAjaxFn, failedAjaxFn);
//async call, always return false hence no postback from here.
//Need waiting unless ajax response is obtained.
return false;
}
I need to raise server side button click event from javascript once ajax response is received.
收到 ajax 响应后,我需要从 javascript 引发服务器端按钮单击事件。
采纳答案by Michiel van Oosterhout
You can get the required JavaScript code from the ClientScriptManager's GetPostBackEventReferencemethod:
您可以从 ClientScriptManager 的GetPostBackEventReference方法中获取所需的 JavaScript 代码:
Returns a string that can be used in a client event to cause postback to the server.
返回一个字符串,该字符串可用于客户端事件以导致回发到服务器。
This is normally used for writing the onclick attributes on controls like the <asp:linkButton>, but you can use it in your jQuery callback as well:
这通常用于在控件上编写 onclick 属性,例如<asp:linkButton>,但您也可以在 jQuery 回调中使用它:
var succeededAjaxFn = function(result) {
//Raise server side button click event. Dont call click side event anymore.
<%= Page.ClientScript.GetPostBackEventReference(btnSubmit, String.Empty) %>;
}
The <%= %>block above will write out the following JavaScript for you:
<%= %>上面的块将为您写出以下 JavaScript:
__doPostBack('btnSubmit','')
Which in turn will post back the form to the server in such a way that ASP.NET thinks the button was clicked, and so the server-side btnSubmit_Clickis triggered.
反过来,它将以 ASP.NET 认为按钮被单击的方式将表单回发到服务器,因此btnSubmit_Click触发了服务器端。
Notice that using this method, you can pass in a C# reference to the actual control. You don't need to worry about its client ID, or the correct name and arguments of the __doPostback()JavaScript function. All that is taken care of by the ClientScriptManagerwhen you call this method.
请注意,使用此方法,您可以将 C# 引用传递给实际控件。您无需担心其客户端 ID 或__doPostback()JavaScript 函数的正确名称和参数。所有这些都由ClientScriptManager您调用此方法时处理。
回答by Crab Bucket
Try this
尝试这个
__doPostBack('<%=btDemo.ClientID%>','OnClick');
Note the double underscore at the beginning
注意开头的双下划线
This should trigger a postback if the button was clicked by the user
如果用户点击了按钮,这应该会触发回发

