Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Include tag in asp.net

Posted on 2004-04-12
6
Medium Priority
?
193 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
4 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 90 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

876 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