ASP.NET Programming: Controls, DB Connectivity, Grid View, State Management, Exercises of Web Design and Development

A detailed guide on various aspects of asp.net programming, including html and web server controls, web user controls, validation controls, data base connectivity, grid view management, and state management. It covers writing programs for calculators, implementing validation controls, and performing database operations using procedures. The document also introduces asp.net ajax and its components, as well as the asp.net component model and page life cycle.

Typology: Exercises

2017/2018

Uploaded on 11/25/2018

kamal5257
kamal5257 🇮🇳

1 document

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PRACTICAL FILE
Workshop on Advanced Web Development
(BSBC-504)
INDEX
Submitt
ed By :
Submitt
ed To :
Chan
digar
h
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download ASP.NET Programming: Controls, DB Connectivity, Grid View, State Management and more Exercises Web Design and Development in PDF only on Docsity!

PRACTICAL FILE

Workshop on Advanced Web Development

(BSBC-504)

INDEX

Submitt

ed By :

Submitt

ed To :

Chan

digar

h

Sr no

Topic Name of the Program Pg. no

1 Introduction to ASP.NET:

♦ NET Framework (CLR, CLI, BCL)

♦ ASP.NET Basics

2 Controls: ♦ (Properties and Methods of)

  • HTML Server Controls
  • Web Server Controls
  • Web User Controls
  • Validation Controls

♦ Write a Program to make calculator using controls

♦ Write a program to implement validation controls

3 Data Base Connectivity

♦. Introduction to ADO.NET, Data Binding,

♦ Procedure to create database connection.

♦ Program to make form for database ♦ (^) Program to insert data in database ♦ Program to display data in data list ♦ Program to update data ♦ Program to delete data ♦ Program to display data ♦ Program to perform

.NET Framework

ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites. It allows you to use a full featured programming language such as C# or VB.NET to build web applications easily

Components of .Net Framework 3.

(1) Common Language Runtime or CLR It performs memory management, exception handling, debugging, security checking, thread execution, code execution, code safety, verification, and compilation. The code that is directly managed by the CLR is called the managed code. When the managed code is compiled, the compiler converts the source code into a CPU independent intermediate language (IL) code. A Just In Time(JIT) compiler compiles the IL code into native code, which is CPU specific. (2) .Net Framework Class Library It contains a huge library of reusable types. classes, interfaces, structures, and enumerated values, which are collectively called types.

(3) Common Language Specification It contains the specifications for the .Net supported languages and implementation of language integration. (4) Common Type System It provides guidelines for declaring, using, and managing types at runtime, and cross-language communication. (5) Metadata and Assemblies Metadata is the binary information describing the program, which is either stored in a portable executable file (PE) or in the memory. Assembly is a logical unit consisting of the assembly manifest, type metadata, IL code, and a set of resources like image files. (6) Windows Forms Windows Forms contain the graphical representation of any window displayed in the application. (7) ASP.NET and ASP.NET AJAX ASP.NET is the web development model and AJAX is an extension of ASP.NET for developing and implementing AJAX functionality. ASP.NET AJAX contains the components that allow the developer to update data on a website without a complete reload of the page. (8) ADO.NET It is the technology used for working with data and databases. It provides access to data sources like SQL server, OLE DB, XML etc. The ADO.NET allows connection to data sources for retrieving, manipulating, and updating data. (9) Windows Workflow Foundation (WF) It helps in building workflow-based applications in Windows. It contains activities, workflow runtime, workflow designer, and a rules engine. (10) Windows Presentation Foundation It provides a separation between the user interface and the business logic. It helps in developing visually stunning interfaces using documents, media, two and three dimensional graphics, animations, and more. (11) Windows Communication Foundation (WCF) It is the technology used for building and executing connected systems. (12) Windows CardSpace It provides safety for accessing resources and sharing personal information on the internet. (13) LINQ It imparts data querying capabilities to .Net languages using a syntax which is similar to the tradition query language SQL.

ASP.NET - Introduction ASP.NET is a web development platform, which provides a programming model, a comprehensive software infrastructure and various services required to build up robust web applications for PC, as well as mobile devices.

application is made of pages. When a user requests an ASP.NET page, the IIS delegates the

processing of the page to the ASP.NET runtime system. The ASP.NET runtime transforms the .aspx page into an instance of a class, which inherits from the base class page of the .Net framework. Therefore, each ASP.NET page is an object and all its components i.e., the server-side controls are also objects.

ASP.NET provides an abstraction layer on top of HTTP on which the web applications are built. It provides high-level entities such as classes and components within an object-oriented paradigm. The key development tool for building ASP.NET applications and front ends is Visual Studio. In this tutorial, we work with Visual Studio 2008. Visual Studio is an integrated development environment for writing, compiling, and debugging the code. It provides a complete set of development tools for building ASP.NET web applications, web services, desktop applications, and mobile applications.

