J
John Rivers
This shows you how to implement html rendering methods
and libraries in ASPX pages.
This also means super fast debugging as no re-initialising
of IIS is required after editting!
This gets around the ridiculous situation of ASPX pages
not being allowed to have methods.
I am working on a way to do this with classes as well!
Here is how to do it:
you will create two ASPX pages:
- FixASP.asp (the page that actually gets called)
- Library.asp (the page that provides library methods to FixASP.asp)
the key thing to note is that we are tricking the ASP.NET compiler
by closing the hidden render method with a brace, then we omit
the last closing brace of our code because ASP.NET will put it in for
us!
Contents of FixASP.asp:
(start)
<%
InitLib(__output, parameterContainer);
PageStart("ASP.NET is sane again!");
PageHeader();
PageMethod();
PageFooter();
PageEnd();
%><!--#Include File="Library.aspx"--><%
}
void PageMethod() {
%>this is the page method<%
%>
(end)
Contents of Library.asp:
(start)
<%}
//Don't change this bit
private System.Web.UI.HtmlTextWriter __output;
private System.Web.UI.Control parameterContainer;
void InitLib(System.Web.UI.HtmlTextWriter o, System.Web.UI.Control pc)
{
__output = o;
parameterContainer = pc;
}
//Convenience methods go here
string HTML(string data) {
return HttpUtility.HtmlEncode(data);
}
string URL(string data) {
return HttpUtility.UrlEncode(data);
}
string QPX(string name, string val) {
return URL(name) + "=" + URL(val) + "&";
}
//Add your rendering methods here!
void RenderLink(string Title, string URL) {
%><a href="<%=HTML(URL)%>"><%=HTML(Title)%></a><%
}
void PageStart(string Title) {
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title><%=HTML(Title)%></title>
</head>
<body><%
}
void PageHeader() {
%><h1>This is the page header</h1><hr/><%
}
void PageFooter() {
%><hr/><h1>This is the page footer</h1><%
}
void PageEnd() {
%></body>
</html><%
}
//Don't change anything after this
void ButtPlug() {
//this is the end
%>
(end)
now set FixASP.asp as your StartPage
and hit F5
and libraries in ASPX pages.
This also means super fast debugging as no re-initialising
of IIS is required after editting!
This gets around the ridiculous situation of ASPX pages
not being allowed to have methods.
I am working on a way to do this with classes as well!
Here is how to do it:
you will create two ASPX pages:
- FixASP.asp (the page that actually gets called)
- Library.asp (the page that provides library methods to FixASP.asp)
the key thing to note is that we are tricking the ASP.NET compiler
by closing the hidden render method with a brace, then we omit
the last closing brace of our code because ASP.NET will put it in for
us!
Contents of FixASP.asp:
(start)
<%
InitLib(__output, parameterContainer);
PageStart("ASP.NET is sane again!");
PageHeader();
PageMethod();
PageFooter();
PageEnd();
%><!--#Include File="Library.aspx"--><%
}
void PageMethod() {
%>this is the page method<%
%>
(end)
Contents of Library.asp:
(start)
<%}
//Don't change this bit
private System.Web.UI.HtmlTextWriter __output;
private System.Web.UI.Control parameterContainer;
void InitLib(System.Web.UI.HtmlTextWriter o, System.Web.UI.Control pc)
{
__output = o;
parameterContainer = pc;
}
//Convenience methods go here
string HTML(string data) {
return HttpUtility.HtmlEncode(data);
}
string URL(string data) {
return HttpUtility.UrlEncode(data);
}
string QPX(string name, string val) {
return URL(name) + "=" + URL(val) + "&";
}
//Add your rendering methods here!
void RenderLink(string Title, string URL) {
%><a href="<%=HTML(URL)%>"><%=HTML(Title)%></a><%
}
void PageStart(string Title) {
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title><%=HTML(Title)%></title>
</head>
<body><%
}
void PageHeader() {
%><h1>This is the page header</h1><hr/><%
}
void PageFooter() {
%><hr/><h1>This is the page footer</h1><%
}
void PageEnd() {
%></body>
</html><%
}
//Don't change anything after this
void ButtPlug() {
//this is the end
%>
(end)
now set FixASP.asp as your StartPage
and hit F5