C# 在 ASP.NET 中,如何在 OnClientClick 之后执行 OnClick?

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

How do I make OnClick execute after OnClientClick in ASP.NET?

c#javascriptasp.net

提问by timbck2

I am working on an ASP.NET web forms application in C#. I have a button that needs to run some client-side Javascript (using the OnClientClick attribute), then run server-side code (using OnClick) if the client-side code returns true. I've done this plenty of times before with jQuery-ui confirmation dialogs, however this time my client-side code isn't a confirmation dialog and it isn't working -- the server-side (OnClick) event never gets triggered even though I'm explicitly returning true.

我正在使用 C# 开发 ASP.NET Web 表单应用程序。我有一个按钮需要运行一些客户端 Javascript(使用 OnClientClick 属性),然后在客户端代码返回 true 时运行服务器端代码(使用 OnClick)。我以前用 jQuery-ui 确认对话框做过很多次这样的事情,但是这次我的客户端代码不是确认对话框,它不起作用——服务器端 (OnClick) 事件甚至从未被触发虽然我明确返回true。

Here's the client-side code in question, which is working (I've verified that it's returning the correct value with an alert() call):

这是有问题的客户端代码,它正在工作(我已经验证它通过 alert() 调用返回了正确的值):

<script type="text/javascript">
    //Function to hide edit fields
    function hideEditFields(retVal) {
        //Hide all edit divs
        var editName = document.getElementsByClassName("editField");
        for (i = 0; i < editName.length; i++) {
            editName[i].style.display = 'none';
        }
        //Show all view divs
        var viewName = document.getElementsByClassName("viewField");
        for (i = 0; i < viewName.length; i++) {
            viewName[i].style.display = 'block';
        }

        //Make investigated & corrective action checkboxes non-highlighted
        $('<%=cbInvestigatedY.ClientID%>').removeClass("editable");
        $('<%=cbInvestigatedN.ClientID%>').removeClass("editable");
        $('<%=cbCorrectiveY.ClientID%>').removeClass("editable");
        $('<%=cbCorrectiveN.ClientID%>').removeClass("editable");

        //Show edit button
        var editButton = document.getElementById("editButton");
        editButton.style.display = 'block';
        //Hide save button
        var saveButton = document.getElementById("saveButton");
        saveButton.style.display = 'none';

        //alert("returning " + retVal);
        return retVal;
    }
</script>

And here's the button that calls this function:

这是调用此函数的按钮:

<asp:Button ID="btnSaveChanges" runat="server" OnClientClick="return 
    hideEditFields(true);" OnClick="btnSubmit_Click" Text="Save Changes" />

I've tried this with UseSubmitBehavior="true"and without, which seems to make no difference. I'm getting no errors in the Javascript console, and the Javascript code is doing what it's supposed to do, it's just not calling the server-side method.

我试过这个UseSubmitBehavior="true"和没有,这似乎没有什么区别。我在 Javascript 控制台中没有收到任何错误,并且 Javascript 代码正在做它应该做的事情,只是没有调用服务器端方法。

Here's how the submit button gets rendered (in view source) by ASP.NET:

以下是 ASP.NET 呈现提交按钮的方式(在视图源中):

<input type="submit" name="ctl00$MainContent$btnSaveChanges" value="Save Changes"
    onclick="return hideEditFields(true);WebForm_DoPostBackWithOptions(new
    WebForm_PostBackOptions(&quot;ctl00$MainContent$btnSaveChanges&quot;, &quot;&quot;,
    true, &quot;&quot;, &quot;&quot;, false, false))"
    id="ctl00_MainContent_btnSaveChanges" />

采纳答案by timbck2

The real problem was that my server-side method wasbeing executed, but was doing nothing, because it compares the original values in the form (stored as hidden fields) with the new values entered in the form's text boxes and other controls and stores them if they have changed. But I was making the noob mistake of not enclosing the code that populated the form in a check for IsPostBack (and I know better!), so the comparison was always coming out equal, so nothing was changing. See Why is ASP.NET submitting the original value of a TextBox control when the contents have been changed?for more details.

真正的问题是,我的服务器端方法执行,但什么都不做,因为它的原始值的形式(存储为隐藏字段),在窗体的文本框和其他控件,并将它们存储输入的新值进行比较如果他们改变了。但是我犯了一个菜鸟错误,没有将填充表单的代码包含在 IsPostBack 的检查中(我知道得更好!),所以比较结果总是相等,所以没有任何变化。请参阅当内容已更改时,ASP.NET 为什么要提交 TextBox 控件的原始值?更多细节。

回答by Asif Mahamud

Pleasea try to remove the returnfrom calling in the client click. I think you should call the function onclientclicklike this:

请尝试return从客户端单击中删除调用。我认为你应该onclientclick像这样调用函数:

<asp:Button ID="btnSaveChanges" runat="server" OnClientClick="hideEditFields('true');"  Text="Save Changes" onclick="btnSubmit_Click" />

回答by Manjeet Kumar

On a client click, set the return value to true:

在客户端单击时,将返回值设置为 true:

btnTest.OnClientClick = 
    "window.open('" + URL + "'); 
    return true;";