Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
New Things in .NET 3.5 SP1 – Moving Hidden Fields to the Bottom of the Page

If you have not downloaded SP1 for Visual Studio 2008 and .NET 3.5, please go ahead and get to it. If you installed the Beta, like me and many other folks you will want to run the prep tool first. I am not going to do one of those here is a list of new features in SP1, there are plenty of those all over the web by now. I mean SP1 has been out all week long. Instead I want to create some post showing some of the important features in more detail.

Over the years I have worked on moving ViewState and other Hidden Input Fields to the bottom of the page. This is important for several reasons, but can also have impact on some content, like the DevExpress HTML Editor. However, moving non rendered content helps the page render faster and also helps place important content higher in the page, thus allowing the search engines know the content is more important. So often it is wise to move Hidden Fields to the bottom of the page.

ASP.NET SP 1 has added a new property to the Page configuration section, RenderAllHiddenFieldsAtTopOfForm. This is a great new property that gives you control over where the system generated hidden fields are rendered by the framework or page handler creates. That is important to note, because the property will not affect any hidden fields you add to the markup, those will still need to be handled by you. Personally I do not think I ever use hidden fields, so it should not be an issue for me and most likely not you. While this will not move the ViewState hidden field it will render other generated hidden fields at the bottom.

<pages theme="DarkBeer" masterPageFile="~/TBHMain.master" 
    maintainScrollPositionOnPostBack="true" 
    renderAllHiddenFieldsAtTopOfForm="false">

When this is an issue is when a a postback is performed before the page is completely rendered. This typically happens when an AJAX postback is performed as the page is being rendered. From my experience you have to test each of your pages to learn which ones will be affected. You will receive an “Invalid Postback” or “callback argument” error. Other things I have found is controls will not render correctly in some cases. I have found most of my problems are with third party controls, in particular WYSIWYG editors like FreeTextBox and the DevExpress HTML Editor.

<div>
    <input type="hidden" name="__SCROLLPOSITIONX" id="__SCROLLPOSITIONX" value="0" />
    <input type="hidden" name="__SCROLLPOSITIONY" id="__SCROLLPOSITIONY" value="0" />
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" 
        value="/wEWBwLjspyADQKP0anjBQK41pvTDwK0yq71CQLBo8n8...PKC" />
</div>

One thing to note is since this is done in the web.config Page element, this feature is applied to all pages and cannot be controlled at the page level. So if you have any issues with this and have to turn it off, beware this will affect all the pages in the web site.

Posted: Thursday, October 09, 2008 6:35 AM

by Chris Love
Filed under: ,

Comments

Justin said:

This is some good info but one thing that surprised me was this: "Personally I do not think I ever use hidden fields, so it should not be an issue for me and most likely not you." What makes hidden fields so bad and how can you never use them? For instance, if you bound data to a repeater control, wouldn't it be acceptable to use a hidden field to store the row's key value?
# October 9, 2008 9:01 AM

Chris Love said:

Justin, no you should not need to store the primary key in a hidden field. THink about it, if you have say an image button to delete an item, you would set the CommandArgument of the imagebutton to the primarykey.

The only hidden fields I 'use' are the ones that the system, controls and page inject. Why you want to move them to the bottom of the page is they take up time to render the page because the browser has to process them before it processes an item that is actually displayed, thus there is a performance issue there, even if it is percieved.

# October 9, 2008 12:51 PM

Justin said:

Chris, I agree you are correct; my scenario would be best handled by assigning the CommandArguement the primary key.  But I’ve been thinking long and hard for a use of a hidden field and I just ran across one.

I have a repeater that lists records that were saved in a database.  There is also an imagebutton on each row used to delete the record with a CommandArguement as the primary key (record ID).   However, before deleting I need to check an attribute of the record first and perform additional logic based on its value.  Instead of instantiating an instance of my record to get this value, it would be more efficient to store that attribute in a hidden field in the repeater.  Would you consider this an okay use of a hidden field or do you know of a better method?  Thanks!

# October 28, 2008 5:06 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS