Solved

Using Include tag in asp.net

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBScript on Html 15 65
Asp in script 6 51
asp classic find word in string and get its value 7 52
2012 SQL to JSON Select 5 45
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

739 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