Installation

Microsoft provides a free version of visual studio which also contains SQL Server and it can be downloaded from www.visualstudio.com. Step 1 − Once downloading is complete, run the installer. The following dialog will be displayed.

Step 2 − Click on the Install button and it will start the installation process.

Step 4 − Open Visual Studio from start Menu which will open the following dialog. It will be a

while for the first time for preparation.

Step 5 − Once all is done you will see the main window of Visual studio.

Let’s create a new project from File → New → Project

  • Top level items in the application are compiled.
  • Response objects are created. The application objects such as Http Context, Http Request and Http Response are created and initialized.
  • An instance of the Http Application object is created and assigned to the request.
  • (^) The request is processed by the Http Application class. Different events are raised by this class for processing the request.

ASP.NET Page Life Cycle

When a page is requested, it is loaded into the server memory, processed, and sent to the browser. Then it is unloaded from the memory. At each of these steps, methods and events are available, which could be overridden according to the need of the application. In other words, you can write your own code to override the default code. The Page class creates a hierarchical tree of all the controls on the page. All the components on the page, except the directives, are part of this control tree. You can see the control tree by adding trace= "true" to the page directive. We will cover page directives and tracing under 'directives' and 'event handling'. The page life cycle phases are:

  • Initialization
  • Instantiation of the controls on the page
  • Restoration and maintenance of the state
  • Execution of the event handler codes
  • Page rendering

Understanding the page cycle helps in writing codes for making some specific thing happen at any stage of the page life cycle. It also helps in writing custom controls and initializing them at right time, populate their properties with view-state data and run control behavior code. Following are the different stages of an ASP.NET page:

  • Page request - When ASP.NET gets a page request, it decides whether to parse and compile the page, or there would be a cached version of the page; accordingly the response is sent.
  • Starting of page life cycle - At this stage, the Request and Response objects are set. If the request is an old request or post back, the IsPostBack property of the page is set to true. The UICulture property of the page is also set.
  • Page initialization - At this stage, the controls on the page are assigned unique ID by setting the UniqueID property and the themes are applied. For a new request, postback data is loaded and the control properties are restored to the view-state values.
  • Page load - At this stage, control properties are set using the view state and control state values.
  • Validation - Validate method of the validation control is called and on its successful execution, the IsValid property of the page is set to true.
  • Postback event handling - If the request is a postback (old request), the related event handler is invoked.
  • Page rendering - At this stage, view state for the page and all controls are saved. The page calls the Render method for each control and the output of rendering is written to the OutputStream class of the Response property of page.
  • (^) Unload - The rendered page is sent to the client and page properties, such as Response and Request, are unloaded and all cleanup done.

ASP.NET Page Life Cycle Events

At each stage of the page life cycle, the page raises some events, which could be coded. An event

handler is basically a function or subroutine, bound to the event, using declarative attributes such as Onclick or handle. Following are the page life cycle events:

  • (^) PreInit - PreInit is the first event in page life cycle. It checks the IsPostBack property and determines whether the page is a postback. It sets the themes and master pages, creates dynamic controls, and gets and sets profile property values. This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.
  • Init - Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.
  • InitComplete - InitComplete event allows tracking of view state. All the controls turn on view-state tracking.
  • LoadViewState - LoadViewState event allows loading view state information into the controls.
  • (^) LoadPostData - During this phase, the contents of all the input fields are defined with the tag are processed.
  • PreLoad - PreLoad occurs before the post back data is loaded in the controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.
  • Load - The Load event is raised for the page first and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.
  • LoadComplete - The loading process is completed, control event handlers are run, and page validation takes place. This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler
  • PreRender - The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.
  • Pre Render Complete - As the Pre Render event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.
  • Save State Complete - State of control on the page is saved. Personalization, control state and view state information is saved. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page Render handler.
  • Unload - The Unload phase is the last phase of the page life cycle. It raises the Unload event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed. This event can be handled by modifying the On Unload method or creating a Page Unload handler.

Adding Folders and Files to your Website

When a new web form is created, Visual Studio automatically generates the starting HTML for

the form and displays it in Source view of the web forms designer. The Solution Explorer is used to add any other files, folders or any existing item on the web site.

  • To add a standard folder, right-click on the project or folder under which you are going to add the folder in the Solution Explorer and choose New Folder.
  • To add an ASP.NET folder, right-click on the project in the Solution Explorer and select the folder from the list.
  • To add an existing item to the site, right-click on the project or folder under which you are going to add the item in the Solution Explorer and select from the dialog box.

Projects and Solutions

