RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


ASP.NET Development Through Web Controls and Declarative Programming

ASP.NET WebControls do more than just allow you to write reusable components. They can provide an entire approach to Web application development, allowing you to bring a new level of OOP to the UI and letting you program declaratively.

ately I've come to notice that no other programming term has more definitions than declarative programming. In this article, I will attempt to explain declarative programming in terms of how it applies to .NET development, specifically ASP.NET through the use of WebControls. I'll do this by illustrating some real-world examples that I have used in my own projects. In the end, I hope to leave you with an understanding of what declarative programming is, how you can use it when developing ASP.NET Web applications, and how, with the help of WebControls, to use it as an approach to ASP.NET development.

Declarative programming by way of custom controls has always been a part of Windows (WinForms) development. But it wasn't until the release of ASP.NET that developers got a chance to apply these principles and techniques to Web development.

OK, time to pick a fight. What is declarative programming? Well, as I stated above, I've seen many definitions of that term lately; but most of them pretty much break down into similar descriptions. It is a style of programming whereas at one level you define, in detail, how a variety of things are done, and from another level you instruct as to what needs to be done. Let me explain with a couple of examples in the context of languages I'm sure you already know.

Back to the Basics
Think about how you create a simple table in HTML: the first thing you do is define your table tags using <TABLE> and </TABLE>. A table consists of rows and columns, so of course you use <TR> and </TR> to declare each row, and then one or more <TD> and </TD> tags to define each column in each row. This is a table at its simplest form:

         <TD>This is cell 1 in row 1</TD>
      <TD>This is cell 2 in row 1</TD>
         <TD>This is cell 1 in row 2</TD>
      <TD>This is cell 2 in row 2</TD>

I know, you know this already, but indulge me because I promise I will build on this idea. This simple code tells the rendering engine in the browser to draw a table on the page. Obviously there is some machine code at the browser level to actually draw the physical table on the page and display it properly. Now, what do you do to alter the way this table looks? Say, for example, you want thicker borders, or some extra padding and everything in the first column to be right justified. This is where the border, cellpadding, and align attributes come in. By simply adding them to your HTML tags, you've totally altered the way the table looks. You didn't need real code because you just alter attributes, or properties if you will. The rendering engine that interprets the HTML knows how to alter the appearance of the table based on what you asked for.

Author's Note: This article assumes knowledge of ASP.NET and the use of the WebControls Microsoft provides. Some knowledge of custom WebControls, at least in concept, is helpful. But most importantly you should have an open mind for looking at what is most likely a different way of programming sites in ASP.NET.

OK, let's turn the clock back all the way to the release of ASP.NET in February of 2002 (not counting betas). Microsoft introduced the Web Control, which let developers do things in what appears to be HTML-like syntax. Starting with a simple Web Control, here is how you declare a textbox control:

   <asp:textbox id="txtMyTextbox"></asp:textbox>

I'm leaving out the properties here for brevity, but I think you know where I'm going with this. Since browsers can still only understand HTML, the ASP.NET parser takes this textbox declaration and turns it into the appropriate HTML; based on various properties you can change, the browser renders the HTML slightly different. The ever-popular DataGrid control, though more complex, works the same way. It gives developers the appearance of an actual grid with all sorts of customizable features; much akin to its WinForm cousins. But as I said, the browser can still only understand HTML, so ASP.NET once again converts a simple looking declaration, <asp:datagrid> </asp:datagrid>, into what turns out to be a regular HTML table. All the properties you can set on the DataGrid, and all the programming you can provide to customize it and to fill it with data, will still only result in an HTML table, albeit a pretty complex one in the end.

This is declarative programming in ASP.NET at its simplest. I say simplest, because you, as the ASP.NET developer, have not really had to do much to get the grid (or the textbox) to display, other than drag it on a WebForm, or code the <asp:...> tags. All the code that actually draws the resulting HTML has been done for you by the folks at Microsoft—it has been programmed declaratively. In the world of Web Controls using ASP.NET, declarative programming moves some of the code off of the WebForm and into the Web Controls. Let's go back to the definition I gave before and apply it here: the Web Controls have determined how all sorts of things are to be done (rendered in this case), and the WebForm has determined what needs to be done.

If all of this sounds familiar to you, it should. This is the very basis of object-oriented programming. You design a class and expose properties, methods, and events, and it is a form of declarative programming. When you gave your objects all that cool functionality and things it can do by putting a lot of programming into them, you've relieved just that much work from any developer (yourself included) that uses your classes. Now you're seeing a classic design paradigm at work in the UI level. Heck, this has been familiar in the UI since Visual Basic 1.0 when you first dropped that textbox (remember the VBX?) on to your first form.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date