Solved

Using Include tag in asp.net

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 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

18 Experts available now in Live!

Get 1:1 Help Now