2

I'm trying to include a common navigation page into each aspx page. The code looks something like this:

<%@ 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>

Here is the navigation.aspx code:

<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>

The dropdownlist is not displaying in the browser. I know one way is to use a Master page (which I plan on doing one day), but for this project, I would like to do something simple like this -- it's crude but functional.

Thanks for your help!

2
  • 1
    Why not leaving out the crude and obviously non-functional way and use a Master Page from the start? Commented Jun 21, 2011 at 15:12
  • I agree with Filburt: considering the question, it's obviously non-functional and therefore you should go back to common practices and use a master page. Commented Jun 21, 2011 at 15:40

3 Answers 3

6

Take a look at using User Controls: ASP.NET User Controls Overview (MSDN).

Quote from the MSDN page:

User controls are substantially easier to create than custom controls, because you can reuse existing controls. They make it particularly easy to create controls with complex user interface elements.

Also, take a look at this MSDN link: How to: Include a User Control in an ASP.NET Web Page

You would create your User Control (.ascx) and include it as follows:

<%@ 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>
Sign up to request clarification or add additional context in comments.

2 Comments

well i feel embarassed. i've actually used usercontrols for other aspects of my web app -_-
@user808710: I hear ya. Sometimes the solution is something you've done a thousand times but, for whatever reason, it just isn't something you considered.
3

what you want is an old-school server-side-include

code:

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

4 Comments

can you be more specific by old-school server-side include?
added a link to msdn, and some example code (from the msdn page)
quick question: isn't the '<!-- -->' tag for commenting?
@user808710 that is corrrect. you can remove those, if you like.
0

Perhaps an easy way to achieve this would be to use a User Control.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.