C# 将另一个asp.net页面加载到当前页面

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

Load another asp.net page into current page

c#asp.net

提问by steventnorris

Using ASP.NET with C#, I would like to load the content of another page into my current page.

在 C# 中使用 ASP.NET,我想将另一个页面的内容加载到我的当前页面中。

I have a main div id="maindiv"on one page with a header above it. The header contains clickable links that route to the same url with a get variable like http://www.mainpage.com&page=nextpage. I would like to use the page get variable in a switch case to load from a specified page into maindiv. I can do the switch case bit fine, but how do I load the response from another page?

id="maindiv"在一页上有一个主 div ,上面有一个标题。标头包含可点击的链接,这些链接使用 get 变量(如http://www.mainpage.com&page=nextpage. 我想在 switch case 中使用 page get 变量从指定的页面加载到 maindiv。我可以把 switch case 做得很好,但是如何从另一个页面加载响应?

采纳答案by jrummell

It sounds like you want to keep the same layout (header, navigation, footer, etc) for all of your pages. ASP.NET 2.0 introduced Master Pages, which are basically layout files that allow you to create Content pages that fill in place holders in the layout.

听起来您希望为所有页面保持相同的布局(页眉、导航、页脚等)。ASP.NET 2.0 引入了母版页,它们基本上是布局文件,允许您创建填充布局中占位符的内容页面。

ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.

ASP.NET 母版页允许您为应用程序中的页面创建一致的布局。单个母版页定义了应用程序中所有页面(或一组页面)的外观和标准行为。然后,您可以创建包含要显示的内容的单独内容页面。当用户请求内容页时,他们会与母版页合并以生成将母版页布局与内容页中的内容相结合的输出。

Here's a sample from the MSDN article.

这是MSDN 文章中的示例。

Master Page:

母版页:

<%@ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
    1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server" >
    <title>Master page title</title>
</head>
<body>
    <form id="form1" runat="server">
        <div><asp:contentplaceholder id="Main" runat="server" /></div>
        <div><asp:contentplaceholder id="Footer" runat="server" /></div>
    </form>
</body>
</html>

Content page:

内容页:

<% @ Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    Main content.
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Footer" Runat="Server" >
    Footer content.
</asp:content>

回答by CoderMarkus

I would suggest either using Panels and showing based on URL variable or use .ASCX user control files and load those based on URL variable if you need code behind logic.

如果您需要逻辑背后的代码,我建议要么使用面板并基于 URL 变量显示,要么使用 .ASCX 用户控制文件并基于 URL 变量加载这些文件。

<asp:Panel ID="pnl1" runat="server" visible="false">
    page 1...
</asp:Panel>

<asp:Panel ID="pnl2" runat="server" visible="false">
    page 2...
</asp:Panel>

回答by Hassan Boutougha

a possible solution is

一个可能的解决方案是

set an iframe like in http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspxyou will take your query string parameter to set source of iframe

http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx 中设置 iframe, 您将使用查询字符串参数来设置 iframe 的来源

frame1.Attributes["src"] = "http://www.live.com" ;

in code behind

在后面的代码中

frame1.Attributes["src"] = nextpage ;

回答by Conrad Lotz

This related post might provide a solution to your problem:

这个相关的帖子可能会为您的问题提供解决方案:

How to display an ASPX in another ASPX's DIV dynamically at runtime?

如何在运行时动态地在另一个 ASPX 的 DIV 中显示一个 ASPX?