2011-06-21 43 views
2

我試圖在每個aspx頁面中包含一個公共導航頁面。該代碼看起來是這樣的:如何在C#ASP.NET(VS 2010)中包含另一個Web文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="canada_Default" %> 

<!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 runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <%   
     Response.WriteFile("../include/navigation.aspx"); 
    %> 

</div> 
</form> 

這裏是navigation.aspx代碼:

<form id="form1" runat="server"> 
<div> 
<asp:DropDownList ID="ddlSites" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSites_SelectedIndexChanged"> 
    <asp:ListItem Selected="True" Text="Global websites" Value="" /> 
    <asp:ListItem Text="Australia" Value="" /> 
    <asp:ListItem Text="Canada" Value="" /> 
    <asp:ListItem Text="Ireland" Value="" /> 
    <asp:ListItem Text="Japan" Value="" /> 
    <asp:ListItem Text="Latin America and the Caribbean" Value="" /> 
    <asp:ListItem Text="Middle East" Value="" /> 
    <asp:ListItem Text="New Zealand" Value="" /> 
    <asp:ListItem Text="Portugal" Value="" /> 
    <asp:ListItem Text="Singapore" Value="" /> 
    <asp:ListItem Text="Spain" Value="" /> 
    <asp:ListItem Text="United Kingdom" Value="" /> 
    <asp:ListItem Text="United States" Value="" /> 
</asp:DropDownList> 

的下拉列表不會在瀏覽器中顯示。我知道一種方法是使用母版頁(我打算有一天會做),但是對於這個項目,我想要做一些簡單的事情 - 這很簡單但很實用。

感謝您的幫助!

+1

爲什麼不留出的原油和顯然是非功能性的方式,從一開始就使用母版頁? – Filburt

+0

我同意Filburt:考慮到這個問題,它顯然是非功能性的,因此您應該回到常規實踐並使用母版頁。 – NotMe

回答

6

看看使用用戶控制:ASP.NET User Controls Overview (MSDN)。從MSDN頁面

報價:

用戶控件實質上更容易 創造出比自定義控件, 因爲可以重用現有 控制。它們使得它特別容易用複雜的 用戶界面元素創建控件。

而且,看看這個MSDN鏈接:How to: Include a User Control in an ASP.NET Web Page

您可以創建您的用戶控件(.ascx),並把它作爲如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="canada_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<%@ Register TagPrefix="uc" TagName="uc1" Src="~/myUserControl.ascx" %> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <uc:uc1 id="myUC" runat="server" /> 
</div> 
</form> 
+0

以及我感到尷尬。我實際上使用usercontrols爲我的網絡應用程序的其他方面-_- –

+0

@ user808710:我聽到你。有時候解決方案就是你做過一千次的事情,但無論出於何種原因,這都不是你考慮的事情。 –

3

你想要的是一個old-school server-side-include

代碼:

<html> 
    <body> 
     <!-- #Include virtual=".\include\header.inc" --> 
     Here is the main body of the .aspx file. 
     <!-- #Include virtual=".\include\footer.inc" --> 
    </body> 
</html> 
+0

你可以通過老派服務器端包含更具體嗎? –

+0

添加了msdn的鏈接,以及一些示例代碼(來自msdn頁面) –

+0

quick question:是不是''標籤用於評論? –

0

也許一個簡單的方法來實現,這將是使用用戶控件。

相關問題