KEMBAR78
C# Unit 3 - 1 | PDF | World Wide Web | Internet & Web
0% found this document useful (0 votes)
60 views21 pages

C# Unit 3 - 1

Uploaded by

shreyaraut2512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views21 pages

C# Unit 3 - 1

Uploaded by

shreyaraut2512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1

Introduction to asp.net
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.
ASP.NET works on top of the HTTP protocol. and uses the HTTP commands and
policies to seta browser-to-server bilateral communication and cooperation.
ASP.NET is a part of Microsoft .Net platform. ASP NET applications are compiled codes, written
using the extensible and reusable components or objects present in .Net framework. These
codes can use the entire hierarchy of classes in .Net framework
The ASP.NET application codes can be written in any of the following languages:
● C#
● Visual Basic Net
● Jscript
● J#
ASP.NET is used to produce interactive, data-driven web applications over the internet It
consists of a large number of controls such as text boxes, buttons, and Labels for assembling,
configuring, and manipulating code to create HTML pages.
ASP.NET Web Forms Model
ASP.NET web forms extend the event-driven model of interaction to the web applications. The
browser submits a web form to the web server and the server returns a full markup page or
HTML page in response.
All client side user activities are forwarded to the server for stateful processing. The server
processes the output of the client actions and triggers the reactions.
Now, HTTP is stateless protocol. ASP NET framework helps in storing the information regarding
the state of the application, which consists of:
● Page state
● Session state
The page state is the state of the client, ie., the content of various input fields in the web form.
The session state is the collective information obtained from various pages the user visited
Let us take an example of a shopping cart User adds items to a shopping cart. Items are
selected from a page, say the items page, and the total collected items and price are shown on
a different page, say the cart page. Only HTTP cannot keep track of all the information coming
from various pages. ASP.NET session state and server side infrastructure keeps track of the
information collected globally over a session.
The ASP.NET runtime carries the page state to and from the server across page requests while
generating ASP.NET runtime codes, and incorporates the state of the
server side components in hidden fields. This way, the server becomes aware of the overall
application state and operates in a two-tiered connected way.
The ASP NET Component Model
The ASP.NET Component model provides various building blocks of ASP.NET pages.
Basically it is an object model, which describes:
Server side counterparts of almost all HTML elements or tags, such as <form>and <input>.
Server controls, which help in developing complex user-interface. For example.
the Calendar control or the Gridview controL.
2

Components of .Net Framework 3.5


Before going to the next session on Visual Studio. Net, let us go through at the various
components of the .Net framework 3.5. The following table describes the components of the.Net
framework 3.5 and the job they perform:
Components and their Description
(1) Common Language Runtime or CLR
It performs memory management, exception handing, debugging, security checking. thread
execution, code execution, code safety, verıfication, 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 anguage (IL) code. A
Just In TimeT)
(2).Net Framework Class Library
It contains a huge library of reusable types. dasses, 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. compiler compiles the IL code into native code,
which is CPU speafic
3

VB.NET
VB.NET is known as Visual Basic.NET. VB.NET is a simple, object-oriented programming
language developed by Microsoft in 2002, and it is the successor of Visual Basic 6 (VB6)
language, that implement on the .NET Framework. One of the best features of the VB.NET
language is that its program can also run on mono (multi-platform), which means that it is not
restricted to run on the Windows operating system; moreover, it can also run on Linux and Mac
OS. The Application built using the VB.NET is much secure, robust, high-performance, and
easy to develop.
There is some VB.NET application:
● Console Application
● Mobile application
● Windows forms Application
● Window control library
● .Net website
C#
C# is an open-source, general-purpose, object-oriented programming language that was
developed by Microsoft in the year 2000, which runs on the .NET Framework. Furthermore, this
language is also got certified as a standard programming language by the ECMA and ISO. The
main purpose of using the C# programming language is that it is used to share information as
well as services across all web services. It also enables the developer to develop a robust,
secure, and portable application easily.
Some of the application are listed below:
● Window and web-based application or services
● Distributed application
● Database application
● Difference Between VB.NET and C#
4

Understanding Namespaces and Assemblies


Whether you realize it at first, every piece of code in .NET exists inside a .NET type (typically a
class). In turn, every type exists inside a namespace. Figure 3-3 shows this arrangement for
your own code and the DateTime class. Keep in mind that this is an extreme simplification—the
System namespace alone is stocked with several hundred classes. This diagram is designed
only to show you the layers of organization.
5
6

