Home arrow ASP.NET arrow Page 2 - Sample Chapter: Pure ASP.Net
ASP.NET

Sample Chapter: Pure ASP.Net


In this sample chapter from "Pure ASP.Net", we'll see what web forms are, how the code is processed on the server and how client-side code can also be integrated into web forms...

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 46
January 26, 2003
TABLE OF CONTENTS:
  1. · Sample Chapter: Pure ASP.Net
  2. · Server-Side Code Blocks
  3. · Web Form Events
  4. · Conclusion

print this article
SEARCH DEVARTICLES

Sample Chapter: Pure ASP.Net - Server-Side Code Blocks
(Page 2 of 4 )

Code to be executed on the server is contained inside a set of <script> </script> tags, similar to the tags you would use to add client-side code to a Web page. In order for the page compiler to process the source code on the server, you must add a runat="server" attribute to the tag. Page logic, including the handling of events such as the Page_Load() event, is found in server-side code blocks rather than on the client side.

If the runat="server" attribute is not specified, then the tag will be passed to the client as is. This causes unpredictable results, depending on the tag in question. In the case of ASP.NET Web controls, the browser will most likely ignore the tag entirely. HTML controls will then be displayed on the client, but they will not be accessible to ViewState management or server-side validation.

Compiling ASP.NET Web Forms
As mentioned in Chapter 2, "The Common Language Runtime," all source code on an ASP.NET Web form is dynamically compiled into an intermediate language and then compiled just-in-time (JIT) into native machine code. This is handled completely behind the scenes; you do not need to compile a Web form manually. Web forms are compiled for several reasons:
  • Compiling the code in this way dramatically reduces the server's response time and processor load. Because the server spends less time processing each request, it can process more requests. Therefore, by using the Microsoft.NET framework, you can get more "bang for the buck" from your Web servers.
  • You can use strongly typed languages such as C#, Visual Basic.NET, JScript.NET, and managed C++ code for Web development on a Web form itself. This forces you to use proper coding techniques and helps eliminate the use of unstructured code that mars many standard Active Server Pages (ASP) applications.
  • The page compiler finds many errors that might be overlooked until runtime in an interpreted language such as VBScript in ASP.
The Intermediate Language (IL) that is generated in ASP.NET is human readable. You can learn a lot about the page processor by making changes to a page and seeing how the IL is affected.

Not just the server-side code is compiled into the IL; every element of the page is compiled, including the Hypertext Markup Language (HTML). HTML is placed into a text literal control and then added to the controls collection for the page.

Consider the simple Web form example in Listing 3.1. This page displays a message, using a label Web control. The text of the label control is specified on the server side. Listing 3.2 shows the IL code generated from the code in Listing 3.1. A complete discussion of the conversion of a Web form to IL is beyond the scope of this book. However, as you can see from Listing 3.1, even a very simple Web form can translate into some relatively intimidating code.

Listing 3.1 A Very Simple Web Form

<HTML>

<HEAD>
<script language="C#" runat="server" >
void Page_Load(Object Source, EventArgs E)
{
msg.Text = "Hello World!";
}
</script>
</HEAD>
<BODY>
<asp:Label id=msg runat="server"></asp:Label>
<%myerror%>
</BODY>
</HTML>


Listing 3.2 Automatically Generated Intermediate Language Code

namespace ASP {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


public class myPage_aspx : System.Web.UI.Page,
System.Web.SessionState.IRequiresSessionState {

private static System.Web.UI.AutomaticHandlerMethodInfos
__autoHandlers;


#line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
protected System.Web.UI.WebControls.Label msg;

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"

private static bool __intialized = false;

private static System.Collections.ArrayList
__fileDependencies;


#line 4 "c:\inetpub\wwwroot\Book\mypage.aspx"

void Page_Load(Object Source, EventArgs E)
{
msg.Text = "Hello World!";
}

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"

public myPage_aspx() {
if ((ASP.myPage_aspx.__intialized == false)) {
System.Collections.ArrayList dependencies = new
System.Collections.ArrayList();
dependencies.Add
("c:\\inetpub\\wwwroot\\Book\\mypage.aspx");
ASP.myPage_aspx.__fileDependencies = dependencies;
ASP.myPage_aspx.__intialized = true;
}
}

protected override
System.Web.UI.AutomaticHandlerMethodInfos AutoHandlers {
get {
return ASP.myPage_aspx.__autoHandlers;
}
set {
ASP.myPage_aspx.__autoHandlers = value;
}
}

protected System.Web.HttpApplication ApplicationInstance {
get {
return ((System.Web.HttpApplication)
(this.Context.ApplicationInstance));
}
}

public override string TemplateSourceDirectory {
get {
return "/Book";
}
}

public override void InstantiateIn(System.Web.UI.Control
control) {
this.__BuildControlTree(control);
}

private System.Web.UI.Control __BuildControlmsg() {
System.Web.UI.WebControls.Label __ctrl;

#line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
__ctrl = new System.Web.UI.WebControls.Label();

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
msg = __ctrl;

#line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
__ctrl.ID = "msg";

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
return __ctrl;
}

private void __BuildControlTree(System.Web.UI.Control
__ctrl) {

#line 1 "c:\inetpub\wwwroot\Book\mypage.aspx"
this.__BuildControlmsg();

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"

#line 1 "c:\inetpub\wwwroot\Book\mypage.aspx"
((System.Web.UI.IParserAccessor)
(__ctrl)).AddParsedSubObject(this.msg);

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
__ctrl.SetRenderMethodDelegate(new
System.Web.UI.RenderMethod(this.__Render__control1));
}

private void __Render__control1
(System.Web.UI.HtmlTextWriter __output,
System.Web.UI.Control parameterContainer) {
System.Web.UI.Control Container;
Container = parameterContainer;
__output.Write("<HTML>\r\n\r\n<HEAD>\r\n\t");
__output.Write("\r\n</HEAD>\r\n<BODY>\r\n ");

#line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
parameterContainer.Controls[0].RenderControl(__output);

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
__output.Write("\r\n ");

#line 13 "c:\inetpub\wwwroot\Book\mypage.aspx"
myerror

#line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
__output.Write("\r\n</BODY>\r\n</HTML>");
}

protected override void FrameworkInitialize() {
this.FileDependencies =
ASP.myPage_aspx.__fileDependencies;
this.EnableViewStateMac = true;
}

public override int GetTypeHashCode() {
return -1269963154;
}
}
}

blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials