开发者

Problem after using jQuery to load external page(s) (aspx) into div(s)

开发者 https://www.devze.com 2023-02-17 15:04 出处:网络
I\'ve a page which consolidates different sections by different .aspx pages. Those pages will be loaded into corresponding div(s) dynamically by using jQuery.load(). They all are rendered properly, ho

I've a page which consolidates different sections by different .aspx pages. Those pages will be loaded into corresponding div(s) dynamically by using jQuery.load(). They all are rendered properly, however, when clicking开发者_如何学运维 on any asp:button in any section page, the entire main page will be replaced by the section page even the asp:button is just popping up a message box. I found no way to fix it out, please kindly advise!

Many thanks. William Choi


When you submit the page your request is going to the nested page and it's response is only for that page. The browser receives the response from the nested page and just displays what it gets back.

Here is what I'm doing which is similar.

I have one main page and within that page I load sections via Ajax. The sections are ASP.NET usercontrols (not pages) here is why: Pages have their own forms and don't play well when nested. The ajax request passes some parameters loads the control then renders the control using a dummy blank page(which I will explain further down). The HTML generated is sent back to the page and loaded into a div.

Here is how I load the control (in this example "FormlessPage" is the dummy page):

public class FormlessPage : Page { public override void VerifyRenderingInServerForm(Control control) { } }

//Ajax call
FormlessPage page = new FormlessPage();
UserControl ctrl = null;
ctrl = (UserControl)page.LoadControl("~/UserControls/someUC.ascx");
// Initialize parameters
// Add the control to the page
page.Controls.Add(ctrl);

// Render the page and capture the resulting HTML.
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
// Return that HTML, as a string.
json = writer.ToString();


Most probable cause will be that your button is posting back to the corresponding page. You should disable the post back by returning false from your js function/code that get launched on click of button.

You have not mentioned that what behavior you want to achieve here - if consolidation is merely for creating a layout then you should exclude un-relevant tags such as head, body & forms from page output before putting into the div. A better technique here would be to have user controls (instead of pages) and then use some handler that would load the requested user control on a temporary page & use HttpServerUtility.Execute method to capture the output - check this article that describes this technique. On the other hand, you wants to retain child pages behavior on post back then iframes would be the way to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号