ASP.NET for web designers: Introduction

As I promised in the one-year-celebration article I am starting an "ASP.NET for web designers" series in order to introduce ASP.NET to web designers who mostly use PHP; as well as to introduce basics of web design in ASP.NET to developers who actively use this technology. For instance, I realized that many ASP.NET developers don't know HTML equivalents for server controls. On the other hand, there is a general opinion that ASP.NET produces bad HTML code. I will cite Dave Ward: "I reject the notion that WebForms makes you write bad code (or that MVC ensures good code). Bad developers do that, not frameworks."

If you are an experienced ASP.NET web developer, some things in this article (or all of them) might be familiar to you. However, you are welcome to share your thoughts and experiences!

The series will include:

  1. Introduction (this article)
  2. Data controls
  3. Navigation
  4. Working with Themes
  5. Working with Master pages
  6. Validation 
  7. Checklist
  8. Sample application

In this article, I will cover introduction to ASP.NET including explanation of server controls, tools that you need for work and additional resources. Scope of this article, as well as all others in this series, is designing user interfaces in ASP.NET. It won't cover development in ASP.NET, but will provide additional resources for those interested in server-side development.

Introduction

A concise description of what ASP.NET is can be found on Wikipedia:

"ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime (CLR), allowing programmers to write ASP.NET code using any supported .NET language."

We can say that you actually don't do any coding in ASP.NET, but rather in HTML and some of the .NET languages. In examples in this series I will use C#.

To be able to work with ASP.NET you can use any text editor, but I would recommend you to use any of Visual Studio versions to get enhanced development environment. Visual Web Developer Express is lite version of Visual Studio that can be downloaded for free (including .NET Framework 3.5). It contains all that you need to design in ASP.NET. You don't necessarily have to have IIS installed because Visual Studio (and Web Developer Express) has built-in ASP.NET development server.

To create a new web project in Visual Studio, you choose File -> New -> Website and choose ASP.NET Website in dialog window. Visual Studio will create a solution with one web site. Solution is a placeholder for your projects; it enables you to organize related projects. As seen on the image below Solution Explorer is panel where you can see all projects and files under your solution. Panel on the left side is Toolbox which contains all the server control, but we'll talk about that later in this article.

When you create a new Web site, Visual Studio creates several files for you: Default.aspx, Web.config and App_Data folder.

All ASP.NET pages consist of two files - presentation file where HTML is stored (.aspx file) and code-behind file where server-side code is stored (.cs file). This enables clear separation of server-side code and presentation. Each .aspx page is being created with a template code. It contains page definition and some basic HTML structure including <FORM> element. Please note that you can have only one form element per page.

An interesting part for web designers who haven't worked with ASP.NET yet is a page element at the very top of each aspx page. It contains a definition of aspx page that may include server side language that will be used, name of code-behind file and so on.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

Web.config file is a configuration file for your web site based on XML structure. This simple XML file contains parameters and determines the behavior of some parts of your web site. You can define, for example, authentication mode and modules or you can define custom application settings. App_Data folder is where your databases might be stored.

To be able to work with ASP.NET you will need some basic understanding of server controls.

Server controls

Besides HTML elements, ASP.NET uses server controls which have similar definition to HTML elements. The important difference between the two is that, unlike HTML elements, server controls are accessible from the code-behind or: the server side. This is determined with runat="server" attribute in their definitions. Server controls have different attributes than HTML elements and they are called properties. For example, Image control (which is equivalent to IMG element) has ImageUrl property instead of SRC attribute. But Visual Studio has very useful feature called intellisense which is some kind of autocomplete for your code and that will help you explore various properties.

Imoprtan note: Always style elements using CSS insted of server controls properties.

Server controls are much simpler than you might think. Each server control is rendered to a known HTML element on the client. Here is a list of server controls with their HTML equivalents and selectors that can be used from CSS or jQuery.

