Alternative way of developing for ASP.NET to WebForms - Any problems with this?
Posted
by
John
on Programmers
See other posts from Programmers
or by John
Published on 2012-09-19T01:26:51Z
Indexed on
2012/09/19
3:51 UTC
Read the original article
Hit count: 282
So I have been developing in ASP.NET WebForms for some time now but often get annoyed with all the overhead (like ViewState and all the JavaScript it generates), and the way WebForms takes over a lot of the HTML generation.
Sometimes I just want full control over the markup and produce efficient HTML of my own so I have been experimenting with what I like to call HtmlForms.
Essentially this is using ASP.NET WebForms but without the form runat="server" tag. Without this tag, ASP.NET does not seem to add anything to the page at all. From some basic tests it seems that it runs well and you still have the ability to use code-behind pages, and many ASP.NET controls such as repeaters.
Of course without the form runat="server" many controls won't work. A post at Enterprise Software Development lists the controls that do require the tag.
From that list you will see that all of the form elements like TextBoxes, DropDownLists, RadioButtons, etc cannot be used. Instead you use normal HTML form controls. But how do you access these HTML controls from the code behind?
Retrieving values on post back is easy, you just use Request.QueryString or Request.Form.
But passing data to the control could be a little messy. Do you use a ASP.NET Literal control in the value field or do you use <%= value %> in the markup page? I found it best to add runat="server" to my HTML controls and then you can access the control in your code-behind like this: ((HtmlInputText)txtName).Value = "blah";
Here's a example that shows what you can do with a textbox and drop down list:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NoForm.Default" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NoForm.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form action="" method="post">
<label for="txtName">Name:</label>
<input id="txtName" name="txtName" runat="server" /><br />
<label for="ddlState">State:</label>
<select id="ddlState" name="ddlState" runat="server">
<option value=""></option>
</select><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace NoForm
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Default values
string name = string.Empty;
string state = string.Empty;
if (Request.RequestType == "POST")
{
//If form submitted (post back)
name = Request.Form["txtName"];
state = Request.Form["ddlState"];
//Server side form validation would go here
//and actions to process form and redirect
}
((HtmlInputText)txtName).Value = name;
((HtmlSelect)ddlState).Items.Add(new ListItem("ACT"));
((HtmlSelect)ddlState).Items.Add(new ListItem("NSW"));
((HtmlSelect)ddlState).Items.Add(new ListItem("NT"));
((HtmlSelect)ddlState).Items.Add(new ListItem("QLD"));
((HtmlSelect)ddlState).Items.Add(new ListItem("SA"));
((HtmlSelect)ddlState).Items.Add(new ListItem("TAS"));
((HtmlSelect)ddlState).Items.Add(new ListItem("VIC"));
((HtmlSelect)ddlState).Items.Add(new ListItem("WA"));
if (((HtmlSelect)ddlState).Items.FindByValue(state) != null)
((HtmlSelect)ddlState).Value = state;
}
}
}
As you can see, you have similar functionality to ASP.NET server controls but more control over the final markup, and less overhead like ViewState and all the JavaScript ASP.NET adds.
Interestingly you can also use HttpPostedFile to handle file uploads using your own input type="file" control (and necessary form enctype="multipart/form-data").
So my question is can you see any problems with this method, and any thoughts on it's usefulness?
I have further details and tests on my blog.
© Programmers or respective owner