Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Ads by Lake Quincy Media
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Understanding Web Forms and Web services in ASP.NET

Understanding Web Forms and Web services in ASP.NET

In this article I will explain Web Forms and Web services in ASP.NET.

Author Rank:
Total page views :  1368
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor


This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

WebForms is a term Microsoft introduced when it released ASP+ for developing Web applications using the ASP model. 

Similar to windows application and services, you can also write Web applications and services. A web application is distributed application, which allows you to work and distribute functionality over the Web and provides user interfaces. Using Web Forms controls, you can write Web GUI applications similar to windows applications.

Web Forms

ASP.NET's Web Forms provide you with the ability to build Web-based GUI applications. Web Forms include Web pages (also called an ASP.NET page or web Forms page) and GUI components (sometimes called server controls) such as text boxes, buttons, list boxes, data grids, and so on. ASP.NET provides the flexibility to add these controls to an ASP.NET page at run-time as well as at design-time as well as at design-time. VS.NET provides design-time features to develop applications in no time. You add controls to a page by dragging controls from the toolbox to the page and then setting the controls' properties and event. Web Forms also provides a method for using the codebehind directive to separate your controls from the code. In other worlds you can write code in a separate file from the controls.

Web Service. 

Web services are applications that perform a certain task; they can be used by a single application as well as distributed on the Web. I'll cover Web services in the next article.

Developing Your First ASP.NET Web Application

Before I discuss the ASP .NET model in more depth, I'll show you how to develop your first ASP .NET application. In this example, I'll create a simple Web application using VS .NET. Similar to your first Windows application, I'll add a button, a text box, and a list box control to a Web page and then add text box contents to list box on the button-click event. 

Creating a Web Application Project 

Creating a new ASP.NET Web application using visual studio.NET is simple. First create a new project using File > New > Project > visual C# project and then select the ASP.NET Web Application template (see figure 7-1).

Figure-7.1.jpg

Figure 7-1. The First Web Application project

The Location box will show you the default of http://local and the application name. Here localhost represents the default IIS server running on your local machine. The default virtual directory for localhost is C:\Inetpub\wwwroot. If you're using a Web server installed on a network rather than your local machine, you need to use that server name. If you don't know the server name, you may want to contact your Web Server administrator. As you can see from figure 7-1, I'm using the MCB network Web Server. So if you see the name "MCB," don't worry. Just replace it with your own server's name. You can type either http://servername//application name or//server name//application name; both formats are correct. 

Clicking the OK button a new directory, FirstWebApplication, in the server's virtual directory. It also creates a new Web application and sends you to the default WebForm1.aspx page, as shown in figure 7-2.

Figure-7.2.jpg

Figure 7-2. Default web Form1.aspx page

From here you can edit your page's HTML. As you see in left-button corner of figure 7-2, there are two modes available: Design and HTML. Click the HTML button to edit the code, as shown in figure 7-3.

Figure-7.3.jpg

Figure 7-3. HTML view of web Form1.aspx

The HTML view shows you the HTML code of a page, its controls, and control properties. The HTML editor also lets you edit the HTML manually. Now if you switch the page view back to the design mode and right-click on Browser, View Code, Synchronize Document, and so on (see figure 7-4).

Figure-7.4.jpg

Figure 7-4. An ASP .NET page's right-click options

You can set a page's properties by selecting properties from the right-click menu. The properties menu opens the Document property pages window. As you can see from figure 7-5, there are three tabs available in the properties window: General, Color and Margins, and Keywords. Most of the properties are self-explanatory. On the General tab, the page Layout property has two options, GridLayout and FlowLayout. GridLayout is when you want drop controls to the page and reposition them. If you want to add text to the page, you should set the page layout to FlowLayout; other wise you won't be able to add text to the page. After setting the page layout property to flow Layout, the editor works as a text editor.

Figure-7.5.jpg

Figure 7-5. An ASP .NET document's page properties

Adding Web Controls to a Web Form

Similar to the Windows control toolbox, VS.NET provides a Web Forms control toolbox. You can open the toolbox by selecting the View > Toolbox main menu item. The web Forms toolbox looks like figure 7-6. The Web Forms category of the toolbox contains form controls and the HTML category contains HTML controls. The Data category provides the same data components you've seen in the Windows application toolbox. It has the same connection, data adapters, datasets, data views, and DataViewManager components. Figure 7-6 and 7-7 shows the Web Forms and HTML controls, respectively.

