Quantcast

Pre-compile Web Page And Hide It From Preying Eyes

Get the WebProNews Newsletter:
[ Technology]

There are two main kinds of aspx files which we create in ASP.NET. The first one is created in VS.NET and it has the statement similar to this one at the top of the page :

<%@ Page language="c#" Codebehind="ShowDetails.aspx.cs" AutoEventWireup="false" Inherits="ABSATree.ShowDetails" EnableSessionState="False" enableViewState="False"%>

List of attributes could be much longer than this. There are about thirty possible attributes and the explanation what they do is in MSDN. The other kind of aspx is usually created in ordinary text editor or free environment like ASP.NET Web Matrix. In that kind of aspx file we usually insert our code in side script element and Page directive is much shorter. Usually it looks like this :

<%@ Page Language="CS" % >

If we want to separate code from aspx file and avoid frequent recompilation of code we may do so. That could be achieved with help of Inherits attribute. Inherits contains name of the class which should be inside some assembly located in bin directory and must inherit from System.Web.UI.Page. How do we inherit from Page? We may try like this :

using System;
using System.Web.UI;
public class MyPage : Page
{
   public MyPage()
   {
      this.Load += new System.EventHandler(this.Page_Load);
   }
   private void Page_Load(object sender, System.EventArgs e)
   {
      Response.Write("Hello World!");
   }
}


We will save it as MyPage.cs and compile from command line using csc /t:library MyPage.cs /debug. Assembly must end up in bin directory. Our aspx file will be extremely simple, it will contain only this :

<%@ Page Inherits="MyPage" %>

When we point the browser to that page we will see “Hello World!Hello World!”. Not really what we want to see, Page_Load was called twice. Is that a bug? Not really, using one additional attribute fixes the problem. So aspx file becomes :

<%@ Page Inherits="MyPage" AutoEventWireup="false"%>

Now it is loaded only once. We may also add few controls and see how it works, but this time we will do InitializeComponent like VS.NET instead of doing initialization manually. There is no need to reinvent the wheel. This is aspx file :

 <%@ Page Inherits="MyPage"  AutoEventWireup="false"%>
<HTML>
    <HEAD>
      <title> MyForm </title>
  < /HEAD>
   <body MS_POSITIONING="GridLayout">
      <form id="Form1" method="post" runat="server">
         <TABLE id="Table" cellSpacing="1" cellPadding="1" width="100%" border="0">
            <TR>
               <TD>  <asp:TextBox id="T" runat="server" />  </TD>
            </TR>
            <TR>
               <TD> <asp:Button id="B" runat="server" Text="Done" / > </TD>
            </TR>
            <TR>
               <TD>  <asp:Label id="L" runat="server" />  </TD>
            </TR>
         </TABLE>
      </form>
  < /body>
 </HTML> 

Code behind looks like this :

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public class MyPage : Page
{
&nbsp &nbsp protected TextBox T;
&nbsp &nbsp protected Button B;
&nbsp &nbsp protected Label L;
&nbsp &nbsp override protected void OnInit(EventArgs e)
&nbsp &nbsp {
&nbsp &nbsp &nbsp InitializeComponent();
&nbsp &nbsp &nbsp base.OnInit(e);
&nbsp &nbsp }
&nbsp &nbsp private void InitializeComponent()
&nbsp &nbsp {
&nbsp &nbsp &nbsp this.B.Click += new System.EventHandler(MyClickHandler);
&nbsp &nbsp }
&nbsp &nbsp private void MyClickHandler(object sender, System.EventArgs e)
&nbsp &nbsp {
&nbsp &nbsp &nbsp L.Text = "Hello " + T.Text;
&nbsp &nbsp }
}

When we point the browser to it everything will work fine. There are other good sides of such approach beside hiding code from curious people. We are avoiding recompiling and if we don’t have VS.NET we can use .NET SDK debugger DbgCLR.exe to debug web page written in such a way.

* Originally published at Csharphelp.com

I’m writing code and articles. I have about sixty published articles on different web sites, most of it is on www.CodeNotes.com and currently I’m writing for them. I’m also preparing presentation for Web Services Conference & Exposition in Toronto (http://www.wowgao.com/web_services_conference/index.php?Region=Global). If you have suggestions, questions or lucrative job offers my e-mail is filipbulovic@hotmail.com.

Pre-compile Web Page And Hide It From Preying Eyes
Comments Off
About Filip Bulovic
I'm writing code and articles. I have about sixty published articles on different web sites, most of it is on www.CodeNotes.com and currently I'm writing for them. I'm also preparing presentation for Web Services Conference & Exposition in Toronto (http://www.wowgao.com/web_services_conference/index.php?Region=Global). If you have suggestions, questions or lucrative job offers my e-mail is filipbulovic@hotmail.com. WebProNews Writer
Top Rated White Papers and Resources

Comments are closed.