Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Databinding on an asp.net webform, how does it work ?
 

In yesterdays post I stated that the compiler checks the databindings on an asp.net webform. Fabrice stated that this is not the case. As usual the truth is a little more complicated. In this post I will take a closer look at the way data-bindings on asp.net webform work.

Let's take a very simple web-form. It has a label, a textbox and a button. The label has one databinding, its text is bound to the custom binding expression TextBox1.Text. The button forces a postback, the page_load performs the databind. The result is that the content of the textbox is copied into the caption of the label on every postback. In the aspx of the page you can find this databinding

asp:Label id=Label1 style="Z-INDEX: 102; LEFT: 19px; POSITION: absolute; TOP: 17px" runat="server" Text="<%# TextBox1.Text %>"

At first sight this is a little snippet of server side script which provides the text of the label. To get an idea how the asp.net server resolves this I will turn it into something unresolvable, like MyStuff(TextBox1.Text). The project will compile fine and the new binding is in the aspx.

asp:Label id=Label1 style="Z-INDEX: 102; LEFT: 19px; POSITION: absolute; TOP: 17px" runat="server" Text="<%# MyStuff(TextBox1.Text) %>"

Running the page now pops up an enormous amount of error info

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'MyStuff' does not exist in the class or namespace 'ASP.WebForm1_aspx'

So the databinding expression leads to a compilation error, where the compiler points to the line in the aspx file describing the label. On the bottom of the page is a link "Show complete compilation source", clicking this reveals a C# source. You will find the file in the directory "temporary internet files".

The simple webform has resulted in a source of over 300 lines.

Line 1:    //------------------------------------------------------------------------------
Line 2:    // <autogenerated>
Line 3:    //     This code was generated by a tool.
Line 4:    //     Runtime Version: 1.1.4322.573
Line 5:    //
Line 6:    //     Changes to this file may cause incorrect behavior and will be lost if
Line 7:    //     the code is regenerated.
Line 8:    // </autogenerated>
Line 9:    //------------------------------------------------------------------------------
Line 10:  
Line 11:   namespace ASP {

This code was generated by the asp.net runtime from the webform1.aspx file and the associated webform1.cs file. It wraps up one class:

Line 28:       [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
Line 29:       public class WebForm1_aspx : MyXSLTapp.WebForm1, System.Web.SessionState.IRequiresSessionState {
Line 30:          
Line 31:          
Line 32:           #line 12 "http://localhost/Puinl/MyXSLTapp/WebForm1.aspx"
Line 33:           protected System.Web.UI.HtmlControls.HtmlForm Form1;

This class inherits from the WebForm1 class and adds the functionality necessary for IIS to produce the page. Here I'll focus on the part which deals with our data-binding problem.

Line 128:          public void __DataBindLabel1(object sender, System.EventArgs e) {
Line 129:              System.Web.UI.Control Container;
Line 130:              System.Web.UI.WebControls.Label target;
Line 131:             
Line 132:              #line 14 "http://localhost/Puinl/MyXSLTapp/WebForm1.aspx"
Line 133:              target = ((System.Web.UI.WebControls.Label)(sender));
Line 134:             
Line 135:              #line default
Line 136:              #line hidden
Line 137:             
Line 138:              #line 14 "http://localhost/Puinl/MyXSLTapp/WebForm1.aspx"
Line 139:              Container = ((System.Web.UI.Control)(target.BindingContainer));
Line 140:             
Line 141:              #line default
Line 142:              #line hidden
Line 143:             
Line 144:              #line 14 "http://localhost/Puinl/MyXSLTapp/WebForm1.aspx"
Line 145:              target.Text = System.Convert.ToString(MyStuff(TextBox1.Text));
Line 146:             
Line 147:              #line default
Line 148:              #line hidden
Line 149:          }

In line 145 the binding expression pops up as a part of the source. This implies that we can fix the error by including a MyStuff method. As this method is used in a class which descends from the MyWebForm1 class it can be a protected method of this base class:

public class WebForm1 : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button Button1;

    protected string MyStuff(string anything)
    {
        return string.Format("You sent me this {0} at {1}", anything, DateTime.Now.ToLongTimeString());
    }
 

And after adding this method the page will run just fine and the result of the method will show up in the label.

To summarize :

The first time a page is requested asp.net generates and compiles the source file we just met. This is the main reason why the first request of a page takes so long. For all next requests the compiled page is used. Which makes them fast. The databindings on the page are all compiled. But they are compiled in IIS and not when you compile your pages in vs.net. Which is a pity because the errors are somewhat harder to find. But all databindings are compiled on the first request, you will not bump into a scripting error which does not show up until the binding is really used. A nice thing is that you can do virtually anything in a custom databinding expression as every binding expression is pure C#. So watch your casing !

Blog on

Peter


Posted 10-14-2003 9:07 AM by pvanooijen

[Advertisement]

Comments

Oscar Romero Capistrán wrote re: Databinding on an asp.net webform, how does it work ?
on 12-15-2004 1:12 PM
Very usefull and analitic your article.
vs.net has been X-ray-ed for you.

Congratulations.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?