ASP.NET - UNIT 1
SYLLABUS :
UNIT
I : ASP.NET Basics Introduction to ASP.NET: .NET Framework
(CLR, CLI, BCL), ASP.NET Basics, ASP.NET Page Structure, Page Life Cycle.
Controls: HTML Server Controls, Web Server Controls, Web User Controls,
Validation Controls, Custom Web Controls.
The .NET Framework is
a software framework developed by Microsoft. The .NET runs on Microsoft
Windows. It provides language interoperability and can run across several
programming languages and platforms. Programs written for the .NET Framework
execute in a software environment, known as the Common Language Runtime (CLR). CLR is an application virtual machine
that provides services such as security, memory management, and exception
handling. The class libraries and the CLR together constitute the .NET
Framework.
The .NET Framework's
Base Class Library provides user interface, data access, database connectivity,
cryptography, web application development, numeric algorithms, and network
communications.
Framework |
Versions /Tools used |
Features |
.NET Framework 1.0 (February 2002) |
|
First version of .NET Framework. |
.NET Framework 1.1 (April 2003) |
C# Version: 1.2 CLR Version: 1.1 |
·
ASP.NET and ADO.NET updates ·
Side by side execution ·
.NET Compact framework ·
Secure coding guideline ·
Information about application deployment |
.NET Framework 2.0 |
C# Version: 2.0 CLR Version: 2.0 Development Tool : Visual Studio 2005 |
·
64-bit platform support ·
Access Control List Support ·
Data Protection APIs ·
Detecting changes in network connectivity ·
FTP Support ·
Web Services |
.NET Framework 3.0 |
C# Version: 3.0 CLR Ver: 2.0 Dev Tool: Visual Studio 2005 |
·
Windows Communication Foundation ·
Windows Presentation Foundation ·
Windows Forms |
.NET Framework 3.5 |
C# Version: 3.0 CLR Version: 2.0 Development Tool : Visual Studio 2008 |
·
ASP.NET Ajax enabled Websites ·
LINQ ·
Dynamic DataCollections ·
Time Zone Improvements ·
Cryptography Enhancements ·
Web Service interoperability |
.NET Framework 4.0 (April 2010) |
C# Version: 4.0 CLR Version: 4 Development Tool : Visual Studio 2010 |
·
Expanded Base Class ·
Cross-Platform Development with portable class library ·
Dynamic Language Runtime ·
Parallel Computing |
.NET Framework 4.6 (July 2015) |
C# Version: 6.0 |
·
Compilation Using .NET Native ·
ASP.NET Core 5 ·
HTTP/2 Support ·
64-bit JIT Compiler for managed code · SSL Support |
.NET Framework 4.7 (April 2017) |
C# Version: |
·
Enhanced cryptography with elliptic curve cryptography. ·
Support for High-DPI awareness support in Windows
Forms. ·
More support for touch and stylus in Windows
Presentation Foundation. |
.NET Framework 4.8 (April 2019) |
C# Version: |
·
Runtime – JIT improvements. ·
Runtime – Antimalware Scanning. ·
Windows Forms – Accessibility Enhancements. |
Common Language Infrastructure (CLI) provides a language-neutral
platform for application development and execution, including functions for
Exception handling, Garbage Collection, security, and interoperability.
By using
the features of the .NET Framework within the scope of the CLI, the
functionality will be available across all
the languages supported by the framework. Microsoft's implementation of the CLI
is called the Common Language Runtime, or CLR.
The
CIL code is housed in CLI assemblies. CLI assemblies are stored in the Portable
Executable (PE) format for all DLL and EXE files.
The
complete name of an assembly contains its simple text name, version number,
culture, and public key token. Assemblies are considered equivalent if they
share the same complete name, excluding the revision of the version number.
ASP.NET, the next version of ASP, is a programming framework used to create enterprise-class Web Applications. These applications are accessible on a global basis leading to efficient information management.
Client-side refers to browser and the machine running the browser. Server-side refers to Web server.
Client-Side Scripting JavaScript and VBScript are generally used for Client-side scripting and it is executed in the browser after the page is loaded. Both, HTML and the script are together in the same file and the script is downloaded as part of the page. A client-side script runs only on a browser that supports scripting and specifically the scripting language that is used. Since the script is in the same file as the HTML and as it executes on the machine you use, the page may take longer time to download.
Server-Side Scripting ASP.NET is purely server-side technology. ASP.NET code executes on the server before it is sent to the browser. The code that is sent back to the browser is pure HTML and not ASP.NET code. Like client-side scripting, ASP.NET code is written alongside HTML. Unlike client-side scripting, ASP.NET code is executed in server and not in the browser. The script that is present along with HTML is not sent back to the browser and that prevents others from stealing the code.
* windows
authentication * Forms Authentication
* Passport Authentication *
Custom Authentication
A web page is in 2 parts
1} Designing part
(HTML Content,Flash,Dreamweaver etc)
2} logic Part (sub programs and event
procedures and it has also your database interaction)
ASP.Net supports 2 techniques for
creating web page
1) In Page Technique when you
place design part code and logic part code with in a single file called as ASPX
then it is called as inPage Technique.
2) Code Behind Technique when
design part code is represented with in ASPX file and logic part code is
represented with in dll file then it is called as code behind technique.
ASP Supports only In Page technique.
DLL file is not a readable file so it is secured.
The important elements of an ASP.NET page are:
· Directives
· Code declaration blocks
· ASP.NET controls
· Code render blocks
· Server-side comments
· Server-side include directives
· Literal text and HTML tags
Directives A directive controls the compilation of ASP.NET. The
directive begins with the characters <%@
and the end of a directive is marked with the characters %>.
These instructions include registering
a custom control, page language etc. It describes how the .aspx pages (web
forms) or .ascx pages (user controls) are processed by the .net framework.
A directive can appear anywhere within a page. By convention, however, a directive typically appears at the top of an ASP.NET page.
There are several types of directives that can be added to an ASP.NET page. Two of the most useful types are page and import directives.
Page Directives A page directive is used to specify the default programming language for a page. Page directives can also be used to enable tracing and debugging for a page.
To change the default programming language of an ASP.NET page from Visual Basic to C#, for example, you would use the following page directive:
<%@ Page Language="C#" %>
NOTE : The keyword Page in a page directive is optional. The following two directives are equivalent: <%@ Page Language="C#" %>
<%@ Language="C#" %>
TRACE DIRECTIVE : If Trace directive is enabled, for a page, additional information about the execution of the page is displayed along with the content of the page. Tracing can be enabled for a page by including the following directive:
<%@ Page Trace="True" %>
After enabling tracing for a page, trace messages can be displayed by using two methods of the Trace class: Write() and Warn(). Either of these methods can be used to display custom message in the trace information displayed at the bottom of the page. The only difference between the two methods is that the former method displays messages in black text, and the latter method displays messages in red text, which is easier to see.
DEBUG DIRECTIVE To enable runtime error messages to be displayed on a page, the Debug directive is used. To display errors in ASP.NET page, the following directive is included:
<%@ Page Debug="True" %>
If this directive is included, when an error is encountered while processing the page, the error is displayed. In most cases, we can view source code for the exact statement that generated the error.
To enable both tracing and debugging for a page, combine the directives like this.
<%@ Page Debug="True" Trace="True" %>
IMPORT DIRECTIVES By default, only certain namespaces are automatically imported into ASP.NET page. Inorder to refer to a class that isn't a member of one of the default namespaces, the namespace must be explicitly imported or t use the fully qualified name of the class.
For example, to send an email from an ASP.NET page by using the Send method of the SmtpMail class, SmtpMail class is to added as an Import directive. The SmtpMail class is contained in the System.Web.Mail namespace. This is not a default namespace imported into an ASP.NET page.
The following page shows how to import the System.Web.Mail namespace and send email message.
ImportNamespace.aspx
<%@ Import Namespace="System.Web.Mail" %>
<Script
Runat="Server">
Sub Page_Load
Dim mailMessage As SmtpMail
mailMessage.Send( "bob@somewhere.com", "Sending Mail!", "Hello!" )
End Sub
</Script>
<html>
<head><title>ImportNamespace.aspx</title></head>
<body>
<h2>Email Sent!</h2>
</body>
</html>
The first line in the above Listing contains an import directive. Without the import directive, the page would generate an error because it would not be able to resolve the SmtpMail class.
@ assembly The @assembly directive attaches assemblies
to the page or an asp.net user control thereby all the assembly classes and
interfaces are available to the class. This directive supports the two
attributes name and src. The name attribute defines the assembly name and the
src attribute defines the source of the assembly.
@ control Defines control-specific attributes used by
the asp.net page parser and compiler and can be included only in .ascx files
(user controls).
@ implements The @implements directive gets the asp.net
pages to implement .net framework interfaces. This directive only supports a
single attribute interface.
@ master Identifies a page as a master page and
defines attributes used by the asp.net page parser and compiler and can be
included only in .master files.
@ page The @page directive enables you to specify
attributes and values for an asp.net page to be used when the page is parsed
and compiled. Every .aspx files should include this @page directive to execute.
There are many attributes belong to this directive.
@ reference Links a page, user control, or com control to the current page or user
control declaratively.
@ register Associates aliases with namespaces and classes,
which allow user controls and custom server controls to be rendered when
included in a requested page or user control.
CODE DECLARATION BLOCKS A code declaration block contains all the application logic for your ASP.NET page and all the global variable declarations, subroutines, and functions. It must appear within a <Script Runat="Server"> tag.
Codefile specifies a path to the referenced
code-behind file for the page. Inherits defines the name of the class from
which to inherit. This can be any class derived from the page class
The difference between Page_Init and
Page_load is that the controls are guaranteed to be fully loaded in the
Page_load.The controls are accessible in the Page_Init event,but the ViewState
is not loaded,so controls will have their default values,rather than any values
set during the postback.
HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute!, ASP.NET requires that all HTML elements must be properly closed and properly nested.
SYNTAX
<INPUT TYPE=TEXT RUNAT=SERVER>
HTML Server
Control |
Description |
HtmlAnchor |
Controls an <a>
HTML tag |
HtmlButton |
Controls a
<button> HTML tag |
HtmlForm |
Controls a
<form> HTML tag |
HtmlImage |
Controls an
<image> HTML tag |
HtmlInputButton |
Controls <input
type="button">, <input type="submit">, and
<input type="reset"> HTML tags |
HtmlInputCheckBox |
Controls an <input
type="checkbox"> HTML tag |
HtmlInputFile |
Controls an <input
type="file"> HTML tag |
HtmlInputHidden |
Controls an <input
type="hidden"> HTML tag |
HtmlInputImage |
Controls an <input
type="image"> HTML tag |
HtmlInputRadioButton |
Controls an <input
type="radio"> HTML tag |
HtmlInputText |
Controls <input
type="text"> and <input type="password"> HTML tags |
HtmlSelect |
Controls a
<select> HTML tag |
HtmlTable |
Controls a
<table> HTML tag |
HtmlTableCell |
Controls
<td>and <th> HTML tags |
HtmlTableRow |
Controls a <tr>
HTML tag |
HtmlTextArea |
Controls a
<textarea> HTML tag |
1.12.2 WEBSERVER
CONTROLS
Web server controls are special ASP.NET tags understood by the server. Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work.
Standard
Controls |
List
Controls |
Validation
Controls |
Data
bound Controls |
Misc
Controls |
§ label § Textbox § Button § Link
Button § Image
Button § Calendar § AdRotator § Panel § Place
Holder § Table § Literal
Control § Radio
Button § Check
Box § XML |
§ Radio
Button List § Check
Box List §
Dropdown List §
List Box |
1.
Required field Validator 2.
Range Validator 3.
Compare Validator 4.
Regular Expression Validator 5.
Custom Validator 6. Validation
Summary |
§ Data
Grid § Data
List § Repeater |
§
Crystal Report Viewer control |
Common Syntax for any web
server control
Eg: <asp:label id=“lb1”
runat=“server” text=“user name”></asp:label>
<asp:button id=“lb1”
runat=“server” text=“Login” />
Web
Server Control |
Description |
AdRotator |
Displays a sequence of images |
Button |
Displays a push button |
Calendar |
Displays a calendar |
CheckBox |
Displays a check box |
CheckBoxList |
Creates a multi-selection check box group |
DataGrid |
Displays fields of a data source in a grid |
DataList |
Displays items from a data source by using templates |
DropDownList |
Creates a drop-down list |
HyperLink |
Creates a hyperlink |
Image |
Displays an image |
ImageButton |
Displays a clickable image |
Label |
Displays static content which is programmable |
LinkButton |
Creates a hyperlink button |
ListBox |
Creates a single- or multi-selection drop-down list |
ListItem |
Creates an item in a list |
Literal |
Displays static content which is programmable |
Panel |
Provides a container for other controls |
PlaceHolder |
Reserves space for controls added by code |
RadioButton |
Creates a radio button |
RadioButtonList |
Creates a group of radio buttons |
BulletedList |
Creates a list in bullet format |
Repeater |
Displays a repeated list of items bound to the control |
Style |
Sets the style of controls |
Table |
Creates a table |
TableCell |
Creates a table cell |
TableRow |
Creates a table row |
TextBox |
Creates a text box |
1.12.3
TEXTBOX
TextBox is a server side
control. It is an input control which is used to take user input.
The asp tag to create TextBox is:
< asp:TextBoxID="TextBox1"
runat="server" ></asp:TextBox>
Notable Properties of TextBox are :
AccessKey : It is used to set keyboard shortcut for
the control.
TabIndex : The tab order of the control.
BackColor : It is used to set background color of
the control.
BorderColor : It is used to set border color of the
control.
BorderWidth : It is used to set width of border of the
control.
Font :
It is used to set font for the control text.
Text :
It is used to set text to be shown for the control.
ToolTip : It displays the text when mouse is
over the control.
Visible :
To set visibility of control on the form.
Height :
It is used to set height of the control.
Width :
It is used to set width of the control.
MaxLength :
It is used to set maximum number of characters that can be entered.
1.12.4 BUTTON
Buttons control is a
server side control and is used to perform events. It is used to submit client
request to the server.
The asp tag to create Button is:
< asp:ButtonID="Button1"
runat="server" Text="Submit" BorderStyle="Solid"
ToolTip="Submit"/>
Notable Properties of Button are :
AccessKey : It is used to set keyboard shortcut for
the control.
TabIndex : The tab order of the control.
BackColor : It is used to set background color of
the control.
BorderColor : It is used to set border color of the
control.
BorderWidth : It is used to set width of border of the
control.
Font :
It is used to set font for the control text.
ForeColor : It is used to set color of the control
text.
Text :
It is used to set text to be shown for the control.
ToolTip : It displays the text when mouse is
over the control.
Visible :
To set visibility of control on the form.
Height :
It is used to set height of the control.
Width :
It is used to set width of the control.
1.12.5 LABEL
Label control is used to display textual information on the web forms. It is mainly used to create caption for the other controls like textbox.
The asp tag to create Label is:
<
asp:LabelID="Label1" runat="server" Text="Label"
></asp:Label>
Notable Properties of Label are :
AccessKey : It is used to set keyboard shortcut for the label.
TabIndex : The tab order of the control.
BackColor : It is used to set background color of the label.
BorderColor : It is used to set border color of the label.
BorderWidth : It is used to set width of border of the label.
Font : It is used to set font for the label text.
ForeColor : It is used to set color of the label text.
Text : It is used to set text to be shown for the label.
ToolTip : It displays the text when mouse is over the label.
Visible : To set visibility of control on the form.
Height : It is used to set height of the control.
Width : It is used to set width of the control.
1.12.6 RadioButtonList The RadioButtonList Web server control is used to display a list of radio buttons. This control provides with a single-selection radio button group that can be dynamically generated via data binding. The Items property of this control allows us to add items to the control. To determine which item is selected, the SelectedItem property of the list is used.
RadioButtonList event : The default event of the RadioButtonList is the SelectedIndexChanged which looks like this in code:
Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As System.Object,_ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
End Sub
Notable properties of the RadioButtonList control are as follows:
Items : Gets/Sets the collection of items in the list
RepeatColumns : Gets/Sets the number of displayed columns in the radio button list
RepeatDirection : Gets/Sets the display direction of radio buttons
RepeatLayout : Gets/Sets the radio button layout
TextAlign : Gets/Sets the radio button's text alignment
RadioButtonListSample
The following code will displayed after adding
some items to the radio button
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As_ |
5. CheckBoxList The
CheckBoxList Web server control displays a number of checkboxes at once. This
control provides a multi selection check box group that can be dynamically
generated with data binding. It contains an Items collection with members that
correspond to individual items in the list. To determine which items are
checked, we need to test the Selected property of each item in the
list.
Notable properties of
the CheckBoxList control are as follows:
Items : Gets/Sets the collection of items in the list
RepeatColumns : Gets/Sets the number of displayed columns in the check box list
RepeatDirection : Gets/Sets the display direction of checkboxes
RepeatLayout : Gets/Sets the checkbox layout
TextAlign : Gets/Sets the check box's text alignment
CheckBoxList
Sample
Drag
a CheckBoxList, TextBox and a Button on to the form. Select the CheckBoxList
and add some items to it using the Items property. The following code will
display the text of the checkbox that is checked in the textbox when the button
is clicked.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As_ |
1.13 SAMPLE ASP.NET
PROGRAM (Using TextBox, RadioButton, Button):
Aim : To choose sex of a candidate from radiobutton, Read age of the candidate from a textbox and determine, whether the candidate is eligible for marriage.
<%@ Page Language="VB" CodeFile="Default.aspx.vb" %>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Style="left: 296px; position: absolute; top: 80px" Width="24px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Style="left: 160px; position: absolute; top: 216px" Width="216px"> </asp:TextBox>
<asp:Label ID="Label1" runat="server" Style= "left: 264px; position: absolute; top: 80px" Text="Age"> </asp:Label>
<asp:Label ID="Label2" runat="server" Style= "left: 8px; position: absolute; top: 80px" Text="Sex"> </asp:Label>
<asp:RadioButton ID="RadioButton1" runat="server" Style="left: 0px; position: absolute; top: 112px" Text="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" Style="left: 0px; position: absolute; top: 144px" Text="Female" />
<asp:Button ID="Button1" runat="server" Style="left: 88px; position: absolute;top: 216px" Text="CHECK" />
</div>
</form>
</body>
</html>
Protected Sub Button1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Button1.Click
If
RadioButton1.Checked = True And Val(TextBox1.Text) > 24 Then
TextBox2.Text = "ELIGIBLE FOR MARRIAGE"
Else
TextBox2.Text = "NOT ELIGIBLE FOR MARRIAGE"
End If
If
RadioButton2.Checked = True And Val(TextBox1.Text) > 21 Then
TextBox2.Text = "ELIGIBLE FOR MARRIAGE"
Else
TextBox2.Text = "NOT ELIGIBLE FOR MARRIAGE"
End If
RadioButton1.Checked = False
RadioButton2.Checked = False
End Sub