简体   繁体   English

当我使用jQuery模型加载aspx页面时,无法使用该aspx页面的服务器端方法

[英]When I load an aspx page using jQuery model, I am unable use the server side methods of that aspx page

When I load an aspx page to a popwindow using jQuery model using below code. 当我使用下面的代码使用jQuery模型将aspx页面加载到弹出窗口时。

function OpenExceptions() {
$('#Equipmentdialog').load('Popups/Test1.aspx', function () {
   $(this).dialog({
        modal: true,
        width: 900,
        height: 400
    });
});

} }

I am unable to call any server side method(C# button click) in the Test1.Aspx method, When I call the the server side events, I am getting resource not found exception? 我无法在Test1.Aspx方法中调用任何服务器端方法(单击C#按钮),当我调用服务器端事件时,我得到资源未找到异常吗?

Can someone please explain me what is the reason? 有人可以解释一下原因吗?

Thanks 谢谢

Update: this is the error I am getting 更新:这是我得到的错误

在此处输入图片说明

I looked at your sample project and the issue you are experiencing is very simple to explain but I am afraid it's not going to be so simple to make it work. 我查看了您的示例项目,您遇到的问题很容易解释,但是恐怕要使它正常工作并不是那么简单。

First I'll explain what's causing the exception. 首先,我将解释导致异常的原因。

When the dialog loads, it sets its content to whatever output is generated by the Test.aspx page. 对话框加载后,它将其内容设置为Test.aspx页生成的任何输出。 Since the page generates this HTML when you navigate to it: 由于页面在导航到该页面时会生成此HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form name="form1" method="post" action="Test.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTI2NTY4ODI3MWRkmRTYlsUe3rVbAI2jDoNeA5EPuo8=" />
</div>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKd2MeEBAKM54rGBl+Fr2fdw6uP6072WYTIw/gz9N5E" />
</div>
    <div>
        <span id="Label1">Label</span>
        <br />
        <br />
        <input type="submit" name="Button1" value="Button" id="Button1" />

    </div>
    </form>
</body>
</html>

The dialog ends up displaying a form whose action is set to Test.aspx ; 对话框最终显示一个表单,其动作设置为Test.aspx therefore, when you click on the Button on the dialog, it attempts to post back the form to Test.aspx but it doesn't find it because this page is inside Popups/Test.aspx . 因此,当您单击对话框上的“ Button ”时,它会尝试将表单回发到Test.aspx但找不到该表单,因为此页面位于Popups/Test.aspx内部。 Now, in order to "fix it" (I say this in quotes because it's not really going to fix anything), you could change the dialog's HTML by brute force; 现在,为了“修复”它(我用引号说这是因为它实际上并不能修复任何东西),您可以通过蛮力更改对话框的HTML。 doing something like this: 做这样的事情:

function OpenExceptions() {
            $('#Equipmentdialog').load('Popups/Test.aspx #form1', function (response, status, xhr) {
                response = response.replace('action="Test.aspx"', 'action="Popups/Test.aspx"'); //Make sure the form's action is accurate
                $(this).html(response);
                $(this).dialog({
                    modal: true,
                    width: 900,
                    height: 400
                });
            });
        }

And now, when you click the Button you will no longer receive a Resource Not Found Exception ; 现在,当您单击Button ,将不再收到Resource Not Found Exception however, because this causes a normal post back the dialog will disappear , the button will post back the page and the label you have on the page will display the current date and time. 但是,由于这会导致正常的回发, 因此对话框将消失 ,按钮将回发页面,并且页面上的标签将显示当前日期和时间。

Again, this all happens because you are doing normal post backs as opposed to Ajax requests. 同样,这都是因为您执行的是普通的回发而不是Ajax请求。 My approach above would work if the Button in the Test.aspx page performs an Ajax request but not the kind you get when you use Update Panels and Script Managers. 如果Test.aspx页面中的Button执行Ajax请求,而不是使用更新面板和脚本管理器时获得的那种Ajax请求,则上面的方法将起作用。 You won't be able to use those tools because of the way they work internally... 由于这些工具的内部工作方式,您将无法使用它们。

If you are looking to use Ajax in your app, I recommend you look at WCF Web Services in conjunction with JQuery. 如果要在应用程序中使用Ajax,建议您将WCF Web服务与JQuery结合使用。 There are many good tutorials online on the topic. 在线上有很多关于该主题的好教程。

I hope my answer at least helps you understand why this isn't working for you and why it won't work if you continue to use this approach. 我希望我的回答至少可以帮助您理解为什么这种方法对您不起作用,以及如果您继续使用此方法,为什么它将不起作用。 There are many hacks that you could apply to make it work, but I can't think of a single one that's going to be easy to maintain and scale going forward. 您可以应用许多技巧来使其正常运行,但是我无法想到一个易于维护和扩展的技巧。 The best approach is to do Ajax properly, using WCF web services (or Page Methods) and JQuery. 最好的方法是使用WCF Web服务(或Page Methods)和JQuery正确执行Ajax。

暂无
暂无

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

相关问题 我可以在aspx文件中使用Page_Load吗? - Can I use Page_Load in my aspx file? 在使用 jspdf 在加载 aspx 页面上下载到 pdf 时,我得到的 pdf 大小根据我打开这个 aspx 的设备的大小而变化 - While downloading on load aspx page to pdf using jspdf ,I am getting pdf which size is varying according to the size of device I am opening this aspx 如何在包含aspx页面和mvc的网站上对页面加载(服务器端)进行检查? - How can I run a check on page-load (server-side) on a website which includes both aspx pages and mvc? 在 aspx 页面中无法识别服务器端对象 - A server side object cannot be recognized in aspx page 将文件上传到ASPX服务器端页面 - Uploading a file to an ASPX server-side page 如何在特定的另一个 aspx 页面中加载一个 aspx 页面<div>标签</div><div id="text_translate"><p>我想用来自另一个 *.aspx 页面的内容更新页面的某个部分,从而避免只有一个(非常)长的代码页面。</p><p> 所以:如何在特定标签中的另一个 aspx 页面中加载一个 aspx 页面,而不必重新加载整个页面。</p></div> - How can I load an aspx page in another aspx page in a specific <div> tag 无法使用jQuery Ajax调用来调用aspx页面Web方法? - Unable to call aspx page web method using jquery ajax call? 在更新面板中加载aspx页面 - load an aspx page in an updatepanel 我无法将我班级的引用添加到我的aspx页面中 - I am not able to add reference of my class into my aspx page 我试图隐藏绑定到文本框的ASPX页面中的文本 - I am trying to hide text in a ASPX page that is tied to a textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM