【问题标题】:Raise Server side button click event from javascript in ajax call在ajax调用中从javascript引发服务器端按钮单击事件
【发布时间】:2012-01-07 16:20:04
【问题描述】:

我在页面上有一个提交按钮。

<asp:Button ID="btnSubmit" runat="server" Text="Save Test" OnClick="btnSubmit_Click"
                                        OnClientClick="return ValidateSaveTest(this);" />

在 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;
    }

一旦收到 ajax 响应,我需要从 javascript 引发服务器端按钮单击事件。

【问题讨论】:

    标签: c# javascript jquery asp.net


    【解决方案1】:

    您可以从 ClientScriptManager 的 GetPostBackEventReference 方法中获取所需的 JavaScript 代码:

    返回一个字符串,该字符串可用于客户端事件以导致回发 到服务器。

    这通常用于在 &lt;asp:linkButton&gt; 等控件上编写 onclick 属性,但您也可以在 jQuery 回调中使用它:

    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:

    __doPostBack('btnSubmit','')
    

    这反过来会以 ASP.NET 认为按钮被点击的方式将表单回发到服务器,因此触发服务器端 btnSubmit_Click。

    请注意,使用此方法,您可以将 C# 引用传递给实际控件。您无需担心它的客户端 ID,或者 __doPostback() JavaScript 函数的正确名称和参数。当您调用此方法时,所有这些都由ClientScriptManager 处理。

    【讨论】:

    • 非常感谢......它成功了。当我提到msdn时,在您最初的回答中,我不明白。但是经过你的解释。解决方案不仅解决了,而且还了解正在发生的事情......
    • Onclick 事件是如何引发的?你有没有提到这段代码中引发的 Onclick 事件?我可以理解。
    • @AliForoughi 我编辑了我的答案以解释如何触发服务器端事件,这有帮助吗?
    • @AliForoughi:是的,它触发了服务器端事件。
    • @AliForoughi:我从 msdn referecne 中了解到的是Returns a string that can be used in a client event to cause postback to the server。因此,使用 GetPostBackEventReference 会给出需要为控件引发的事件。来自 msdn goo.gl/nKlGf The GetPostBackEventReference method can be used with the Button control when the UseSubmitBehavior property is false. In this scenario, the GetPostBackEventReference method returns the client postback event for the Button control
    【解决方案2】:

    试试这个

    __doPostBack('<%=btDemo.ClientID%>','OnClick');
    

    注意开头的双下划线

    如果用户点击了按钮,这应该会触发回发

    【讨论】:

    • 这是回发,但不会引发按钮的服务器端按钮点击事件。
    • ClientID 对我也不起作用,但 UniqueID 起作用了。我的代码缠绕在一堆 SharePoint 2010 标记中,我注意到有人提到使用此属性。还有 ;对我也有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多