Just to show you my proof that C# is a lot easier to code and read than HTML, I found C# easier to read than HTML. And my web hosting provider uses Microsoft Windows. I don't use Linux (I don't like Linux that much).
Here's what I do when I create a simple web page.
In Visual C# .net, I create a new ASP.net template project.
Let's say that I want to create a listbox with a textbox, Add, Remove, Remove All, and a status label.
I give the following ID to each of the controls:
Textbox: txtItemName
Button: btnAdd
ListBox: lbItems
Button: btnRemove
Button: btnRemoveAll
Label: lblStatusInfo
When you first double-click the control, VC# creates a code automatically. For example, if I double-click Add, I will see this:
Code:
private void btnAdd_Clicked(object sender, System.EventArgs e)
{
}
Visual C# had also added the code for the web form designer:
Code:
this.btnAdd.Clicked += new System.EventHandler(this.btnAdd_Clicked);
The code that start with "this.btnAdd.Clicked" executes the code:
Code:
private void btnAdd_Clicked(object sender, System.EventArgs e)
{
}
...when a user clicks a button.
Since the add button is for adding the name of the text from txtItemName, I add the code inside the btnAdd_Clicked() function:
Code:
private void btnAdd_Clicked(object sender, System.EventArgs e)
{
if(txtItemName.Text != "")
{
lbItems.Items.Add(txtItemName.Text);
lblStatusInfo.Text = "You have added " + txtItemName.Text +
" to the list.";
btnRemove.Enabled = true;
btnRemoveAll.Enabled = true;
}
else
{
lblStatusInfo.Text = "No name in the text box.";
}
What this does is when you click the Add button, it copies from txtItemName into lblItems' list box. The lblStatusInfo lets a user know that s/he had added an item to the list. But if there's no text in the txtItemName text box, lblStatusInfo will mention "No name in the text box." Pretty simple.
Here's the code for the Remove button, btnRemove:
Code:
private void btnRemove_Click(object sender, System.EventArgs e)
{
string GetItem;
try
{
if(lbItems.SelectedIndex != 0)
{
GetItem = lbItems.SelectedItem.Text.ToString();
lbItems.Items.Remove(lbItems.SelectedItem.Text);
}
else
{
lblStatusInfo.Text = "You need to select an item" +
" from the list.";
}
}
catch
{
lblStatusInfo.Text = "You have removed " + GetItem +
"from the list.";
}
if(lbItems.Items.Count == 0)
{
btnRemove.Enabled = false;
btnRemoveAll.Enabled = false;
}
}
This code, when you press Remove after you select (or if you don't select) an item, handles the exception. The web page will generate an error if you don't select an item. Of course, if there's no items in the list, two buttons: Remove and Remove All, will be disabled.
The third, and last button called Remove All (btnRemoveAll), removes all the items in the list. This is a very simple code.
Code:
private void btnRemoveAll_Click(object sender, System.EventArgs e)
{
if(lbItems.Items.Count != 0)
{
lbItems.Items.Clear();
lblStatusInfo.Text = "You have removed all the items" +
" from the list.";
}
btnRemove.Enabled = false;
btnRemoveAll.Enabled = false;
}
Here's the full source code in C#:
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace www.future_gpnet.com
{
/// <summary>
/// Summary description for Sampler.
/// </summary>
public class Sampler : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.ListBox lbItems;
protected System.Web.UI.WebControls.Button btnRemove;
protected System.Web.UI.WebControls.TextBox txtItemName;
protected System.Web.UI.WebControls.Label lblStatusInfo;
protected System.Web.UI.WebControls.Button btnRemoveAll;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
this.btnRemoveAll.Click += new System.EventHandler(this.btnRemoveAll_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(txtItemName.Text != "")
{
lbItems.Items.Add(txtItemName.Text);
lblStatusInfo.Text = "You have added " + txtItemName.Text +
" to the list.";
btnRemove.Enabled = true;
btnRemoveAll.Enabled = true;
}
else
{
lblStatusInfo.Text = "No name in the text box.";
}
}
string GetItem;
private void btnRemove_Click(object sender, System.EventArgs e)
{
try
{
GetItem = lbItems.SelectedItem.Text.ToString();
lbItems.Items.Remove(lbItems.SelectedItem.Text);
lblStatusInfo.Text = "You have removed " + GetItem +
" from the list.";
}
catch
{
lblStatusInfo.Text = "You need to select an item" +
" from the list.";
}
if(lbItems.Items.Count == 0)
{
btnRemove.Enabled = false;
btnRemoveAll.Enabled = false;
}
}
private void btnRemoveAll_Click(object sender, System.EventArgs e)
{
if(lbItems.Items.Count != 0)
{
lbItems.Items.Clear();
lblStatusInfo.Text = "You have removed all the items" +
" from the list.";
}
btnRemove.Enabled = false;
btnRemoveAll.Enabled = false;
}
}
}
Here's the source code generated by Visual C# .net:
Code:
<%@ Page language="c#" Codebehind="Sampler.aspx.cs" AutoEventWireup="false" Inherits="www.future_gpnet.com.Sampler" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Sampler</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout" text="white" bgColor="black">
<form id="Sampler" method="post" runat="server">
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 15px; POSITION: absolute; TOP: 11px" runat="server" Width="331px" Height="19px">Enter an item in the text box below and click Add to add an item to the list</asp:Label>
<asp:Button id="btnRemoveAll" style="Z-INDEX: 106; LEFT: 334px; POSITION: absolute; TOP: 120px" runat="server" Width="92px" BackColor="#C00000" ForeColor="White" Text="Remove All"></asp:Button>
<asp:TextBox id="txtItemName" style="Z-INDEX: 102; LEFT: 17px; POSITION: absolute; TOP: 59px" runat="server" Width="315px" Height="23px" BackColor="Black" ForeColor="White"></asp:TextBox>
<asp:Button id="btnAdd" style="Z-INDEX: 103; LEFT: 333px; POSITION: absolute; TOP: 59px" runat="server" Width="92px" BackColor="Green" Text="Add"></asp:Button>
<asp:ListBox id="lbItems" style="Z-INDEX: 104; LEFT: 17px; POSITION: absolute; TOP: 87px" runat="server" Width="316px" Height="220px" BackColor="Black" ForeColor="White">
<asp:ListItem Value="Made by Grayson Peddie">Made by Grayson Peddie</asp:ListItem>
<asp:ListItem Value="Used with Visual C# .net">Used with Visual C# .net</asp:ListItem>
</asp:ListBox>
<asp:Button id="btnRemove" style="Z-INDEX: 105; LEFT: 334px; POSITION: absolute; TOP: 89px" runat="server" Width="92px" BackColor="#C00000" ForeColor="White" Text="Remove"></asp:Button>
<asp:Label id="lblStatusInfo" style="Z-INDEX: 107; LEFT: 18px; POSITION: absolute; TOP: 306px" runat="server" Width="317px" Height="71px"></asp:Label>
</form>
</body>
</HTML>
This is an ASP.net code, by the way.
Why don't you try out my sample just to give you a feel of how ASP.net and C# works? Here's the link:
http://www.future-gpnet.com/Sampler.aspx