Server control HTML equivalent CSS/jQuery selector
Label <span> span
TextBox <input type="text"> input[type="text"]
TextBox (TextMode="Password") <input type="password">
input[type="password"]
TextBox (TextMode="Multiline") <textarea>
textarea
Button <input type="submit"> input[type="submit"]
LinkButton <a href="postback options"> a
ImageButton <input type="image"> input[type="image"]
HyperLink <a> a
DropDownList <select> select
ListBox <select size="n"> select
CheckBox <input type="checkbox"> with <label> input[type="checkbox"]
CheckBoxList <table> with a list of <input type="checkbox"> table or table input[type="checkbox"] for items
RadioButton <input type="radio"> with <label> input[type="radio"]
RadioButtonList <table> with a list of <input type="radio"> table or table input[type="radio"] for items
Image <img> img
ImageMap <img> img
Table <table> table
BulletedList <UL> or <OL> based on BulletedStyle property ul or ol
HiddenField <input type="hidden"> input[type="hidden"]
Literal Literal doesn't have its HTML equivalent, it is usually used as a placehoder to render HTML generated on the server  
Calendar <table> <table>
FileUpload <input type="file"> input[type="file"]

All server controls are placed in Toolbox positioned on the left side if Visual Studio window. The controls listed in the table above are placed in "standard" panel in the Toolbox. To see how ASP.NET renders controls let's have a look at one example.

Sample ASP.NET signup form

This is an example that simulates very basic signup form. Validation, for example, won't be included since it will come later in this series. Let's first see the difference between the HTML strucutre in design mode and rendered code. We can say that design mode represent a code that is accessible on the server and that will be rendered on the client (if anyone knows better definition, please share).

<form id="form1" runat="server">
    <h1>Sample ASP.NET signup form</h1>
    <asp:Literal ID="litMessage" runat="server"></asp:Literal>
    <fieldset title="Please enter all data" id="signupForm">
        <label for="txtFirstName">
            <asp:Literal ID="litFirstName" runat="server" Text="Firstname">
            </asp:Literal>
        </label>
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        <label for="txtLastName">
            <asp:Literal ID="litLastName" runat="server" Text="Lastname">
            </asp:Literal>
        </label>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        <label for="txtEmail">
            <asp:Literal ID="litEmail" runat="server" Text="Email address">
            </asp:Literal>
        </label>
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <label for="txtPassword">
            <asp:Literal ID="litPassword" runat="server" Text="Password">
            </asp:Literal>
        </label>
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
        <asp:Button ID="btnSignup" runat="server" Text="Sign up!"
            onclick="btnSignup_Click" />
    </fieldset>
</form>

And the CSS code looks like this:

body { font-family:Arial, Sans-Serif; font-size:0.75em; line-height:130%;}
fieldset {border:none;}
label {display:block;}
input[type="text"], input[type="password"] { display:block; margin: 0 0 10px 0;}
.message { color:#f00; font-size:1.5em;}

We have a fieldset with four fields: Firstname, Lastname, Email and Password. Each field has an input element (TextBox) and corresponding Label with Literal control inside. If you take a look at the table above you will notice that Literal is just a placeholder, so each  Literal will render its content of Text property. Also, each TextBox has its own HTML equivalent. It is not that hard to conclude that HTML structure would be very clear without clutter.

<h1>Sample ASP.NET signup form</h1>
<fieldset title="Please enter all data" id="signupForm">
     <label for="txtFirstName">Firstname</label>
     <input name="txtFirstName" type="text" id="txtFirstName" />
     <label for="txtLastName">Lastname</label>
     <input name="txtLastName" type="text" id="txtLastName" />
     <label for="txtEmail">Email address</label>
     <input name="txtEmail" type="text" id="txtEmail" />
     <label for="txtPassword">Password</label>
     <input name="txtPassword" type="password" id="txtPassword" />
     <input type="submit" name="btnSignup" value="Sign up!" id="btnSignup" />
</fieldset>

The code above can be seen by looking at the source in your browser once you run your small website (just to make it clear, one of the ways to do that is by pressing F5 key).

Now let's see some (fake) action. When I click on Signup button, I want to generate a message on the server and show it on the client. Take a look at the "design" mode code. btnSignup has btnSignup_Click method assigned to onclick event. This is the method that is defined on the server and it looks like this:

protected void btnSignup_Click(object sender, EventArgs e)
{
    // This is where some reall proccess would occur
    litMessage.Text = "<p class=\"message\">You successfuly faked signup proccess :)</p>";
}

When user clicks on button postback occurs. New HTML code is generated and sent back to the client. The code fills Literal control with HTML code (paragraph that has "message" class and some text inside it). If you take a look at CSS code, you'll notice there is a class "message". This is how the message will look like.

When returned to client, the code will be almost the same as before postback. It will contain just one more paragraph:

<h1>Sample ASP.NET signup form</h1>
<p class="message">You successfuly faked signup proccess :)</p>
<fieldset title="Please enter all data" id="signupForm">
     <label for="txtFirstName">Firstname</label>
     <input name="txtFirstName" type="text" id="txtFirstName" />
     <label for="txtLastName">Lastname</label>
     <input name="txtLastName" type="text" id="txtLastName" />
     <label for="txtEmail">Email address</label>
     <input name="txtEmail" type="text" id="txtEmail" />
     <label for="txtPassword">Password</label>
     <input name="txtPassword" type="password" id="txtPassword" />
     <input type="submit" name="btnSignup" value="Sign up!" id="btnSignup" />
