Introduction to ASP.NET - Article-Library.Network

Article-Library .Net

More than 100.000 Excellent Articles - Free Content for your Site.


Earn Money. ($73.568) | CNCHobby | Leona Sims | Hosting from 1 euro | Invite Friends. Get Paid.



Search:

Home | Computer


Introduction to ASP.NET

By: Daniel Benor


Add this Article to your website/blog

ASP.NET is Microsoft's successor to ASP. It is a set of technologies used to build
web applications in general, including Web Sites and XML Web Services. In this article
we will deal with simple web site developing using C# (CSharp), so that you can get
started with the ASP.NET technology and the .Net 2.0 Framework. C# is a .Net framework
capable programming language by Microsoft, which we will use to build our web sites.

In order to build the example presented in this article you need to have :
Windows 2000/Xp/2003/Vista
Visual Studio 2005

Start Visual Studio 2005 and go to File, New, Web Site. From the templates list and
pick ASP.NET Web Site (selected by default). Make sure that you have C# selected in
the language combo box. Pick a location where you want to save your project and click
OK.
So far you should see a file called Default.aspx with some HTML code in it. In the
Solution Explorer you can see all the files contained in your web site. Right now the
only ones are Default.aspx and Default.aspx.cs. If you cannot find Solution Explorer,
go to View, Solution Explorer menu.
A Web site consists of various web pages handling different tasks and presenting different
kind of information. A web page in ASP.NET is made up by two files one with .aspx
extension and the other one with .cs (deriving from the fact that we use C#) extension.
In the .aspx file we design the layout of the web page; where each control appears on
the browser, the look and feel of the web page and the number and type of objects (images,
buttons, text boxes, text, links etc) that make up your page. The .cs file contains
all the code behind our web page. All the logic, such as data manipulation and database
connectivity, goes there. This is called the code behind file, IE the file which has the
code for the aspx page we are dealing with.
But let's have a more hands on approach on a web page. In Visual Studio and as you have
Default.aspx file open, click the Design button on the left bottom. Now you can see how
your page will appear when displayed by a browser. Here you can drag and drop various
web controls available on the toolbox, usually located on your left. If you cannot find
the toolbox, go to View, Toolbox menu. On the toolbox, expand the Standard section (if not
already expanded). There you can see the most common web controls. Drag and drop two
textbox controls, a button and a label control on the page.
Right click on the button control and click properties. You can now see the properties
window of the button control. Change its text property from Button to OK. You can see its
caption changing. On the page, click the Source button (bottom left, next to the Design
button you clicked earlier) to switch to HTML view once again. Your web page now should
look something like this :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="OK" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>

One interesting thing is the CodeFile="Default.aspx.cs" on the first line. This denotes
what we mentioned earlier, that the code for the default.aspx page lies in the Default.aspx.cs
file.
You can change the properties of the controls in Source view as well. Change Label1's text property
from "Label" to "Enter two integers and click OK". Then add a
next to the button tag so that
the whole file looks like this :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text="Enter two integers and click OK"></asp:Label>
</div>
</form>
</body>
</html>

This way the label will appear on a separate line.

This far we have created a web page with a few controls on it but we haven't put any
logic behind it. Now it's time to do just that. Switch once again to design view on your
page (click the design button). Then double click on the button. Visual Studio opens the
Default.aspx.cs file. C# is an object oriented programming language. In other words, every
type is actually a class (even integers and strings). As far as web developing is concerned,
C# is also an event driven programming language. This means that everything takes place
inside handler functions which handle various events. In our example, when you double clicked
the button control, Visual Studio automatically created a function called Button1_Click
and registered is as an event handler for the onclick event of the button on your page.
You can see that, if you go to the Default.aspx file and switch to source view. The button
tag is now like this :

Which means that when the button is clicked, the Button1_Click function will be executed on
the Web Server.
There is also one more function in the default.aspx.cs file called Page_Load. This is an
automatically created event handler function which is called when the page loads (during a
client request or post back). This event is the first one to be executed no matter how many
events there are on the page. Postback is when a page is loaded in response to a client's
submit of a form. Ie the only time that a request is not considered to be a postback, is
when the page loads for the first time.
Add the following code in the button click event handler :

try
{
int result;
result = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text);
Label1.Text = result.ToString();
}
catch (Exception ex)
{
Label1.Text = "The data you entered could not be processed";
}

This code snippet will try to convert each textbox's text property from string to integer,
add them together and place the result in the Label1's text property. If the text of any of
the two text box controls cannot be converted to integer, an exception is thrown from the ToInt32
function of the Convert class. That is why we need to enclose our code in an exception handling
structure. If an exception is detected, we notify the user that we could not understand her
request.

Your file should now look like this :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
int result;
result = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text);
Label1.Text = result.ToString();
}
catch (Exception ex)
{
Label1.Text = "The data you entered could not be processed";
}
}
}

Run the sample by pressing F5, or Ctrl+F5 (run without debugger) or simply click on the play
button.

More Articles: http://www.article-library.net

by Karnage Article Source articles.ekland.net



Add this Article to your website/blog

Introduction to ASP.NET

Please Rate this Article

 

Not yet Rated

Click the XML Icon Above to Receive Computer Articles Via RSS!


Sponsors



Extra Information
| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
At least 100.000 Articles

FindingIT

Article-Library.Net


MyEasyDating
Sponsors
Links
elAdvertise

Powered by Article Dashboard