The Anatomy of an ASP.NET Application


It's sometimes difficult to define exactly what a web application is. Unlike a traditional desk-top program
(which users start by running a stand-alone EXE file), ASP NET applications are almost always divided
into multiple web pages. This division means a user can enter an ASP NET
application at several different points or follow a link from the application to another
part of the website or another web server. So, does it make sense to consider a website as an
application?
In ASP NET, the answer is yes. Every ASP NET application shares a common set of resources
and configuration settings. Web pages from other ASP NET applications don't share these are
Resources, even if they're on the same web s server. Technically speaking, every ASP NET
application is executed inside a separate application domain. Application domains are isolated
areas in memory, and they ensure that even if one web application causes a fatal error, it's
unlikely to affect any other application that is currently running on the same computer. Similarly,
application domains restrict a web page in one application from accessing the in-memory
information of another application. Each web and has its own set of cached, application and
maintained separately
The standard definition of an ASP NET application describes it as a combination of files,
pages, handlers, modules, and executable code that can be invoked from a virtual directory
(and, optionally, its subdirectories) on a web server. In other words, the virtual directory is the
basic grouping structure that delimits an application. Figure 5-1 shows a web server that hosts
four separate web applications.
7

Table 5-1. ASP NET File Types


File Name. Description
Ends with .aspx
These are ASPNET web pages (the.NET equivalent of the .asp file in an ASP application). They
contain the user interface and, optionally the underlying application code. Users request or
navigate directly to one of these pages to start your web application.

Ends with .ascx


These are ASPNET user controls. User controls are similar to web pages, except that the user
can't access these files directly. Instead, they must be hosted inside an ASPNET web page.
User controls allow you to develop a small piece of user interface and reuse it in as many web
forms as you want without repetitive code. You'll learn about user controls in Chapter 12.
8

Ends with .asmx


These are ASPNET web services-collections of methods that can be called over the Internet.
Web services work differently than web pages, but they still share the same application
resources, configuration settings, and memory. You'll see an example that uses a web service in
Chapter 25.
web.config
This is the XML-based configuration file for your ASP NET application. It includes settings for
customizing security, state management, memory management, and much more. You'll get an
introduction to the web.config file in this chapter, and you'll explore its settings throughout this
book.
Global.asax
This is the global application file. You can use this file to define global variables (variables that
can be accessed from any web page in the web application) and react to global events (such as
when a web application first starts). You'll learn about it in Chapter 7.
Ends with.cs
These are code-behind files that contain C# code. They allow you to separate the application
logic from the user interface of a web page. We'll introduce the code-behind model in this
chapter and use it extensively in this book.

The Global.asax File


The Global.asax file allows you to write code that responds to global application events. These
events fire at various points during the lifetime of a web application, including when the
application domain is first created (when the first request is received for a page in your web-
site folder).
To add a Global.asax file to an application in Visual Studio, choose Website 1 Add New
Item, and select the Global Application Class file type. Then, click OK.
The Global.asax file looks similar to a normal .aspx file, except that it can't contain any
HTML or ASP NET tags. Instead, it contains event handlers. For example, the following
Global.asax file reacts to the Application.EndRequest event, which happens just before the
page is sent to the user:
<%@ Application Language="C#" %>
<script language="c#" runat="server">
protected void Application_OnEndRequest ()
Response. Write("<hr /> This page was served at " +
DateTime. Now. ToString () );
</script>
This event handler uses the Write) method of the built-in Response object to write a footer
at the bottom of the page with the date and time that the page was created (see Figure 5-10).
9