</fieldset>

Download the example

This example shows that designing ASP.NET websites and web applications doesn't have to be a problem as many people see it. If things set right from the beginning it can be easy as designing for any other framework/technology.

Conclusion

For better understanding what is happening "under the hood", I recommend you to read more about ASP.NET page lifecycle, state management and viewstate. In next part of the series we'll take a look at the tricky ASP.NET data controls.

Additional resources

To get more information about ASP.NET you can visit:

More articles in Blog archive or elsewhere
Advertisement

36 Comment(s)

Jin

Jin 27 Apr 2009 #

I'm glad you started this series Janko. I think designers need to learn about asp.net besides php, rails and other programming languages. (All are great, depends on the nature of the site IMO).

Working at enterprise level, chances of running into asp.net and j2ee is pretty high. I think your articles will be very beneficial to web designers.

GJ

Simone

Simone 27 Apr 2009 #

"I reject the notion that WebForms makes you write bad code (or that MVC ensures good code). Bad developers do that, not frameworks."

But some framework make writing bad code easier... and, specifically, WebForms tries to make you use web server controls, and they DO emit bad HTML code
In this sample you only showed simple form controls, let's see what comes out when you write grids or menus

Janko

Janko 27 Apr 2009 #

Jin: Thanks, it took me a long time to start with this series. It is indeed important to talk about ASP.NET and webdesgn. On enterprise level, as you said, one would probably run into ASP.NET or Java. We'll see if this will help.

Simone: You are right, but you don't need to use controls/features that render bad code. If, for instance, menu control renders crap HTML you just don't use it. There are many ways to create good menus - you can stick to simple and effective UL generated on the server or by using Repeater.

Neil

Neil 27 Apr 2009 #

I never add an onClick event to my front end .aspx page just:

Protected Sub btnSignup_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSignup.Click

        Dim boolError As Boolean = False

        boolError = ValidateForm()

        If boolError = False Then
            pnlError.Visible = False
         'Sub to fire if successful
        Else
            lblErrorTitle.Text = "Please check the required fields"
            lblError.Visible = True
            pnlError.Visible = True
        End If
    End Sub

Neil

Neil 27 Apr 2009 #

By the way, looking forward to the series, are you going to touch on OOP, data access and  business logic layers?

Janko

Janko 27 Apr 2009 #

Neil: VB.NET uses Handles for events, in C# you can add event in markup. I won't touch BL and DAL, I will focus this series more on rendering and styling the content in ASP.NET. The farthest I will go is user interface process components, or - code behind Smile

Simone

Simone 27 Apr 2009 #

If you are not going to use  WebForms' server controls, what's the point of using WebForms at all?

Janko

Janko 27 Apr 2009 #

Simone: If I choose Repeater over GridView, it's still WebForms. I want postback, viewstate, etc, but I also want as clean HTML as I can get.

People just use WebForms, especially if they work for "enterprise" companies. And it's not easy for them to move to MVC or other frameworks. Too many projects are currently based on WebForms and many people have problems with creating clean user interfaces. The idea behind this series is to make life of designers and web developers easier with their current or future projects.

Simone

Simone 27 Apr 2009 #

If you use Repeater over GridViews or make your own menu rendering code, you gain better HTML, but make the life a lot more difficult to developers that have to deal with custom editing or have to write their own menu rendering code.
You might want to take a look at the CSS Adapters

Karl

Karl 27 Apr 2009 #

Ack - Cans of worms are going to be opened when you get to the viewstate! Good to read this article though - hopefully you can spread the ASP.NET word further. On a separate note - if I recall correctly, ASP.NET used to rewrite some of the markup; divs magically changing into tables, and stuff like that. Do you know if this is still the case?