A typical ASP.NET application consists of many items: the web content files (.aspx), source files (.cs files), assemblies (.dll and .exe files), data source files (.mdb files), references, icons, user controls and miscellaneous other files and folders. All these files that make up the website are contained in a Solution. When a new website is created. VB2008 automatically creates the solution and displays it in the solution explorer. Solutions may contain one or more projects. A project contains content files, source files, and other files like data sources and image files. Generally, the contents of a project are compiled into an assembly as an executable file (.exe) or a dynamic link library (.dll) file. Typically a project contains the following content files:

  • Page file (.aspx)
  • User control (.ascx)
  • Web service (.asmx)
  • Master page (.master)
  • Site map (.sitemap)
  • Website configuration file (.config)

Building and Running a Project

You can execute an application by:

  • Selecting Start
  • Selecting Start Without Debugging from the Debug menu,
  • pressing F
  • Ctrl-F

The program is built meaning, the .exe or the .dll files are generated by selecting a command from the Build menu.

HTML ServerControls

The HTML server controls are basically the standard HTML controls enhanced to enable server side processing. The HTML controls such as the header tags, anchor tags, and input elements are not processed by the server but are sent to the browser for display. They are specifically converted to a server control by adding the attribute run at="server" and adding an id attribute to make them available for server-side processing. For example, consider the HTML input control:

It could be converted to a server control, by adding the run at and id attribute:

Advantages of using HTML Server Controls

Although ASP.NET server controls can perform every job accomplished by the HTML server controls, the later controls are useful in the following cases:

  • HTML Server controls.
  • ASP.NET Server controls.
  • ASP.NET Ajax Server controls.
  • User controls and custom controls.

ASP.NET server controls are the primary controls used in ASP.NET. These controls can be grouped into the following categories:

  • Validation controls - These are used to validate user input and they work by running client-side script.
  • Data source controls - These controls provides data binding to different data sources.
  • (^) Data view controls - These are various lists and tables, which can bind to data from data sources for displaying.
  • Personalization controls - These are used for personalization of a page according to the user preferences, based on user information.
  • Login and security controls - These controls provide user authentication.
  • Master pages - These controls provide consistent layout and interface throughout the application.
  • Navigation controls - These controls help in navigation. For example, menus, tree view etc.
  • Rich controls - These controls implement special features. For example, AdRotator, File Upload, and Calendar control.

The syntax for using server controls is:

In addition, visual studio has the following features, to help produce in error-free coding:

  • Dragging and dropping of controls in design view.
  • IntelliSense feature that displays and auto-completes the properties.
  • The properties window to set the property values directly.

Properties of the Server Controls

ASP.NET server controls with a visual aspect are derived from the WebControl class and inherit all the properties, events, and methods of this class. The WebControl class itself and some other server controls that are not visually rendered are derived from the System.Web.UI.Control class. For example, PlaceHolder control or XML control. ASP.Net server controls inherit all properties, events, and methods of the WebControl and System.Web.UI.Control class. The following table shows the inherited properties, common to all server controls:

Property Description AccessKey Pressing this key with the Alt key moves focus to the control. Attributes It is the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control. BackColor Background color. BindingContainer The control that contains this control's data binding.

BorderColor Border color.

BorderStyle Border style.

BorderWidth Border width.

CausesValidation Indicates if it causes validation.

ChildControlCreated It indicates whether the server control's child controls have been created.

ClientID Control ID for HTML markup.

Context The HttpContext object associated with the server control.

Controls Collection of all controls contained within the control.

ControlStyle The style of the Web server control.

CssClass CSS class

DataItemContainer Gets a reference to the naming container if the naming container implements IDataItemContainer.

DataKeysContainer Gets a reference to the naming container if the naming container implements IDataKeysControl.

DesignMode It indicates whether the control is being used on a design surface.

DisabledCssClass Gets or sets the CSS class to apply to the rendered HTML element when the control is disabled.

Enabled Indicates whether the control is grayed out.

EnableTheming Indicates whether theming applies to the control.

EnableViewState Indicates whether the view state of the control is maintained.

Events Gets a list of event handler delegates for the control.

Font Font.

Forecolor Foreground color.

HasAttributes Indicates whether the control has attributes set.

HasChildViewState Indicates whether the current server control's child controls have any saved view-state settings.

Height Height in pixels or %.

ID Identifier for the control.

IsChildControlStateCleared Indicates whether controls contained within this control have control state.

IsEnabled Gets a value indicating whether the control is enabled.

IsTrackingViewState It indicates whether the server control is saving changes to its view state.

IsViewStateEnabled It indicates whether view state is enabled for this control.

LoadViewStateById It indicates whether the control participates in loading its view state by ID instead of index.

Page Page containing the control.

Parent Parent control.

RenderingCompatibility It specifies the ASP.NET version that the rendered HTML will be compatible with.

Site The container that hosts the current control when rendered on a design surface.