Solved

Using Include tag in asp.net

Posted on 2004-04-12
6
182 Views
Last Modified: 2013-11-18
Hello:

When I did classic asp in the past, I would use something like <!-- #Include file="logo.aspx" --> to display the same header menu across several of my pages.

I was wondering, how do you go about doing it in asp.net now?

Thanks!
0
Comment
Question by:gregca80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 10804685
create a user control
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10804702
put the followinf directive at the top of the page

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="UCLogo.ascx.vb" Inherits="YourProject.UCLogo" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

and then the code of your include file. Save this file as UCLogo.ascx

Then register your user control on your aspx page by putting the following at the top of the page, after @Page directive:

<%@ Register TagPrefix="UCL" TagName="LogoControl" Src="UCLogo.ascx" %>

then use this code to display your control:

<UCL:LogoControl ID="myLogo" runat="server" />
0
 

Accepted Solution

by:
mitcheloc earned 30 total points
ID: 10807010
If you want the same look and feel acrossed all your pages here is a great solution (this is what I use):

Create a class in your project called SkeletonPage and place this in it:
      public class SkeletonPage : Page
      {
//--------------------------------------------------------------------------------------------
            protected override void Render(HtmlTextWriter writer)
            {  
                  StringWriter      swriter            = new StringWriter();
                  HtmlTextWriter      buffer            = new HtmlTextWriter(swriter);
                  base.Render(buffer);

                  string                  basehtml      = swriter.ToString();
                  string                  template      = basehtml.Substring(basehtml.IndexOf("<template>"));
                  template                              = template.Substring(10);
                  template                              = template.Substring(0, template.IndexOf("</template>"));

                  string                  content            = basehtml.Substring(basehtml.IndexOf("<content>"));
                  content                                    = content.Substring(9);
                  content                                    = content.Substring(0, content.IndexOf("</content>") + 10);

                  string                  pagehtml      = template.Replace("%content%", content);
                  writer.Write(pagehtml);
            }
//--------------------------------------------------------------------------------------------
            private string GetTextFromAssembly(string file)
            {
                  //string                  temphtml      = GetTextFromAssembly("Namespace.template.htm");
                  Stream stream                        = Assembly.GetExecutingAssembly().GetManifestResourceStream("TitaniumSoft.Mail.." + file);
                  System.IO.StreamReader sr      = new System.IO.StreamReader(stream);
                  string text                              = sr.ReadToEnd();
                  sr.Close();
                  return text;
            }
//--------------------------------------------------------------------------------------------
      }
}


Then under your project add a file called "template.htm" and place the two following variables in it where you want each element to be placed, think of this as your master page...you can design the look and feel of your pages here.
<%menuleft%>
<%pagebody%>

Now make sure it is in your main project folder not in a subdirectory because this will affect how it is accessed. Then click on the file in your solution explorer and press F4 to get the properties window, now change build action to Embedded Resource and Custom Namespace to "Namespace".


Now under your aspx page press F7 to view the code and change the public class PageName : Page to public class Pagename : SkeletonPage.

Last step:
Use this as your temporary asp.net page html code and modify it to your needs.

<html>
<body>
<form runat="server">
      <menuleft>
            <% // your menu control here %>
      </menuleft>
      <pagebody>
            <% // place your page code and UI here %>
      </pagebody>
</form>
</body>
</html>

Replace menucontrol with the solution YZlat gave you and do the rest of your page between the pagebody tags.


The reason for doing everything this way is that it gives you a master control page that you can change the look of everything very easily. Also this is the way .NET works so it renders everything you could need with a properly nested control hierarchy. The SkeletonPage.cs code is necessary to parse the template.htm and your main pages html and to put it all together properly.

Mitchel
0
 

Author Comment

by:gregca80
ID: 12700010
I forgot to award points for this, but finally did. I never tried out the solution, but gave the points for a whole hearted effort in helping out. I've switched to PHP. :p

cheers
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question