从Ajax调用JavaScript的提高服务器端的按钮单击事件 [英] Raise Server side button click event from javascript in ajax call
问题描述
我有一个提交页面上的按钮。
I have a submit button on a page.
<asp:Button ID="btnSubmit" runat="server" Text="Save Test" OnClick="btnSubmit_Click"
OnClientClick="return ValidateSaveTest(this);" />
在Javascript中,ValidateSaveTest函数被调用这验证所有字段。
On Javascript, ValidateSaveTest function is called which validates all the fields.
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;
}
我需要一旦收到Ajax响应从javascript提高服务器端的按钮单击事件。
I need to raise server side button click event from javascript once ajax response is received.
推荐答案
您可以从ClientScriptManager的的GetPostBackEventReference 方式:
You can get the required JavaScript code from the ClientScriptManager's GetPostBackEventReference method:
返回一个可以在客户端事件可用于使回发一个串
到服务器。
Returns a string that can be used in a client event to cause postback to the server.
这是通常用于书写的onclick属性上,如&LT控制; ASP:LinkButton的&GT;
,但你可以在你的jQuery回调使用它,以及:
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:
var succeededAjaxFn = function(result) {
//Raise server side button click event. Dont call click side event anymore.
<%= Page.ClientScript.GetPostBackEventReference(btnSubmit, String.Empty) %>;
}
的&LT;%=%GT;
块的上方会写了如下的JavaScript您:
The <%= %>
block above will write out the following JavaScript for you:
__doPostBack('btnSubmit','')
这反过来将表格回发到服务器在ASP.NET认为该按钮被点击,所以服务器端 btnSubmit_Click
被触发这样的方式。
注意,使用这种方法,您可以在C#中引用的实际控制人通过。你并不需要担心它的客户ID,或正确的名称和__ doPostback的的参数()
JavaScript函数。所有这一切都是由 ClientScriptManager
照顾一>当你调用此方法。
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 ClientScriptManager
when you call this method.
这篇关于从Ajax调用JavaScript的提高服务器端的按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!