Each ASP NET application can have one Global.asax file. Once you place it in the appropriate website
directory, ASPNET recognizes it and uses it automatically. For example, if you add the Global.asax file
shown previously to a web application, every web page in that application will include a footer.
ASP.NET CONFIGURATION
Every web application includes a web.config file that configuration fundamental setting everything from
the way error message are so to the security setting that lock out unwanted visitor you will consider the
setting in the web.config file throughout this book
(And there are many more settings that you uon't consider in this book, because they're used
much more rarely.)
The ASPNET configuration files have several key advantages:
They are never locked: You can update web.config settings at any point, even while your
application is running. If there are any requests currently under way, they'll continue to use the
old settings, while new requests will get the changed settings right away.
They are easily accessed and replicated: Provided you have the appropriate network rights, you
can change web.config file from a remote computer. You can also copy the web.config file and
use it to apply identical settings to another application or another web server that runs the same
application in a web farm scenario.
The settings are easy to edit and understand: The settings in the web.config file are human
readable, which means they can be edited and understood without needing a special
configuration tool.
In the following sections, you'll get a high-level overview of the web.config file and learn
how ASPNET'S configuration system works.
The web.config File
The web.config file uses a predefined XML format. The entire content of the file is nested in a
root <configuration> element. Inside this element are several more subsections, some of
which you'll never change, and others which are more important.
Here's the basic skeletal structure of the web.config file, with the three most important
sections highlighted in bold:
10

<?Xml version="1.0" ?>


<configuration>
<config Sections>...</configSections>
cappSettings>...c/appSettings>
<connectionStrings>...</connectionStrings>
<system. web>. ..</system.web>
<System. codedom> ...</system. codedom>
(system.webServer>...</system. webServer>
</configuration>
Note that the web.config file is case-sensitive, like all XMIL documents, and starts every setting with a
lowercase letter. This means you cannot write <AppSettings> instead of cappSettings>.
As a web developer, there are three sections in the web.config file that you'll work with. The
<appSettings> section allows you to add your own miscellaneous pieces of information.
You'll learn how to use it in the next section. The connectionStrings> action allows you
Nested Configuration
ASPNET uses a multilayered configuration system that allows you to set settings at different levels.
Every web server starts with some basic settings that are defined in two configuration files in the
c:lWindows \ Microsoft.NET\Framework\v2.0.50727\ Config directory. These two files are machine.config
and web.config. Generally. you won't edit either of these files manually, because they affect the entire
computer. Instead, you'll configure the web.config file in your web application folder. Using that file, you
can set additional settings or override the defaults that are configured in the two system files.
More interestingly, you can use different settings for different parts of your application. To use this
technique, you need to create additional subdirectories inside your virtual directory.
These subdirectories can contain their own web.config files with additional settings.
Subdirectories inherit web.config settings from the parent directory. For example, imagine you create a
website in the directory c:\ASPNET\TestWeb. Inside this directory, you create a folder named Secure.
Pages in the c\ASPNETVTestWeb\ Secure directory can acquire settings from three files, as shown in
Figure 5-11.
11

Web form fundamental

{
Currency.Items.Add("Euro");
Currency.Items.Add("Japanese Yen");
Currency.Items.Add("Canadian Dollar");
}
}
12

The HTML Control Classes


Before you can continue any further with the currency converter, you need to know about the
control objects you’ve created. All the HTML server controls are defined in the
System.Web.UI.HtmlControls namespace. Each kind of control has a separate class. Table 5-3
describes the basic HTML server controls and shows you the related HTML element.
13

Page Class
One control you haven’t considered in detail yet is the Page class. As explained in the previous
chapter, every web page is a custom class that inherits from System.Web.UI.Page. By inheriting
from this class, your web page class acquires a number of properties and methods that your
code can use. These include properties for enabling caching, validation, and tracing, which are
discussed throughout this book. Table 5-9 provides an overview of some of the more
fundamental Page class properties, which you’ll use throughout this book.
14
15

This table omits some of the more specialized controls used for data, navigation, security, and
web portals.
Web Control Events and AutoPostBack
The previous chapter explained that one of the main limitations of HTML server controls is their
limited set of useful events—they have exactly two. HTML controls that trigger a postback, such
as buttons, raise a ServerClick event. Input controls provide a ServerChange event that doesn’t
actually fire until the page is posted back.
ASP.NET server controls are really an ingenious illusion. You’ll recall that the code in an
ASP.NET page is processed on the server. It’s then sent to the user as ordinary HTML. Figure
6-11 illustrates the order of events in page processing.
16
17

Figure 6.12 the postback processing sequence


18

ASP.NET supports two types of validation controls:


● Client-side validation controls
● Server-side validation controls
Client-side validation is good, but we must depend on browser and scripting language
support. The client-side validation is done in the user's browser using JavaScript and another
scripting. You can use client-side validation libraries such as WebUIValidation.js in .NET.
Server-side validation in ASP.NET is done in the C# code-behind, where the value of the user
input is read and validated on the server. This process is time-consuming but provides better
security and is easier to implement in ASP.NET. For example, if an app wants to check the date
range, the date value will be read from ASP.NET in C#, and the C# code behind the validation
method will be compared against the rules.

Rich Controls
ASP.NET provides large set of controls. These controls are divided into different categories,
depends upon their functionalities. The followings control comes under the rich controls
category.
● FileUpload control
● Calendar control
● AdRotator control
● MultiView control
● Wizard control
19

FileUpload control
FileUpload control is used to browse and upload files. After the file is uploaded, you can store the file on
any drive or database. FileUpload control is the combination of a browse button and a text box for
entering the filename.
The FileUpload control supports the following important properties.

FileBytes: It returns the contents of uploaded file as a byte array


FileContent: You can get the uploaded file contents as a stream.
FileName: Provides the name of uploaded file.
HasFile: It is a Boolean property that checks whether particular file is available or not.
PostedFile: Gets the uploaded file wrapped in the HttpPostedFile object.

Calendar control
Calendar control provides you lots of property and events. By using these properties and events
you can perform the following task with calendar control.
Select date.
Selecting a day, a week or a month.
Customize the calendar's appearance.
The Calendar control supports three important events:

When you select a date, SelectionChanged event will fired and displays the date in a label
controls. In this example the date format is MM/DD/YYYY.
20

AdRotator control
AdRotator control is used to display different advertisements randomly in a page. The list of
advertisements is stored in either an XML file or in a database table. Lots of websites uses AdRotator
control to display the advertisements on the web page.
Important properties of AdRotator control.
ImageUrl: The URL of the image that will be displayed through AdRotator control.
NavigateUrl: If user clicks the banner or ad then the new page opened according to givenURL.
AlternateText: It is used for displaying text instead of the picture if picture is not displayed. It is also used
as a tooltip.
Impressions: It is a number that sets how frequently an advertisement will appear.
Keyword: It is used to filter ads or identifies a group of advertisement.
MultiView control
MultiView control can be used when you want to create a tabbed page. In many situations, a
web form may be very long, and then you can divide a long form into multiple sub forms.
MultiView control is made up of multiple view controls. You can put multiple ASP.NET controls
inside view controls. One View control is displayed at a time and it is called as the active view.
View control does not work separately. It is always used with a Multiview control.
If working with Visual Studio 2010 or later, you can drag and drop a MultiView control onto the
form. You can drag and drop any number of View controls inside the MultiView control. The
number of view controls is depends upon the need of your application.
MultiView control supports the following important properties
ActiveViewIndex: It is used to determine which view will be active or visible.
Views: It provides the collection of View controls contained in the MultiView control.
For understand the Multiview control, first we will create a user interface as given below.
In the given example, in Multiview control, we have taken three separate View control.
1. In First step we will design to capture Product details
2. In Second step we will design to capture Order details
3. Next we will show summary for confirmation.
Here we have not used any database programming to save the data into the database. We will
show only the confirmation page, that data has been saved.
21

Wizard Control
This control is same as MultiView control but the main difference is that, it has inbuilt navigation
buttons.
The wizard control enables you to design a long form in such a way that you can work in
multiple sub form. You can perform the task in a step by step process. It reduces the work of
developers to design multiple forms. It enables you to create multi step user interface. Wizard
control provides with built-in previous/next functionality.

The Wizard control can contains one or more WizardStep as child controls. Only one
WizardStep is displayed at a time. WizardStep control has an important property called as
StepType. The StepType property determines the type of navigation buttons that will be
displayed for that step. The possible values are:
The StepType associated with each WizardStep determines the type of navigation buttons that
will be displayed for that step. The StepTypes are:
● Start:
● Step:
● Finish:
● Complete:
● Auto:
Important events of Wizard control are as follows:

ActiveStepChanged:
CancelButtonClick:
FinishButtonClick:
NextButtonClick:
PreviousButtonClick:
Now we will create an application as we had done with MultiView control. We will create three
different WizardStep in Wizard control.
1. In First step we will design to capture Product details
2. In Second step we will design to capture Order details
3. Next we will show summary for confirmation.

You might also like