简体   繁体   English

AJAX响应后的ASP.NET回发

[英]ASP.NET PostBack after AJAX response

I'm working with a legacy web application - there's a form with some text inputs and some file inputs, and the form is saved on a PostBack.. 我正在使用旧版Web应用程序-有一个带有一些文本输入和一些文件输入的表单,并且该表单保存在PostBack上。

HTML: HTML:

<input id="txt1" name="txt1" type="text" />
<input id="hfd1" name="hfd1" type="hidden" value="-1" />
<input id="file1" name="file1" runat="server" type="file" />
<asp:Button ID="btnSave" runat="server" OnClientClick="return PrepareSave()" OnClick="btnSave_Click" Text="Save" />

JS: JS:

function PrepareSave() {
    $('#hfd1').val($('#txt1').val());
    return true;
}

I need to send an AJAX request inside PrepareSave() to validate a field on the server, and depending on the response, carry on with the PostBack or don't. 我需要在PrepareSave()内发送一个AJAX请求以验证服务器上的字段,并根据响应选择是否使用PostBack进行操作。 I've tried this: 我已经试过了:

function PrepareSave() {
    Project1.Web.Services.AJAXService1.CheckSomething(someString, function (result) {
        if (result == true) {
            // continue PostBack
            $('#hfd1').val($('#txt1').val());
            return true;
        }
        else {
            // abort PostBack
            alert('invalid');
            return false;
        }
    });
}

But it's failing - it doesn't post back regardless of the AJAX response. 但这是失败的-无论AJAX响应如何,它都不会回发。 I've tried implementing the accepted answer here but it's having the same problems. 我已经尝试过在这里实现可接受的答案但是它也有同样的问题。 What can I do to make it work? 我该怎么做才能使其正常工作?

I've tried simplifying that accepted answer above, to test it: 我尝试简化上面接受的答案,以对其进行测试:

<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" OnClientClick="PrepareSave(); return false;" Text="Save" />
<asp:Button ID="btnSaveHidden" runat="server" ClientIDMode="Static" OnClick="btnSave_Click" Visible="False" />

function PrepareSave() {
    $('#btnSaveHidden').click(); // does nothing. also tried .trigger('click')
}

In code behind you can do: 在后面的代码中,您可以执行以下操作:

ClientScript.RegisterStartupScript(GetType(), "ReloadParent", "window.parent.location.href = window.parent.location.href;", true);

or in js, use the script part by itself: 或在js中,单独使用脚本部分:

window.parent.location.href = window.parent.location.href;

hth. hth。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM