Asp.net|include A Common Way to builds the navigation and layout for A Asp-driven of the to use include files. Most Advanced ASP developers know that's the it is best to encapsulate the functionality of the Include file I n a Sub or Function, and then to call this routine from the page, which is including the file. This is avoids problems with variable scope, allows parameters to is passed easily to the include file, and makes the code EA Sier to read.
As these are sites are migrated to use ASP.net, it are likely that classic the ASP and asp.net pages would exist side-by-side, which is one of the touted advantages of asp.net. Unfortunately, there is no built-in way for a ASP.net page to take advantage of a classic ASP include file, which means T Hat the obvious solution if you want to maintain a consistent look and feel are to duplicate the look of the classic ASP Te Mplate in a asp.net user control. Unfortunately, this means duplicating presentation logic, and inevitably, the classic ASP template and the asp.net E'll get out of sync.
To overcome this problem, I built a simple user control that uses ASP. NET ' s built-in page-scraping library, HttpWebResponse, to grab a template ASP file and render it in my. aspx page. The template ASP file is simply a page so includes my presentation logic include file, and calls the functions to render The HTML, passing through any querystring parameters it has to those functions (such as in page title for a header inclu De file).
For this demonstration, there are six files:
Layout_sample.asp-this ASP page uses the include file the standard Classic ASP way.
Header_include.asp-this is i actual ASP include file, which has a function called Showheader that'll display the HTML For the page header wherever it is called. The page header is just a HTML table with the title of the page in it. The title is passed into Showheader as a required parameter.
Header_template.asp-this is my template file. All it does are include my ASP header include file, call the Showheader function, and insert the QueryString parameter for The title. If you click on this page, add "? Title=foo" to the URL to the "I" uses the title from the querystring.
Showheader.ascx--My user control this scrapes an ASP page to get the HTML to insert in my. aspx page.
ShowHeader.ascx.cs-The Code-behind file for my user control.
Layout_sample.aspx-this asp.net page would use the Classic ASP layout
The Classic ASP example is very simple. All it does are include a file, call Showheader, and wrap it all in a basic HTML page:
1 <%option explicit%>
2 <!--#INCLUDE file= "header_include.asp"-->
3 <%
4 ' Declare Variables
5 Dim Title
6
7 title = "Sample Layout"
8%>
9 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/REC-html40/loose.dtd" >
<title><%=title%></title>
<body>
<% call Showheader (title)%>
<p>
The This is the main content of my sample Classic ASP page. Compare it to the
<a href= "layout_sample.aspx" >asp.net version</a>.
</p>
</body>
The include file is equally simple--"a single" that outputs some HTML. Note So this file can is called by either VBScript or JScript ASP pages, and avoids the any context switching. It could also be called the several times to one page, if need be.
1 <script runat= "Server" language= "VBScript" >
2 Sub Showheader (title)
3 Response.Write "<table width=" "100%" "bgcolor=" "#CC0000" "border=" "1" ">"
4 Response.Write "<tr><td align=" "Center" "><b>" & title & </b></tr></td> "
5 Response.Write "</table>"
6 End Sub
7 </script>
Now-let's take a look at the real "hack" section of this implementation, the dummy ASP page which is no more than a page That's calls our include file (header_template.asp):
Finally, we can look at the "user control" makes this whole thing work. It ' s pretty simple. All it does are use the HttpRequest object this is built into ASP.net to grab the header_template.asp page and insert it in to the ASP.net page. Something similar could is done with the Asphttp object in Classic ASP. Here ' s the file:
Showheader.ascx
1 <%@ Control language= "C #" src= "ShowHeader.ascx.cs"
2 inherits= "ASPAlliance.UserControls.showHeader"%>
3 <%@ OutputCache duration= "3600" varybyparam= "None"%>
4 <!--Header Cached: <%=System.DateTime.Now%>-->
5 <asp:literal id= "header" runat= "Server"/>
ShowHeader.ascx.cs
1 namespace Aspalliance.usercontrols
2 {
3 using System;
4 using System.IO;
5 using System.Net;
6 using System.Text.RegularExpressions;
7 using System.Web;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10
One public abstract class ShowHeader:System.Web.UI.UserControl
12 {
//Declare Controls
protected System.Web.UI.WebControls.Literal Header;
Public String title = "";
16
Showheader public () {
this. EnableViewState = false;
19}
20
private void Page_Load (object sender, System.EventArgs e)
22 {
header. Text = Readhtmlpage ("http://www.aspalliance.com/stevesmith/articles/examples/includelets/header_template.asp?title=" +
Title + "&" +
request.servervariables["Query_string"]);
header. Text = Regex.Replace header. Text,
"</title>",
Title + "</title>");
header. Text = Regex.Replace header. Text,
"/libraryaspa/ssheader.asp",
request.servervariables["URL"]);
32}
33
-Private string Readhtmlpage (string url)
35 {
WebResponse Objresponse;
Panax Notoginseng WebRequest objrequest = System.Net.HttpWebRequest.Create (URL);
Objresponse = Objrequest.getresponse ();
StreamReader sr = new StreamReader (Objresponse.getresponsestream ());
return Sr. ReadToEnd ();
41}
42}
43}
44
45
46
This is really pretty straightforward. In the Page_Load of the control, we grab use the HttpWebResponse object to scrape the contents of header_template.asp, PAS Sing it we public, page_title. We then suck in the "result" and display it in a asp:label tag. You might the would be just atrociously slow, but in practice it works reasonably. Throw a page-level output cache on your. aspx page, and any performance problems you might encounter disappear anyway. The only issue I ' ve run into thus far is this sometimes images just don ' t show up through the web-scraper. This is usually happens with larger images, so I am I-I-it has something to doing with the scraper (HttpWebResponse) object Runni Ng out of the time before it needs to return.
To conclude this example, let's take a look at one of the last page, the ASP.net file which uses this control:
1 <%@ Page language= "C #" trace= "False"%>
2 <%@ Register tagprefix= "sstemplate" tagname= "Showheader" src= "Showheader.ascx"%>
3 <%@ OutputCache duration= varybyparam= "*"%>
4 <script runat= "Server" >
5 String page_title = "Sample asp.net Layout";
6 void Page_Load (Object Src, EventArgs E) {
7//can set the title here programatically
8 header.title = page_title;
9//or down below we could declaratively add title= "Sample asp.net Layout" to our tag.
10}
</script>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/REC-html40/loose.dtd" >
<title><%=page_title%></title>
<body>
<sstemplate:showheader id= "header" runat= "Server"/>
<p>
<p>
This is the main content of my sample asp.net page. Compare it to the
<a href= "layout_sample.asp" >classic ASP version</a>.
</p>
</p>
Num </body>
This page is written in C #, just because I can, and also demonstrates how asp.net allows for easy code interoperability. In the case I have a C # page invoking a vb.net user control. In fact, I ' m even using a Classic ASP VBScript via a ASP include file, although somewhat indirectly!
By using this user control, you can maintain a single location for your site ' s layout templates, rather than has to Mai Ntain two sets of layout files. Once all of your. asp files are converted to. aspx files, your controls are already where and you can simply delete R. asp templates and includes and move the layout HTML into your controls directly. Hope this helps!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.