Thursday, April 12, 2012

Creating an ASP.NET Application

Creating an ASP.NET Application
After you've set up the development environment for ASP.NET, you can create your first
ASP.NET Web application. You can create an ASP.NET Web application in one of the
following ways:
§ Use a text editor: In this method, you can write the code in a text editor, such
as Notepad, and save the code as an ASPX file. You can save the ASPX
file in the directory C:\inetpub\wwwroot. Then, to display the output of the
Web page in Internet Explorer, you simply need to type
http://localhost/.aspx in the Address box. If the IIS
server is installed on some other machine on the network, replace
"localhost" with the name of the server. If you save the file in some other
directory, you need to add the file to a virtual directory in the Default
WebSite directory on the IIS server. You can also create your own virtual
directory and add the file to it.

§ Use the VS.NET IDE: In this method, you use the IDE of Visual Studio .NET
to create a Web page in a WYSIWYG manner. Also, when you create a
Web application, the application is automatically created on a Web server
(IIS server). You do not need to create a separate virtual directory on the
IIS server.
From the preceding discussion, it is obvious that the development of ASP.NET Web
applications is much more convenient and efficient in Visual Studio .NET.
ASP.NET Web pages consist of HTML text and the code. The HTML text and the code
can be separated in two different files. You can write the code in Visual Basic or C#. This
separate file is called the code behind file. In this section, you'll create simple Web pages
by using VB as well as C#.
Cross-
Reference

Before you start creating a Web page, you should be familiar with basic ASP.NET
syntax. At the top of the page, you must specify an @ Page directive to define pagespecific
attributes, such as language. The syntax is given as follows:
<%@ Page attribute = value %>
To specify the language as VB for any code output to be rendered on the page, use the

following line of code:

<%@ Page Language = "VB" %>
This line indicates that any code in the block, <% %>, on the page is compiled by using
VB.
To render the output on your page, you can use the Response.Write() method. For
example, to display the text "hello" on a page, use the following code:
<% Response.Write("Hello") %>
Note The syntax used in the block, <% %>, must correspond to the
language specified in the @ Page directive. Otherwise, an error is
generated when you display the page in a Web browser.
You can use HTML tags in the argument passed to the Response.Write() method.
For example, to display the text in bold, you use the following code:
<% Response.Write(" Hello ") %>
For dynamic processing of a page, such as the result of a user interaction, you need to
write the code within the

In this syntax . . .

§ runat="server" indicates that the code is executed at the server side.
§ [language=codelanguage] indicates the language that is used. You can
use VB, C#, or JScript .NET. The square brackets indicate that this attribute
is optional. If you do not specify this attribute, the default language used is
VB.
After gaining an understanding of the basic ASP.NET page syntax, you can now create a
simple ASP.NET Web application. In the following sections, you'll create a simple Web
application by using VB and C#. To do so, you'll use the VS.NET IDE.

Creating a Visual Basic Web Application

You can create an ASP.NET application using Visual Basic by creating a Visual Basic
Web Application project. To do so, complete the following steps:
1. Select File ® New ® Project. The New Project dialog box appears.
2. Select Visual Basic Projects from the Project Types pane.
3. Select ASP.NET Web Application from the Templates pane. The
Name box contains a default name of the application. The Location
box contains the name of a Web server where the application will be
created. However, you can change the default name and location. In
this case, the name of the sample application is SampleVB. The New
Project dialog box now appears as shown in Figure.



VS.NET displays the application, as shown in Figure . By default, the file
WebForm1.aspx is selected and displayed. In addition to several other files,
WebForm1.vb is also created. You can write the page logic in this file. This file is the code behind file.


The WebForm1.aspx file is displayed in Design mode by default. To view the file in HTML mode, click HTML at the bottom of the WebForm1.aspx file window.
As you can see in HTML view, the language to be used on the page is VB. Any HTML
text or code (in the<% %> block) within the <Body> </Body> block is rendered on the
page when it is displayed in a Web browser.
The default background color of a page is white. You can change the background color
of a page by setting the bgcolor attribute of the element. When you set this
attribute, you are prompted to pick the color, as shown in Figure :




the value of the bgcolor attribute. A sample of such code is given as follows:
<Body bgcolor="#ccccff">
Write the following code within the <Body> </Body> element to display the text "Hello
World":
<% Response.Write("
Hello World

") %>
After you complete writing the code for your application, you need to build your
application so that you can execute it on a Web server. To build the project, choose Build
® Build.
Tip You can also build a project by pressing Ctrl + Shift + B.
When you build a project, the Web Form class file is compiled to a Dynamic Link Library
(DLL) file along with other executable files in the project. The ASPX file is copied to the
Web server without any compilation. You can change the ASPX file (only the visual
elements of the page) without recompiling, because the ASPX file is not compiled. Later,
when you run the page, the DLL and ASPX files are compiled into a new class file and
then run.
The output of the page that you developed is displayed in Figure 2-6.
Figure 2-6: A sample output of the Web page




Creating a C# Web Application
In addition to Visual Basic, you can also use C# to create ASP.NET Web applications.
To do so, you need to create a Visual C# Web application project as follows:
1. Select File ® New ® Project. The New Project dialog box appears.
2. Select Visual C# Projects from the Project Types pane.
3. Select Web Application from the Templates pane. The Name box
contains a default name of the application. The Location box contains
the name of a Web server where the application will be created.
However, you can change the default name and location. In this case,
the name of the sample application is SampleCSharp.
4. Click OK to complete the process.
When you switch to HTML view of the WebForm1.aspx file, you'll notice that the
language specified in the @ Page directive is C#. To create a Web page that displays
"Hello World," you simply need to write the following code in the <Body> </Body> block
of the page:
<% Response.Write("
Hello World

"); %>
Notice that the code in the <% %> block is terminated with a semicolon. This difference in
syntax is due to the fact that the language for this page is C# and not VB.
When you build the application and execute it, a Web page appears in the browser
displaying the text "Hello World."

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment