In moving to ASP.NET from classic ASP and ColdFusion, one of the first things that I had to adjust to was having only one form element within a web page. To be accurate, an ASP.NET web page can have multiple HTML forms, but only one form element can act as a server-side form. The additional forms are not able to participate in the viewstate management and postback features offered by the ASP.NET Web Forms framework.

<form runat="server">

    <!-- a lot of magic here -->

</form>

The aim, I believe, is to provide state and events in the world of web application development that are similar to what we are accustomed to with WinForms applications. The two key features in the ASP.NET Web Form framework are viewstate and postback events, which are raised by other server-side form elements such as buttons, written as:

<asp:Button ID="MyButton" OnClick="MyButton_Click" runat="server">

Since adopting ASP.NET MVC3 I’ve been reunited with HTML form elements and I find myself enjoying the flexibility of multiple forms per page by design and not necessarily a hack. As written in Pro ASP.NET MVC3 Framework, Third Edition, “…there are no ViewState or other hidden form elements, and the ID values you assign to [form elements] don’t get mangled”. So not to travel down memory lane on this one, but it is nice to write HTML and JavaScript (or should I say jQuery script) without wrestling with the nuances of Web Forms. With the broad adoption of data attributes in HTML5 elements, markup has become cleaner as well. For example, HTML markup such as this:

<div id='<%= MyLogicalValue1 %>_<%= MyLogicalValue2 %>'
    onclick="javascript:MyFunction(this.id);">

    <!-- display information -->

</div>

can now be better written as:

<div id="MyDiv" data-url="/mydestination.aspx"
    data-value1='<%= MyDataValue1 %>'
    data-value2='<%= MyDataValue2 %>'>

    <!-- display markup here -->

</div>

Also, JQuery provides a few convenient methods for enumerating and working with data values in HTML forms. Although this was merely a quick and dirty explanation, data attribute values can also be expressed in JSON notation or as a collection of properties and values. It is not necessary to have a data attribute for each value that you would like to represent.

In any case…it’s a good time to be a developer. Things are getting simpler.

Comments


Comments are closed