Why should you avoid the excessive use of ViewState in Asp.Net web page?

Automatic state management is a feature that enables server controls to re-populate their values on a round trip without requiring you to write any code. This feature is not free however, since the state of a control is passed to and from the server in a hidden form field.

Since the view state data resides in a hidden form field (__VIEWSTATE); ViewState increases the size of page and results in slow loading of page at browser. You should be aware of when ViewState is helping you and when it is not.

For example, if you are binding a control to data on every round trip , then you do not need the control to maintain it’s view state, since you will wipe out any re-populated data in any case. ViewState is enabled for all server controls by default. To disable it, set the EnableViewState property of the control to false, as in the following example:

< asp:datagrid EnableViewState=”false” datasource=”…” runat=”server”/>
You can also turn ViewState off at the page level. This is useful when you do not post back from a page at all, as in the following example:

< %@ Page EnableViewState=”false” %>
Note that this attribute is also supported by the User Control directiv

Describe the difference between inline and code behind. which is best in a loosely coupled solution?

ASP.NET supports two modes of page development: Page logic code that is written inside runat=”server”> blocks within an .aspx file and dynamically compiled the first time the page is requested on the server. Page logic code that is written within an external class that is compiled prior to deployment on a server and linked “”behind”” the .aspx file at run time.

What is ASP.NET Web Form?

ASP.NET Web forms are designed to use controls and features that are almost as powerful as the ones used with Windows forms, and so they are called as Web forms. The Web form uses a server-side object model that allows you to create functional controls, which are executed on the server and are rendered as HTML on the client. The attribute, runat=”server”, associated with a server control indicates that the Web form must be processed on the server.

An the HTML controls retain State across postbacks? If no, can you make HTML controls retain State across postbacks?

No, by default the HTML control doesn’t retain any state across postbacks. But yes, if you are converting HTML controls to Server Controls then you can retain HTML control State across postbacks.

There are 2 ways to convert HTML control to Server Control.

  • Right click on the HTML Control and then click “Run As Server Control”
  • Set runat=”server” attribute for the Control.

Can an ASPX file contain more than one form marked with runat=”server”?