Professional Documents
Culture Documents
AWP Unit II
AWP Unit II
NET
• ASP.NET is a programming framework used to create Web
Applications for PC as well as mobile devises.
• Seamless upgrades
• Availability from any machine over a network like internet
or intranet without you having to install client software and
works as long as the user has a working browser
Web Applications
– A web application consists of a set of web pages that are
generated in response of user requests.
Cntd..
Web Applications
– The components of a web application includes:
Clients
A server
A network connecting clients to server
Web Forms
– For a web application, content is generated from a web form.
– The browser submits a web form to the web server and the
server returns an HTML page in response.
What is rendering a web page?
• You can drag controls from the Toolbox onto the design surface of
the page.
• Or, move the cursor to where you want the control in either Source
or Design view, and double click on the control in the Toolbox to
place it there.
• For the controls that ship with ASP.NET 4 you always use the asp:
prefix followed by the name of the control.
• Example;-
<asp:Button>
<asp:TextBox>
<asp:Label>
Label Control
• Label controls are used to display text on a form.
• The click event of a button does not send any parameters to the code
behind file.
• ListControl.Items Property
• public virtual System.Web.UI.WebControls.ListItemCollection Items { get; }
• ListItemCollection class
• The ListItemCollection class represents a collection of ListItem objects.
• The ListItem objects, in turn, represent the items displayed in list controls such
as the DropDowmList and ListBox.
• Methods
Add(String) //Appends a ListItem to the end of the collection that represents the specified string.
• ListItem class
• It is a sealed library class.
• ListItem(String) --- Initializes a new instance of the ListItem class
with the specified text data.
• ListItem(String, String) ---- Initializes a new instance of the
ListItem class with the specified text and value data.
ListItem
Property Description
Selected Gets or sets a value indicating whether the item is
selected.
• public virtual int[] GetSelectedIndices()
• Say for example, if ID of the list box is ‘ListBox1’ then the following code
displays all selected items from the list box to a label with ID ‘Label1’.
int[] x = ListBox1.GetSelectedIndices();
Label1.Text = "";
for (int i = 0; i < x.Length; i++)
Label1.Text += ListBox1.Items[x[i]].Text + "<br>";
SelectedIndexChanged Event
• Event: CheckedChanged
<asp:ListItem>
RadioButtonList
Property Description
CellPadding The amount of pixels between the border and the
contents of the table cell
CellSpacing The amount of pixels between table cells
RepeatColumns The number of columns to use when displaying the
check box group
RepeatDirection Specifies whether the check box group should be
repeated horizontally or vertically
runat Specifies that the control is a server control. Must be set
to "server"
TextAlign On which side of the check box the text should appear
RadioButtonList Example
aspx code
<asp:RadioButtonList AutoPostBack="true" CellSpacing="2"
CellPadding="10" RepeatDirection="Vertical" ID="r1" runat="server"
onselectedindexchanged="r1_SelectedIndexChanged">
<asp:ListItem Value="0">Windows 7</asp:ListItem>
<asp:ListItem Value="1">Macintosh</asp:ListItem>
<asp:ListItem Value="2">Linux</asp:ListItem>
</asp:RadioButtonList>
Code file
protected void r1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = RadioButtonList1.SelectedItem.Text;
}
RadioButton
button group.
• Event: CheckedChanged
Properties Description
BackImageUrl URL of the background image of the panel.
1. RequiredFieldValidator Control
2. RangeValidator Control
3. RegularExpressionValidator Control
4. CompareValidator Control
5. CustomValidator Control
Validation Controls
Common Validator Properties
RequiredFieldValidator Controls
• It ensures that the user has entered a value in the associated input
control.
• Use the RequiredFieldValidator control to make an input control a
mandatory field.
• By default, the initial value is an empty string (""), which
indicates that a value must be entered in the input control for it to
pass validation.
• Both server-side and client-side validations are performed
RequiredFieldValidator Properties
Property Description
ControlToValidate The ID of the control to validate
Display •None (the control is not displayed and error message is shown in the
ValidationSummary control)
•Static (in case of failure error message is shown in the space reserved).
•Dynamic (In case of failure error message is shown. Space is not
reserved.)
EnableClientScript A Boolean value that specifies whether client-side validation is enabled
or not
Enabled A Boolean value that specifies whether the validation control is enabled
or not
ErrorMessage The text to display in the ValidationSummary control when validation
fails. Note: This text will also be displayed in the validation control if the
Text property is not set
id A unique id for the control
InitialValue Specifies the starting value of the input control. Default value is ""
runat Specifies that the control is a server control. Must be set to "server"
Text The message to display when validation fails
ValidationGroup Using this property we can assign every validation control to a group.
SetFocusOnError Set focus to the input control in case of error.
A property of required field validator
RequiredFieldValidator Example
• The RequiredFieldValidator control is used to make an input control a required
field.
.
.
.
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Enter your name:"></asp:Label>
<asp:textbox id="Name" runat="server"/>
<asp:RequiredFieldValidator id="rfv" runat="server"
ControlToValidate="Name"
ErrorMessage="*Name is mandatory" ForeColor="Red"
Display="dynamic"> </asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Update Button" />
</form>
.
.
.
ValidationSummary
• The ValidationSummary control does not perform any validation but
shows a summary of all errors in the page.
• The summary displays the values of the ErrorMessage property of all
validation controls that failed validation.
• The following are two important properties:
DayHeaderStyle Sets style for the day heading part ( weekdays name)
DayStyle Sets the style properties for the days in the displayed
month.
NextPrevStyle Sets style for Previous and Next month navigation
part.
OtherMonthDayStyle Sets style for the numbered tiles which comes from
previous month and next month.
SelectedDayStyle Sets style for all the days selected in the control.
SelectorStyle Sets style for the Week and Month Selector of it.
TitleStyle Sets style for the Calendar’s Title( Month and Year )
TodaysDayStyle Sets style for the Today’s Date.
WeekendDayStyle Sets style for numbered tile which comes from
weekend days.
Cntd. to next slide
Calendar Control Events
Event Description
SelectionChanged It is raised when a day, a week or an entire
month is selected.
DateTime d = e.Day.Date;
string s = d.Day + "/" + d.Month + "/" + d.Year;
if (ht[s] != null)
{
Literal x = new Literal();
x.Text = "<br/>";
e.Cell.Controls.Add(x);
Label lb = new Label();
lb.Text = ""+ht[s];
e.Cell.Controls.Add(lb);
}
}
Selecting entire week or month
• The navigation controls consume this file and display its contents as
</siteMapNode>
</siteMapNode>
</siteMap>
SiteMapDataSource Control
• A TreeView or Menu should be bound to SiteMapDataSourceControl.
Ways to create a SitemapDataSource
• There are two ways:
1. Drag the control from the Toolbox. ( ToolBox >> Data >>
SiteMapDataSource)
2. Click the Smart Tag icon in the upper right corner of TreeView or
Menu control, then select New Data Source in the Choose Data
the Items property.
web site.
• SiteMapPath control is a series of links that lead the let the user to
• No SiteMapDataSource is required.
1. The HTML code page with .aspx extension maintains the code for
User Interface design.
2. The .aspx.cs file maintains the program logic.
• The file that contain the programming logic (.aspx.cs file) is known as
the code behind file.
• Only server controls can interact with the code behind the page; not the
HTML controls.
Code Behind Model
Using Code Behind Class
• The code behind class inherits from the Page class in the
System.Web.UI namespace.
• Therefore our web page inherits methods and properties from Page
class.
• All ASP.NET pages inherits from the Page class, or from a class which
inherits from the Page class.
Using Code Behind Class
• Visual Studio .NET uses Codebehind attribute to maintain a reference to
the associated code-behind file for the .aspx page.
CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication15.WebForm1" %>
Using Code Behind Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication16
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Working of Code Behind Model
1. The code-behind file contains a partial class that inherits from Page class.
2. The .aspx file contains an Inherits attribute in the @ Page directive that
points to the code-behind partial class.
4. Then ASP.NET generates another class that inherits from the class generated
in Step 3. This class contains the code required to build the page.
Stages in ASP.Net Page Life Cycle
Stage Description
When the page is requested by a user, ASP.NET determines whether to
Page request parse and compile the page or there would be a cached version of the
page; accordingly the response is sent
In the start stage, page properties such as Request and Response are set.
Start At this stage, the page also determines whether the request is a postback
or a new request and sets the IsPostBack property.
During page initialization, controls on the page are available and each
Initialization
control's UniqueID property is set.
During load, if the current request is a postback, control properties are
Load
loaded with information recovered from view state and control state.
Postback event if the request is a postback, the related event handler is called. After that,
handling the Validate method of all validator controls is called.
Before rendering, view state is saved for the page and all controls. During
the rendering stage, the page calls the Render method for each control,
Rendering
providing a text writer that writes its output to the OutputStream object
of the page's Response property.
Unload The rendered page is sent to the client and page properties, such as
Response and Request are unloaded and all cleanup done.
The ASP.NET Page life Cycle Events
Inside the life cycle of an ASP.Page there is a collection of events and some
of these events can be controlled through user code events
PreInit
• It is the first event in page life cycle.
• Triggered after the start stage is complete and before the initialization stage
begins.
• This event can be used to checks the IsPostBack property to determine
whether the page is requested for the first time or is it a subsequent request ,
creates or re-creates dynamic controls, sets a master page dynamically and
so on and so forth.
• This event can be handled by the Page_PreInit handler.
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("<b><font size=24>PreInit event Triggered<br>");
}
The ASP.NET Page life Cycle Events
Init
• Triggered after all controls have been initialized and any skin settings have
been applied for those controls.
• In this phase of the page life cycle, all the server controls of the web page
are initialized to their default values.
• Init event can be used to initialize control properties.
• The in Init event of individual controls occurs before the Init event of the
page.
• This event can be handled by Page_Init handler.
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Init event triggered <br>");
}
The ASP.NET Page life Cycle Events
InitComplete
• Is raised after all of the initialization is taken place (controls as well as
page).
• All the controls turn on tracking view-state changes.
• This event can be handled by Page_InitComplete handler.
PreLoad
• Raised after the page loads view state for itself and all controls, and after it
processes postback data that is included with the Request instance.
• It is the event where programmer can perform any processing on the page or
a control before its load event.
• This event can be handled by Page_PreLoad handler.
protected void Page_PreLoad (object sender, EventArgs e)
{
Response.Write("PreLoad event triggered <br>");
}
The ASP.NET Page life Cycle Events
Page_Load
• Load is raised for page first and recursively for all child controls.
• In the event handler associated with this event you can write code that needs
to be executed for each postback of the web page.
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Load event Triggered<br>");
}
The ASP.NET Page life Cycle Events
LoadComplete
• In LoadComplete event we can do any task which are required for all of the
other controls on the page to be loaded and validated.
• At this state controls have been created in the memory and ready for
rendering.
• This event can be handled by Page_LoadComplete handler.