Looking forward to read more Smile

Janko

Janko 27 Apr 2009 #

Simone: Css adapters might be one of the solutions.

Karl: I'll just mention what the hieroglyphics at the top of each page are Smile I don't remember about rewriting markup, haven't had such issues lately

Shane

Shane 27 Apr 2009 #

The fact is that ASP.NET does emit crap HTML for some controls.  Not using it is one way around it, but that's hardly a viable or acceptable solution.

The CSS control adapters (http://www.asp.net/CssAdapters/) aren't fantastic - they suffer a little from divitis, but they do provide a decent enough starting point to customise the markup for anyone who has a bit of ASP.NET experience.

You can get ASP.NET to spurt out valid XHTML (though avoid the tree view control), but it can be a little bit of work.

lawrence77

lawrence77 27 Apr 2009 #

I want to do this, before that u do this ;)
any way great work! Smile

Mike Kivikoski

Mike Kivikoski 27 Apr 2009 #

Thanks for the article, some really good insight.  I'm a designer & front end developer working in asp.NET and was wondering: Why does almost all the asp.NET code get wrapped in spans and how do you stop that?

Thanks
Mike

Janko

Janko 27 Apr 2009 #

Shane: It's important to get  ASP.NET code cleaner, be it XHTML valid or not (I'm not that strict about XHTML)

Not using it is one way around it, but that's hardly a viable or acceptable solution.

I can't agree that avoiding some server controls isn't acceptable solution. For example, I love the way BlogEngine.NET generates code, and it's far from bad.

lawrence77: Different points of view on the same subject are always valuable ;)

Mike: ASP.NET developer just looove to use Label controls. You can use Literals instead Smile

Marco

Marco 28 Apr 2009 #

Hiya Janko!

Wow - that was one pretty complete and good tutorial giving people an introduction to ASP.NET .

Although I'm not a .NET developer, I'm sure many other people will find this post very useful Smile .

Keep up the great work!

Calil

Calil 28 Apr 2009 #

Hey Janko, what's up? First of all, congratulations. I've been around the design sphere (I'm only a developer) for a couple of years, and it's amazing how people think that ASP.NET is something awfull like VB6 and just discard it.
Well, we know that hosting is a little more expensive but, after all, it's a great framework for both web and app development. I even develop enterprise solutions, like a service that listens for logon events and connects to an eDir server, with the ASP.NET framework.
One thing that must be said is that the drag n' drop feature may tend the developers (bad developers, though) to create bad HTML pages.
Everything I've learned, since basic and advanced CSS, JS frameworks (as jQuery), even to PNG fixes, I've applied on my .NET projects, with great results.
Webforms give the developers a complete scenario to develop solutions with n-layers, making a complete separation of business rules, view, persistence and even design. It's, IMHO, the best way to work with SOC.
Keep on with the series. I found your blog through CSS Globe, but I just got your RSS.
Regards

Janko

Janko 28 Apr 2009 #

Marco: Thanks! Who knows, maybe you'll design something in ASP.NET in the future Smile

Calil: Exactly, you really can create good user interfaces in ASP.NET. My opinion is that it doesn't matter if you use one server control over the other. Take a look at BlogEngine.NET - the engine is simply amazing.

Stacey

Stacey 28 Apr 2009 #

Thanks for doing this series, I was looking for something like this a couple of months ago when I first started learning .NET.

Davide Espertini

Davide Espertini 28 Apr 2009 #

Hi Janko!

Great tutorial! Simple and clear, but really simple to understand!
I hope this helps some developers to get into ASP.Net like me, you, simone and other!!

FP

FP 28 Apr 2009 #

Just a note on the Label control. It will render as a <label> if you set the AssociatedControl property.
So e.g.

<asp:Label runat="server" AssociatedControlID="txtFirstName">First name:</asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

will render as:

<label for="txtFirstName">Firstname</label>
<input name="txtFirstName" type="text" id="txtFirstName" />


Just a different way of doing it!

Janko

Janko 28 Apr 2009 #

Stacey, Davide, Thanks Smile

FP: Thanks for the tip! I got used to have Literals.

Jape

Jape 28 Apr 2009 #

Also, the use of <label for="txtFirstName"></label> will probably not work in pages where you have a nested structure of server controls, because off the way ASP.NET renders the names of controls.
A nested textbox will render to something like <input name="...$txtFirstName" type="text" id="...$txtFirstName" />, so the browser won't be able to find an input element with the name "txtFirstName".

Janko

Janko 28 Apr 2009 #

Jape: In the example in this article it will work. I will get to ASP.NET naming issue in next articles in series

Jape

Jape 28 Apr 2009 #

Alright, I'll be sure to check it out. Neat to see some ASP.NET centered content on a design blog, keep it up!

Andy

Andy 28 Apr 2009 #

Nice Article Janko. I particlularly like the fact that you used asp:literals not asp:labels - one of my bugbears Laughing
I wish the beginner books just used literals not labels in the first place...

Speed Dating

Speed Dating 28 Apr 2009 #

I am now learning ASP.net, mostly through the tutorials, at www.asp.net, and am interested in learning server side programming. But the design aspect is very important because it prepares you with the knowledge you will need to be able to accomplish many design aspects of a website.
Thanks.

boba

boba 29 Apr 2009 #

Nice overview ... but i like my java server faces mutch better. Wink

Neil

Neil 29 Apr 2009 #

@Andy, why do you prefer literals over labels?

balanza

balanza 07 May 2009 #

WebForms helped Windows application developer to get in touch with www industry. I think they worked very well for their goal, and i apreciate this, but nothing more. If you wanna do something a bit different from "place a control and manage its events in code", you just can't do it (or better, it's gonna be very difficult). I always think easy html/css/javascript with some server code is the best to make good web interfaces.

