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
{
protected TextBox T; protected Button B;
protected Label L;
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.B.Click += new System.EventHandler(MyClickHandler);
}
private void MyClickHandler(object sender, System.EventArgs
e)
{
L.Text = "Hello " + T.Text;
}
}
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
About the Author:
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.
Read this newsletter at: http://www.webproasp.com/2003/1007.html |
|