Not All Code Render Blocks Are Created Equal – Part 1

There are 2 types main types of render blocks.  On one hand there are <%= %> style render blocks that are the more general type and allow the coder to print out directly to the html.  And on the other hand there is the <%# %> style render blocks that are used inside of controls that are data bound but still serve the purpose of printing out directly to the html.

One thing that was confusing to me, someone who had experience with php before, was that they don't actually run in the same phases of execution.  Databinding blocks are run when the control has its DataBind method called on it.  The more general type of render block is run in order from the top of the page to the bottom of the page once the controls inside the page have finished rendering to html.

Here is a simple aspx page that can help illustrate this:

    <form id="form1" runat="server">
        Time One: <%= PrintTicks() %>
    <asp:Repeater ID="repeaterRenderBlock" runat="server" >
                Time Two: <%# PrintTicks() %>
        Time Three: <%= PrintTicks() %>

And here is code behind for it:


        protected void Page_Load(object sender, EventArgs e)
            List<string> list = new List<string>();
            repeaterRenderBlock.DataSource = list;

        protected string PrintTicks()
            return Environment.TickCount.ToString();

I added the sleep to help show the time differences.  This page is so simple it will render everything within 0 ticks, so I need to show some separation. The results are shown bellow with the digits with the same values 'X'ed out:

Time One XXXXX831
Time Two XXXXX722
Time Three XXXXX924

We can see that the 'Time Two' was run before the other two blocks, even though it was in the middle. 


Next time, using this knowledge we will look into how the render blocks have access to different objects and ways to work around differences.

Skip to main content