Things you need to know when programming in ASP.NET Web Forms

John Avis by | January 21, 2015 | ASP.NET Web Forms

Web forms makes it really easy to create a simple website, but it is also really difficult to crate a complicated website. These are some of the things you will need to know sooner or later.
Web forms makes it really easy to create a simple website, but it is also really difficult to crate a complicated website. These are some of the things you will need to know sooner or later.

Dynamic user controls

If you ever want to read the state or value of a control on post back, then a dynamic control must exist before you attempt to read the data. Unless you want to start getting data yourself from Request.Post, of course.

So on post back, usually in the Page_Init event, you should re-add any dynamic controls again.

The ASP.NET page life cycle

You need to be familiar with the ASP.NET page life cycle. You should know at least at what point any control's state is recreated from ViewState, when a control's value is populated on post back, and when control events fire, plus what Page events you need to use to do tasks before and/or after these happen.

You should also know what order events fire, between master pages, pages and user controls. You also need to know that some events play "catch up".

For more information see https://msdn.microsoft.com/en-us/library/ms178472.aspx.

Update Panels: friend or foe?

Update Panels have a bad reputation but they do have a lot going for them: they are part of the framework, and they make it possible to do complex AJAX operations with no client side code.

There are a few important points to keep in mind when working with Update Panels.

1. You should minimise the amount of markup and controls within an update panel as it will all have to be transferred to the client each post back.

If you are changing one control based on an event of another control, you don't need to put both controls in an Update Panel. You can put only the control that is affected, and make the other control a trigger for it.

2. You should minimise the amount of ViewState as it has to be transmitted to and from the server each post back.

3. If you change any control on the page on a partial post back, that control must be within an Update Panel and that Update Panel must get updated.

You should usually set all of your Update Panels UpdateMode property to "Conditional", which means that not all UpdatePanels update on a partial post back. With that setting, by default only the affected Update Panel will update but you can use the Update method of any other panel that also needs updating due to a change within. If you do use the Update method, you need to also do this on the affected Update Panel as it will no longer update automatically.

4. On partial post back all of your Page events will fire, just like any other post back.

Repeaters

Repeaters are very useful for many different purposes but when you combine Repeaters with Update Panels it can get complicated.

You generally need to register any control events that you need to be triggers for Update Panels in your code behind. You can do this in the repeater's ItemDataBound event or just iterate through the Repeater Item collection in Page_Render or wherever appropriate.

For more information see http://stackoverflow.com/a/15258052.

Linq to SQL

Linq to SQL makes it easy to handle related tables in your database, but you need to be aware of "Lazy Loading".

Lazy Loading means data is accessed only when needed.

As an example of what can go wrong, if your query returns 100 rows and then for each row you request the value from a related table, you will be performing 101 database queries. With some additional settings, you can do this in a single query instead.

For more information see https://msdn.microsoft.com/en-us/library/bb386920(v=vs.110).aspx.

ViewState

Everyone already knows that ViewState can be bad and cause performance issues with your website. By default it is turned on and it's quite easy to have kilobytes of ViewState on a page.

There are solutions to store ViewState on the server instead but the real solution is to turn off ViewState in your Master Page and then only turn it on for specific controls that need it.

Do you really need for ViewState to record your database-sourced DropDownList? You may be better caching the DropDownList values on the server and then re-adding them on each post back in the Page_Init event.


You might also want to read my Things you need to know when programming for the web post for more general web lessons.

Related Posts

Bootstrap ASP.NET Web Forms

ASP.NET Web Forms and Bootstrap 3 Modals

by John Avis | July 6, 2017

There are a few methods for hiding and showing Bootstrap 3.x modals in an ASP.NET Web Forms application. This technique is pure ASP.NET (requires no client script) and allows you to show and hide modals by changing the Visible property of a containing PlaceHolder or Panel.


ASP.NET Web Forms

Disable common UpdateProgress controls for an UpdatePanel with an associated UpdateProgress

by John Avis | April 12, 2017

If you have an ASP.NET Web Forms page with multiple UpdatePanels and multiple UpdateProgress controls, sometimes you may want one or more UpdateProgress controls associated with a specific UpdatePanel, and one or more UpdateProgress controls that have no association with an UpdatePanel so are shown when any UpdatePanel is updated.


Bootstrap ASP.NET Web Forms

Bootstrap checkbox-inline and radio-inline with ASP.NET CheckBoxList and RadioButtonList controls

by John Avis | June 20, 2016

Although you can easily create inline checkboxes and radio buttons using ASP.NET CheckBoxes and RadioButtons using Bootstrap's checkbox-inline and radio-inline classes, it's not so easy with ASP.NET CheckBoxList and RadioButtonList controls.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment
Tags
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

Subscribe

Get the latest posts delivered to your inbox.