Library tutorials & articles
Valid XHTML within .NET
The Problem
In their original out-of-the-box format Microsoft's ASP.NET HtmlControls and WebControls do not produce XHTML compliant code. Microsoft has thoughtfully (and thankfully) allowed developers to extend the controls to add user-defined custom functionality. It will be through this added functionality that we will be able to output valid code.
To start with, let us look at the code that the current default implementation produces. Create a new ASP.NET page in the root folder of your IIS development machine; call the page currentform.aspx and copy the code in Listing 01 (you'll notice were using the "XHTML 1.0 Strict" DTD in this example):
Listing 01
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<head>
<meta http-equiv="Content-Type"
content="application/xhtml+xml; charset=utf-8"/>
<title>Default ASP.NET Form</title>
</head>
<body>
<h1>Default ASP.NET Form</h1>
<form runat="server">
<fieldset>
<legend>Sample form</legend>
<input type="submit"/>
</fieldset>
</form>
</body>
</html>
Point your browser at currentform.aspx , you should see something similar to Figure 1a .
Figure 1a
If you view the source code you should see the following HTML (formatted here for brevity):
...
<form name="_ctl0" method="post" action="currentform.aspx"
id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="**random characters**" />
<fieldset>
<legend>Sample form</legend>
<input type="submit"/>
</fieldset>
</form>
...
As you can see ASP.NET has added the name , method , action and id attributes as well as a hidden field called __VIEWSTATE containing the forms encrypted triplet view-state values. If we try and validate this document by uploading a copy of the source-code to the W3C's validating service you'll see we get the following response:
This page is not Valid XHTML 1.0 Strict!
The validating service goes on to tell us we have two errors:
- There is no attribute called name within a form tag.
- The document type does not allow element input here because input is an inline tag when a block-level tag was expected.
The remainder of this document will focus on fixing these two errors.
Related articles
Related discussion
-
Gizmox Announces release of Visual WebGui SDK version 6.2.2
by Visual WebGui (0 replies)
-
Error 4 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
by lbargers (3 replies)
-
How to receive data in web server sending from GPRS modem
by AshokSingh (1 replies)
-
Working with frames in c# .net
by pulak2008 (0 replies)
-
Time Out Problem
by lorrainepetty (1 replies)
Related podcasts
-
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum) -
.net developer
in Rijswijk (€2K-€4K per annum)
Events coming up
-
Dec
3
An afternoon of SQL Server Data Services and ASP.NET Dynamic Data
Bradford, United Kingdom
This event is in association with Black Marble. In the morning Black Marble will be presenting on Microsoft "Oslo": The Future of Enterprise Applications. To find out more about this please follow the link on the right.
Have you tried putting any asp.net controls on your page, (such as an asp:button control)? I tried and got an error telling me that the "Control 'x' of type 'y' must be placed inside a form tag with runat=server". Now, I know that the control is between the form tags and runat is set to server, so what is going on?
This thread is for discussions of Valid XHTML within .NET.