Figure-7.6.jpg

Figure 7-6. Web Forms controls

Figure-7.7.jpg

Figure 7-7. HTML Web Controls

For the application in this article, I'll be using the Web Forms controls. OK now switch the page back to the Design mode and GridLayout mode (if you changed its modes) and add a button, a text box, and a List Box to the form by dragging these controls from the Web Forms toolbox to WebForm1.aspx. The page should now look like figure 7-8.

Figure-7.8.jpg

Figure 7-8. WebForms1.aspx Design mode after adding Web Forms controls

Setting Control Properties

The next step is to add some text to the page and change some of the control's properties. To add text to the page, first you need to change the page layout to FlowLayout in the properties windows, which you can do by right-clicking on the page and selecting properties. Now add a heading to the page. I added two lines to the page and set a different font and font size for these lines. The first line text is "My First ASP.NET Application," and the second line text is "Click Add button to add contents of text to the list box." I also set some properties of the button and text box controls (see figure 7-9).

Figure-7.9.jpg

Figure 7-9. Properties window for the Web controls

Specifically, I changed the border, background color, font, and fore ground color of these controls. As you can see, changing these properties is similar to changing them for Windows application. The final page with these properties looks like figure 7-10.

Figure-7.10.jpg

Figure 7-10. Final page of the Web application after changing some control properties

Using Document Outline

Another nice feature of the visual Studio.NET IDE is that you can synchronize a Web page's controls with its contents in the Document outline viewer. This is really useful when you're developing a Web application with hundreds of controls; it can be hard to keep track of all the controls, HTML tags, and other page contents. The Document Outline viewer enables you to manage a page's contents in a tree format. The tree format displays all the page elements in the order they're called in the page. You can open the Document outline viewer by right-clicking on a Page and selecting Synchronize Document Outline (see Figure 7-11).

Figure-7.11.jpg

Figure 7-11. Calling the Document outline viewer

This action launches the Document outline viewer in the left pane (see figure 7-12). As you can see, the tree view displays the page contents, including the form, button, text box, and paragraph. If you click on a control in the Document Outline Viewer, it selects the corresponding control in the form. And vice versa, if you select a control in the form and make Document Outline the active window, the viewer selects that control in the tree view.

Figure-7.12.jpg

Figure 7-12. Document outline viewer

You can also use the Document Viewer's right-click menu to cut, paste, delete, view the code, and view the properties of these controls (see figure 7-13).

Figure-7.13.jpg

Figure 7-13. Document outline viewer's right-click options

Not only that, but now I'll show you one more thing. Select the HTML view of your page, and you can move to specific HTML tags using the Document Outline viewer. As you can see from figure 7-14, the tree view displays all the code of an HTML page in a nested structure as they're organized in the original code.

Figure-7.14.jpg

Figure 7-14. HTML view of Document Outline

So, the point is that you can find and organize your HTML code and controls from the Document Outline viewer instead of looking for a tag in the file manually.

Writing Code on the Button-Click Event Handler

The last step of this tutorial is to add an event handler for the button and write code to add some text box text to the list box. You add a control event similar to Windows applications. You can either double-click on the button or use the properties window's lighting icon to add an event. I'll add the button's click event as Button1_Click (see figure 7-15).

Figure-7.15.jpg

Figure 7-15. Adding an event handler to the button-click event

This action adds the Button1_Click method to the Web Form1.aspx.cs class, which hosts the code for the page controls and events. Now write a line of code to add the text box contents to the list box. Specifically, add the bold line in Listing 7-1 to the Button_Click method.

Listing 7-1. Adding the text box contents to the list box

        private void Button1_Click(object sender, System.EventArgs e)
        {
            ListBox1.Items.Add(TextBox1.Text.ToString());
        }

Note: You can also see the code using the View Code option of the page's right-click menu.

Now compile and run the project. The output of the program looks like figure 7-16 Clicking the Add button adds the text box contents to the list box.

Figure-7.16.jpg

Figure 7-16. Output Of our first Web application

After finishing this application, you can see the power and flexibility of ASP.NET and the VS.NET IDE. You've just developed a nice ASP.NET Web application with out any knowledge of ASP.NET and just by writing only one line of code.

Conclusion

Hope this article would have helped you in understanding Web Forms and Web services in ASP.NET. See other articles on the website also for further reference.

adobook.jpg
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.


Login to add your contents and source code to this article
 About the author
 
Puran Mehra

Working as a Software professional. 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.