Solved

Using Include tag in asp.net

Posted on 2004-04-12
6
177 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
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…
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:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now