Home | Computer
ASP.NET is Microsoft's successor to ASP. It is a set of technologies used to buildweb applications in general, including Web Sites and XML Web Services. In this articlewe will deal with simple web site developing using C# (CSharp), so that you can getstarted with the ASP.NET technology and the .Net 2.0 Framework. C# is a .Net frameworkcapable 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/VistaVisual Studio 2005 Start Visual Studio 2005 and go to File, New, Web Site. From the templates list andpick ASP.NET Web Site (selected by default). Make sure that you have C# selected inthe language combo box. Pick a location where you want to save your project and clickOK.So far you should see a file called Default.aspx with some HTML code in it. In theSolution Explorer you can see all the files contained in your web site. Right now theonly 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 differentkind of information. A web page in ASP.NET is made up by two files one with .aspxextension 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 onthe 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 containsall the code behind our web page. All the logic, such as data manipulation and databaseconnectivity, goes there. This is called the code behind file, ie the file which has thecode 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 haveDefault.aspx file open, click the Design button on the left bottom. Now you can see howyour page will appear when displayed by a browser. Here you can drag and drop variousweb controls available on the toolbox, usually located on your left. If you cannot findthe toolbox, go to View, Toolbox menu. On the toolbox, expand the Standard section (if notalready expanded). There you can see the most common web controls. Drag and drop twotextbox controls, a button and alabel 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 itscaption changing. On the page, click the Source button (bottom left, next to the Designbutton 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 denoteswhat we mentioned earlier, that the code for the default.aspx page lies in the Default.aspx.csfile.You can change the properties of the controls in Source view as well. Change Label1's text propertyfrom "Label" to "Enter two integers and click OK". Then add a <br /> 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 anylogic behind it. Now it's time to do just that. Switch once again to design view on yourpage (click the design button). Then double click on the button. Visual Studio opens theDefault.aspx.cs file. C# is an object oriented programming language. In other words, everytype 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 placeinside handler functions which handle various events. In our example, when you double clickedthe button control, Visual Studio automatically created a function called Button1_Clickand 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 buttontag is now like this :<asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" />Which means that when the button is clicked, the Button1_Click function will be executed onthe Web Server.There is also one more function in the default.aspx.cs file called Page_Load. This is anautomatically created event handler function which is called when the page loads (during aclient request or post back). This event is the first one to be executed no matter how manyevents there are on the page. Postback is when a page is loaded in response to a client'ssubmit of a form. Ie the only time that a request is not considered to be a postback, iswhen 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 ofthe two text box controls cannot be converted to integer, an exception is thrown from the ToInt32function of the Convert class. That is why we need to enclose our code in an exception handlingstructure. If an exception is detected, we notify the user that we could not understand herrequest. 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 playbutton.
Article Source: http://www.SponsorDirectory.com/Free-Content
Free Content. Articles with images, find what you are searching for at articles.ekland.net
Please Rate this Article
5 out of 54 out of 53 out of 52 out of 51 out of 5
Not yet Rated