Read, With the Name of Your Lord Who Created

The Differences of ASP.NET Forms and Windows Forms in a Nutshell

Posted by triaslama on June 24, 2008

I think its quite interesting if we compare two things that has equalities and differences. Now we will talk about Forms (WebForms and WinForms).

As we already know WebForms uses browser as presentation (ASP.NET Forms is no exception) and Windows Forms has its own presentation. So we may agree that WebForms has the strength through its availability and WinForms through its rich user interface.

In this post I will talk about the differences, what I get from both forms (ASP.NET Forms and Windows Forms) at a glance. I hope this post not tedious for those who already familiar with ASP.NET and Windows Forms.

Base Class
An ASP.NET Form inherits Page class (contained in System.Web.UI namespace). Look at the following example:
Presentation page (Welcome.aspx):

    <%@ Page Language=”C#” debug=”true” CodeFile=”Welcome.aspx.cs” Inherits=”Welcome.HelloWorldClass” %>

    <html>
    <head>
    <title>Welcome To ASP.NET Form</title>
    <script lang=”C#” runat=”server”>
    </script>
    </head>
    <body>
    <form runat=”server”>
    <center><asp:Label id=”label” Font-Size=”25px” style=”font-weight:bold;” runat=”server” /></center>
    </form>
    </body>
    </html>

 

This is the code behind (Welcome.aspx.cs):

using System;
using System.Web;
using System.Web.UI;

namespace Welcome
{
	public partial class HelloWorldClass : Page
	{
		private void Page_Load()
		{
			this.label.Text = "Hello, World!!!";
		}
	}
}

 

I use C# as server side language, and note that HelloWorldClass must be marked as partial class and it inherits Page class.

A Form of Windows Forms inherits Form class (contained in System.Windows.Forms namespace). Look at the following example:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Welcome
{
	public class HelloWorldClass : Form
	{
		private Label label;

		public HelloWorldClass()
		{
			this.ClientSize = new Size(400, ClientSize.Height);

			this.label = new Label();
			this.label.Location = new Point(100,100);
			this.label.Size = new Size(280,60);
			this.label.Font = new Font("Arial", 25f,
                                     FontStyle.Bold);

			this.label.Text = "Hello, World!!!";

			this.Controls.Add(label);
		}

		static void Main()
		{
			Application.Run(new HelloWorldClass());
		}
	}
}

 

Postback Mechanism
If you are new to ASP.NET (web application in general), maybe you are wondering with what is called Postback. In Wikipedia, postback defined as follow “A Postback is an action taken by an interactive webpage, when the entire page and its contents are sent back to the server for processing.” Desktop application not recognize Postback term.
Suppose that I have a form with Combo Box (Drop Down List) inside it. I want to take an action everytime selected value of Combo box / Drop Down List changed. In Windows Forms we just do the following:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinForms
{
	public class ComboMessage : Form
	{
		private ComboBox cbBox;
		string[] cities = new string[]{"Jakarta,Indonesia",
          "Yogyakarta,Indonesia","Kuala Lumpur,Malaysia","Sydney,Australia"};

		public ComboMessage()
		{
			this.cbBox = new ComboBox();
			cbBox.Location = new Point(30,50);
			cbBox.Size = new Size(110,22);
			cbBox.DataSource = cities;
			cbBox.SelectedIndexChanged += new EventHandler
                                                (cbBox_SelectedIndexChanged);

			this.Controls.Add(cbBox);
		}

		private void cbBox_SelectedIndexChanged(object o, EventArgs e)
		{
			MessageBox.Show(cbBox.SelectedValue.ToString());
		}

		static void Main()
		{
			Application.Run(new ComboMessage());
		}
	}
}

 

To do the such thing in ASP.NET Form, we need postback! Look at the following code:

    <%@ Page Language=”C#” CodeFile=”DdlWeb.aspx.cs” Inherits=”DdlCodeBehind” %>

    <html>
    <head>
    <title>Drop Down List With Postback</title>
    <script language=”C#” runat=”server”>
    </script>
    <script lang=”javascript” type=”text/javascript”>
    function changeAlert()
    {
    var ddl = document.getElementById(“ddl”);
    window.alert(“Selected Index: “+ddl.selectedIndex);
    }
    </script>
    </head>
    <body>
    <form runat=”server”>
    <asp:DropDownList id=”ddl” OnSelectedIndexChanged=”SelectedChanged” AutoPostback=”true” runat=”server”>
    </asp:DropDownList>
    <p>
    <asp:Label id=”lbl” runat=”server”>
    </asp:Label>
    </form>
    </body>
    </html>

 

Here the code behind for above code:

using System;
using System.Web;
using System.Web.UI;

public partial class DdlCodeBehind : Page
{
	private string[] cities = new string[]{"Jakarta,Indonesia",
   "Yogyakarta,Indonesia","Kuala Lumpur,Malaysia","Sydney,Australia"};

	private void Page_Load()
	{
		this.ddl.DataSource = cities;
		this.ddl.DataBind();

		this.ddl.Attributes.Add("onchange","changeAlert()");
	}

	protected void SelectedChanged(object o, EventArgs e)
	{
		this.lbl.Text = this.ddl.SelectedValue.ToString();
	}
}

 

When selected index of Dropdownlist changed two action will be taken. First show the alert message (handled via Javascript code), and second show the selected value in a label (handled via C# code). To see what is postback means in the above code, change the value of AutoPostback attribute to false:

<asp:DropDownList id="ddl" OnSelectedIndexChanged="SelectedChanged" AutoPostback="false" runat="server">

Position Overlapping
In Windows Forms we must specify the Location property for a control, if we forget it, the controls inside a WinForms will overlap each other. Its not the case with WebForms because the position follow the document (ASP.NET page) flow.

Styles
In Windows Forms we govern the looks and feels of Form through property of each control (e.g. Font, Color, BackColor, etc.). There is one static method in Windows Forms, use it when we want to apply visual styles in our Form, the name is Application.EnableVisualStyles(). This method is useful enough just call it before Application.Run() and then you will see the different is obvious!
In ASP.NET Form we can use CSS file to handle the looks and feels of our web page.

That’s some of differences between ASP.NET Forms and Windows Forms which I know. I have no doubt that still many differences between them that uncovered here.

7 Responses to “The Differences of ASP.NET Forms and Windows Forms in a Nutshell”

  1. His_wife17 said

    To complement the branding of target governments as dictatorial, opposition forces are branded as democratic. ,

  2. Stinky93 said

    VW comes up with all kinds of rather complex mechanical ideas like dual clutch and those variable continuous gears. ,

  3. leonelbeira said

    Hi, please people, for you wich interface is best to work with?

    For softwares which do you believe is the best?

  4. jones said

    ZOverB http://djIjw3MnccVop6a5hFgql.com

  5. jeremy said

    GT6CUb http://dhY3n0fjvTtj48mG9sFnCv.com

  6. ygrtyg said

    ghjthjuy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: