3

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.

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):

<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.

Here's how the submit button gets rendered (in view source) by 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" />
4

3 回答 3

4

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

<asp:Button ID="btnSaveChanges" runat="server" OnClientClick="hideEditFields('true');"  Text="Save Changes" onclick="btnSubmit_Click" />
于 2013-06-25T15:56:37.117 回答
1

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

于 2013-06-25T17:10:35.950 回答
1

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

btnTest.OnClientClick = 
    "window.open('" + URL + "'); 
    return true;";
于 2015-04-10T06:50:05.963 回答