Thursday, November 29, 2007

ASP .NET | Client/Server Side Action with asp:Button

The asp:Button imitates the HTML input button's onclick event by having a property called OnClick. However, this property can only be set to a valid method name that is executed on the server. More, since the property is named the same as the HTML event, it's not possible to add client side code directly to the asp:Button tag using the onclick attribute that gets excecuted upon a click.

ASP solves this problem by adding another property called OnClientClick that can be set to execute any client side code. Then during compilation, the ASP .NET worker process consolidates both codes into the one HTML event 'onclick' because both codes must be executed upon the same event, a click. ASP .NET only compiles a page if it's modified otherwise it grabs the page from the cache by default. For the server side code, the only thing that is added is client side code that posts the page back using the button id to identify the control that caused the post back. After the page is posted back, the server side code gets executed.

In contrast, since other events don't have a matching asp:Button property they can be added directly to the asp:Button tag and ASP .NET will add them to the HTML tag generated for the button. For example, onfocus, onblur, onselect and other event attributes can be added directly as follows:


<asp:button id="btnPost" runat="server" onblur="javascript:blured()" onfocus="javascript:focused();" />


HTML attributes can also be added progromatically such as in C#:

btnPost.Attributes.Add("ondblclick", "javascript:dblClicked();");
btnPost.Attributes["onkeypress"] = "javascript:pressed();";

Sunday, November 25, 2007

State Management with ASP .NET 2.0 - 1

There is a fundemental problem with building complex stateful web application. The HTTP protocol is stateless and hence each request is a new request to the web server as far as the HTTP server is concerned. ASP .NET 2.0 solves this problem using several options. I will start with the View State and cover the rest in the upcoming posts.
  1. View State Collection

The View State collection can be used to store objects of any type to be retrieved whenever needed unique to each client. The View State is stored within the HTML code rendered to the client as a string. If you take a look at the HTML source of a page generated by ASP .NET and is using view state, you will find an input tag that looks like:

<input id="__VIEWSTATE" name="__VIEWSTATE" value="/wEPDwULLTE5ODI3MTMwMDkPZBYCAgEPZBYKZg9k" type="hidden">

Anything stored in the View State collection will be encoded into a Base64 string and included in the HTML code for the page generated in the value of the __VIEWSTATE input tag. This same tag automatically stores the visual information of web controls that have View State enabled, thus the naming "View State." When a page is posted, ASP .NET checks the __VIEWSTATE value and restores the state of the page including any user entered fields and progromattically inserted objects. By default the View State is enabled for an ASP .NET page. To disable View State include 'EnableViewState="false"' in the beginning <%@ Page tag.

<%@Page EnableViewState="false" ... >

Also by default the View State code is tamper-proofed using a hash code. ASP .NET makes a hash of the View State code using a private key and appends it to the View State value. When a page is posted back, ASP .NET recomputes the hash and checks that they match. Developers also have the option of encrypting the View State value. Encryption can be enabled per page or per site in the config file.

<%@Page ViewStateEncryptionMode="Always" ... >
Or
<system.web>
<pages viewStateEncryptionMode="Always" />
...
</system.web>


There are three encryption modes:
Always: encrypt view state information for all controls
Never: encrypt view state information for none of the controls
Auto: encrypt view state information only if a control requests it by calling Page.RegisterRequiresViewStateEncryption()


Using the view state collection is just as easy as using any collection. Objects can be inserted into the collection using a key/value pair. The key however has to be of type string. For example,

ViewState["key"] = "this is a value";

Since data is stored as object, to retrieve it in the right type it must be type casted.

string value = (string) ViewState["key"];

This concludes the view state part of state management. I hope this was useful and informative. Would love to see your comments and know what you think.

The First

This is my first post on this blog. In fact this is my first blog post period. My hope is that this blog will make a contribution to the Internet plethora of information.

Currently I am a consultant software developer working for Avanade, a joint venture between Microsoft and Accenture.

Thoulfekar