yorkie

yorkie 10 May 2009 #

Having just read your tutorial on ASP I was going to venture into ASP then I started to read the comments. If you are not bothered with HTML standards as prescribe by W3C then why use ASP at all after all we are here to make sure web platforms are available to all our community. So I think I shall stick with my XHTML, HTML and CSS at least I know it will be clean code and conform to standards.

I would not be so arogant as to write "It's important to get ASP.NET code cleaner, be it XHTML valid or not (I'm not that strict about XHTML) " that Janko wrote on April 27.

So keep your ASP I do prefer to have a good cleam site rather than a crap ASP site with crap XHTML/HTML

Janko

Janko 11 May 2009 #

yorkie: The point of this series is not to prove that ASP.NET is outstanding-super-duper framework that enables you to create standards compliant interfaces. The goal is to help designers who design in ASP.NET.

We obviously have different backgrounds. I have been working in large teams that develop business applications for more than 15 years, and I am fully aware of the fact that designers who designed those applications, websites or whatever had problems in understanding the way ASP.NET works. This is to help them with their current and future projects.

There is web design outside freelancing. I know many web designers that work for companies and have various challenges. One of them is to design heavy asp.net business applications, and that is a tough job, believe me. So yes, I believe that insisting on xhtml and css in this cases are almost impossible.

If you can explain me please what is so arrogant about that? Isn't rather attitude: "So keep your ASP I do prefer to have a good cleam site rather than a crap ASP site with crap XHTML/HTML" arrogant? Would you refuse to design an application written in asp.net?

Mike Meisner

Mike Meisner 28 May 2009 #

HI Janko

Thanks for this tut, and series of ASP.NET articles.

As a front-end designer, I have a solid understanding of PHP and jQuery, but never ventured into other coding arenas. Not long ago I began a new job at a company that uses ASP .NET to develop sites. This will be a great resource, for sure.

And fwiw, I don't understand the commotion about the code output being sloppy, and people slammin .NET for its lack of standards adherence.

Yorkie makes a shallow point that's hardly worth addressing since he/she doesn't seem to understand that

Our engineers develop some seriouly robust applications, and the last thing our they are thinking about is valid (X)HTML and CSS. For large businesses, there is a give and take. I actually think it's stupid to be so strict about web standards, one way or another; it all depends on your goals, working environment, and what you've been dealt....

Website Design

Website Design 04 Jun 2009 #

A nice introduction to ASP.NET for web designers. This will be quite helpful for starters. Thanks for taking the time to share.

thomase

thomase 06 Jun 2009 #

thanks for this article janko.

im a total asp.net dummy, and youre really doing me a favour breaking it down like this.
i'll be looking forward to your next articles in this series

webpixstixs

webpixstixs 08 Jun 2009 #

programınız ve anlatımlarınız kodlarınız çok işime yaradı
Thanks you jankoatWarpSpeed

Comments are closed
via Ad Packs
9292