Skip navigation

How Does ASP.NET Compile Pages?

Learn why ASP.NET performs better than ASP classic and how to create binding servercontrols dynamically.

Ask Microsoft

 

 

How Does ASP.NET Compile Pages?

Learn why ASP.NET performs better than ASP classic and how to create binding server controls dynamically.

 

By Rob Howard

 

Good news - the ASP.NET team is working 100 percent on the next version of ASP.NET. We're working on some exciting stuff, and I look forward to sharing some of the new features in later columns.

 

We're also excited about the beta 1 release of ASP.NET Forums. ASP.NET Forums, much like Microsoft's IBuySpy sample applications, is an end-to-end ASP.NET application that includes its full source code. You can learn more about this new ASP.NET sample application at http://www.asp.net/Forums. Be sure to check it out.

 

And now, on to the questions.

 

I read an FAQ that answered this question: Why does ASP.NET perform better than ASP classic? One reason given was that an ASP.NET file is parsed and compiled into a .NET class, which is then cached on the disk. So, how does ASP.NET ensure correct cached and original-source versions? - Praveen R.

 

The short answer: file-change notification. But I'll give you a bit more background on what's happening. When ASP.NET handles a request for a page that has never been accessed, the physical aspx file is loaded from the disk into a special ASP.NET page parser and converted into a class file using the language the developer specified. This class file is then persisted to a well-known location (a special subdirectory under Windows | Microsoft.NET) so the developer-defined aspx file is not re-parsed on each request.

 

On subsequent requests, ASP.NET simply creates an instance of the class file. Under the covers, however, ASP.NET also keeps track of the original aspx file. If the original aspx file changes, ASP.NET knows the persisted class file is no longer valid and the ASP.NET page parser must re-parse the original aspx file.

 

I'm trying to build an ASP.NET page that adds server controls dynamically. I can add the controls with properties, and even with event handlers. What I can't do, however, is bind the fields of the control to a data-source column. In the ItemTemplate, I want to create Label controls for each available data-bound item:

 

<<ItemTemplate>>

  <<asp:Label runat="server"

       Text=

   '<<%# DataBinder.Eval(Container.DataItem, "descItem") %>>'

       ID="Label1" />>

 

<</ItemTemplate>>

 

How can I set the Text property of the Label control to the data-bound value <%# DataBinder.Eval(Container.DataItem, "descItem") %>? - Oscar Peli

 

The problem with the code is the ID value of the <<asp:Label />> control. The ID property of any server control must be unique. In the previous code sample, the code would work only if one item is data-bound. But if more than one item is data-bound - which is usually the case - the code won't work. Why? When the ItemTemplate is used to create a new <<asp:Label />> for each data-bound item, it attempts to create a control with the ID Label1. For example, if there are three data-bound items, the ItemTemplate would attempt to create three <<asp:Label />> controls, each with the ID Label1. To make the ID unique, you could rewrite the code like this:

 

<<%@ Import Namespace="System.Data.SqlClient" %>>

 

<<script runat="server">>

  Public Sub Page_Load (sender As Object, e As EventArgs)

    Dim sqlConnection As New

         SqlConnection("server=.;database=Pubs;uid=sa; _

                pwd=00password;")

    Dim sqlCommand As New

         SqlCommand("SELECT * FROM Titles", sqlConnection)

 

    ' Open the connection

    sqlConnection.Open()

 

    ' Execute the reader

    Titles.DataSource = sqlCommand.ExecuteReader()

    Titles.DataBind()

 

    ' Close the connection

    sqlConnection.Close()

 

  End Sub

<</script>>

 

<<asp:DataList id="Titles" runat="server">>

  <<ItemTemplate>>

    <<asp:Label runat="server"

         Text='<<%# DataBinder.Eval(Container.DataItem,

     "Title") %>>' />>

  <</ItemTemplate>>

<</asp:DataList>>

 

You'll notice I didn't specify an ID property in the <<asp:Label />> control. When ASP.NET sees a control with no ID value, it creates a unique ID for the control automatically.

 

I have an <<asp:DropDownList />> populated when a page is loaded. The <<asp:DropDownList />> is configured to support AutoPostBack, so when the user selects a value, the page displays additional information about the selected item. There are also two buttons on the page: Process and Cancel. If the user chooses Process, certain actions are performed, and the item is removed from the <<asp:DropDownList />>. The Cancel button's only job is to redirect the user back to a previous page.

 

I have a case in which there might be no data available to populate the <<asp:DropDownList />>. In such a case, the only option is to choose Cancel. When I choose the Cancel button, however, Page_Load is fired for post-back. I have validation turned off for the Cancel button, but how do I detect what event caused the post-back to occur? - Steven Mandel

 

This is a great question - and it's a scenario I'm sure many developers have experienced. There are a couple different ways to solve this problem. First, if no data is available to populate the <<asp:DropDownList />>, redirect the page. Or, display the Cancel button - usually with some text telling the user no data is available - and allow the user to return to the previous page. Your Page_Load event probably looks something like this:

 

Public Sub Page_Load (sender As Object, e As EventArgs)

 

  ' Populate the DropDownList based on parameters passed via

  ' the QueryString

  List1.DataSource = MyData.GetData( _

   Request.QueryString("key"))

  List1.DataBind()

 

End Sub

 

It also sounds as if your Cancel button is not wired up to any event. The easiest way to solve the problem is to change Page_Load and add this event handler for the Cancel button:

 

Public Sub Page_Load (sender As Object, e As EventArgs)

 

  ' Is this a postback?

  If (Not Page.IsPostBack) Then

    ' Populate the DropDownList based on parameters passed via

    ' the QueryString

    List1.DataSource = MyData.GetData( _

     Request.QueryString("key"))

    List1.DataBind()

  End

 

End Sub

 

Public Sub Cancel_Click (sender As Object, e As EventArgs)

  Response.Redirect("/default.aspx")

  Response.End()

End Sub

 

Note the use of Response.End after the Response.Redirect call. When a redirect is performed, the server code continues to execute even though the client has been instructed to go to a new location. Response.End ends the request and prevents this further processing.

 

I'm always happy to get your questions. E-mail me at [email protected].

 

Rob Howard is a program manager on the ASP.NET team. He also writes the Microsoft Developer Network's "Nothing but ASP.NET" column and is a co-author of Professional ASP.NET (Wrox).

 

Tell us what you think! Please send any comments about this article to [email protected]. Please include the article title and author.

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish