Professional Documents
Culture Documents
Unit 2
Unit 2
Web Server Controls : Button, Image Button, Link Button, Textbox, Hyperlink, ImageMap control
(Creating Hotspots),CheckBox and RadioButton, CheckBoxList, RadioButtonList, ListBox, DropdownList
Rich Controls:Calendar, Adrotator control (showing advertisement from XML file and Database),
FileUpload control
Using Navigation Controls : TreeView, SiteMapPath, Menu, Creating sitemap file for navigation
Designing Master page
1. Button
The Button is an asp.net web server control. This control is used to perform events. It is also used to
submit client request to the server. To create Button either we can write code or use the drag and
drop facility of visual studio IDE. We can display a simple push button on a web page by adding
button control on asp.net web page.
1. Simple Push Button: Simple Push Button displays text on a button control.
2. Link Button: Link button displays text that looks like a link or hyperlink.
3. Image Button: Image Button displays an image on a button control.
These entire three buttons has main two events Click and Command events. By default in asp.net
button has click events.
This is a server side control and asp provides own tag to create it. The example is given below.
Server renders it as the HTML control and produces the following code to the browser.
1
This control has its own properties that are tabled below.
Property Description
2. ImageButton
Use of ImageButton control is to display an image that responds to mouse clicks. Both the Click and
Command events are raised when the ImageButton control is clicked. You can use the OnCommand
event handler to make the ImageButton control behave like a Command button.
This is a server side control and asp provides own tag to create it.
<asp:ImageButton ID=”ImageButton1″ runat=”server” />
This control has its own properties that are tabled below.
Property Description
3. Link Button
It is a server web control that acts as a hyperlink. It is used to display a hyperlink-style button control
on the web page. ASP.NET provides a tag to create LinkButton and has following syntax. The Link
Button code is like:
2
Property Description
4. Textbox
Textbox control is most usable web server control in asp.net. TextBox control is a rectangular box
which is used to take user to input. In simple word the TextBox is a place where user can input some
text on asp.net web form. To use TextBox on page we can write code or just drag and drop from
toolbox. This is server side control, asp provides own tag to create it. The example is given below.
Properties Description
5. Hyperlink
HyperLink is an asp.net web server control. We can display a hyperlink on a web page by adding a
hyplerlink control on asp.net web page. HyperLink control used to navigate user to anther page or on
the same page. It responds to a click event. The main property of hyperlink control is NavigateUrl.
The NavigateUrl property of hyperlink control store the address of destination page. The HyperLink
control display both as text and as image by specifying Text or ImageUrl property of hyperlink
control. If we set any image in ImageUrl property of hyperlink control then the hyperlink display
3
image on it. If we set any text value in Text Property of hyperlink control then the hyperlink control
display as text link.
This is a server side control and ASP.NET provides own tag to create it. The example is given
below.
Server renders it as the HTML control and produces the following code to the browser.
<a id="HyperLink1" href="www.javatpoint.com">JavaTpoint</a>
This control has its own properties that are tabled below.
Property Description
6. ImageMap Control
The ImageMap control in ASP.NET 2.0 and onward versions can be used to create an image that
contains defined hot spot regions. When a user clicks a hot spot region, the control can either
generate a post back to the server or navigate to a specified URL.
There are three kinds of hot spot regions defined in ImageMap control.
RectangleHotSpot
CircleHotSpot
PolygonHotSpot
The RectangleHotSpot defines rectangular hot spot regions. The CircleHotSpotdefines circle-shaped
ones and the PolygonHotSpot is used for irregularly shaped hot spot area.
4
This control has its own properties that are tabled below.
Property Description
HotSpotMode PostBack/Navigate .... When Navigate, the user is navigated to a different URL. In case of
PostBack, the page is posted back to the server.
OnClick Attach a server side event that fires after clicking on image when HostSpotMode is
PostBack.
PostBackValue You can access it in the server side click event through ImageMapEventArgs. (eg.
e.PostBackValue)
7. CheckBox
It is used to get multiple inputs from the user. It allows user to select choices from the set of choices.
It takes user input in yes or no format. It is useful when we want multiple choices from the user.
This is a server side control and ASP.NET provides own tag to create it. The example is given
below.
Server renders it as the HTML control and produces the following code to the browser.
This control has its own properties that are tabled below.
Property Description
5
8. Radio Button
It is an input control which is used to takes input from the user. It allows user to select a choice from the
group of choices.
This is a server side control and ASP.NET provides own tag to create it. The example is given below.
Property Description
OnCheckedChanged Fires when Radio button selection changes. This works only
if AutoPostBack property is set to true.
GroupName It is used a group a set of radion buttons so only one of them can be
selected at a time.
9. ChechkboxList
CheckBoxList control is a single control that groups a collection of checkable list items,
Following are some important properties that are very useful.
Property Description
DataTextField Name of the data source field to supply the text of the items.
DataValueField Name of the data source field to supply the value of the items.
6
DataSource The datasource that populates the items in the checkboxlist box.
AutoPostBack true/false. If true, the form is automatically posted back to the server when user
click any of the checkbox. It will also fire OnSelectedIndexChanged method.
AppendDataBoundItems true/false. If true, the statically added item (added from .aspx page) is maintained
when adding items dynamically (from code behind file) or items are cleared.
OnSelectedIndexChanged Method name that fires when user click any of the checkbox in the list. (Fires
only when AutoPostBack=true.)
RepeatLayout table/flow. Gets or Sets the layout of the chekboxes when rendered to the page.
RepeatColumns Gets or Sets the no. of columns to display when the control is rendered.
RepeatDirection Horizontal/Vertical. Gets or Sets the the value to indicate whether the control
will be rendered horizontally or vertically.
10. RadioButtonList
Note: RadioButtonList controls supports the same set of properties as the CheckBoxList control
does.
11. ListBox
ListBox control is an asp.net web server control. ListBox control used to store the multiple items and
allow user to select multiple item from listbox control. In a ListBox control there is a SelectionMode
property to change the mode of section from single to multiple. By default listbox control selection
mode is single if you want to select multiple items from listbox, then just change the SelectionMode
property to multiple.
Property Description
SelectionMode Single or Multiple. If multiple, it allows user to select multiple items from the list
by holding Ctrl or Shift key.
SelectedValue Get the value of the Selected item from the dropdown box.
SelectedIndex Gets or Sets the index of the selected item in the dropdown box.
DataTextField Name of the data source field to supply the text of the items.
DataValueField Name of the data source field to supply the value of the items.
7
DataSourceID ID of the datasource component to provide data.
DataSource The datasource that populates the items in the listbox box.
AutoPostBack true or false. If true, the form is automatically posted back to the server when
user changes the dropdown list selection.
AppendDataBoundItems true or false. If true, the statically added item (added from .aspx page) is
maintained when adding items dynamically (from code behind file) or items are
cleared.
OnSelectedIndexChanged Method name that fires when user changes the selection of the dropdown box.
(Fires only when AutoPostBack=true.)
12. DropDownList
The DropDownList control is asp.net web server control. We can use dropdownlist control for hold
group of items. The dropdownlist control is used to store the multiple items and allow user to select
only one item from it.The dropdownlist control is also known as combo box control. In dropdownlist
control we can store multiple items but we can select only one item at a time, that’s why it is also known
as Single Row Selection Box.
Property Description
SelectedValue Get the value of the Selected item from the dropdown box.
SelectedIndex Gets or Sets the index of the selected item in the dropdown box.
DataTextField Name of the data source field to supply the text of the items.
DataValueField Name of the data source field to supply the value of the items.
DataSource The datasource that populates the items in the listbox box.
AutoPostBack true or false. If true, the form is automatically posted back to the server when
user changes the dropdown list selection. It will also
fire OnSelectedIndexChanged method.
AppendDataBoundItems true or false. If true, the statically added item (added from .aspx page) is
maintained when adding items dynamically (from code behind file) or items are
cleared.
OnSelectedIndexChanged Method name that fires when user changes the selection of the dropdown box.
(Fires only when AutoPostBack=true.)
8
Rich Controls
1. AdRotator
2. FileUpload
3. Calendar
1. AdRotator
Element Description
<ImageUrl> Optional. The path to the image file
<NavigateUrl> Optional. The URL to link to if the user clicks the ad
<AlternateText> Optional. An alternate text for the image
<Keyword> Optional. A category for the ad
<Impressions> Optional. The display rates in percent of the hits
Properties of Control
Property Description
AdvertisementFile Specifies the path to the XML file that contains ad information
AlternateTextField Specifies a data field to be used instead of the Alt text for an ad
Specifies a data field to be used instead of the ImageURL attribute for
ImageUrlField
an ad
KeywordFilter Specifies a filter to limit ads after categories
Specifies a data field to be used instead of the NavigateUrl attribute
NavigateUrlField
for an ad
runat Specifies that the control is a server control. Must be set to "server"
Target Specifies where to open the URL
9
<AlternateText>Books Available</AlternateText>
<Impressions>50</Impressions>
<Keyword>elearning</Keyword>
</Ad>
</Advertisements>
2.FileUpload
The FileUpload control allows the user to browse for and select the file to be uploaded.
It provides a Browse button and a text box for entering the filename.
Properties Description
Designing:
10
Output:
3. Calendar
It is powerful Web server control that you can use to add calendar to the web page.
The calendar control is a functionally rich web control, which provides the following capabilities:
The Calendar is complex, powerful Web server control that you can use to add calendar
feature to your web page. We can use calendar control display any date between 0 A.D. and
9999A.D.
Property Description
Day Allow selection of a single date.
DayWeek Allows the selection of a single date or a complete week.
DayWeekMonth Allow selection of single date, complete week or complete month.
None Doesn't allow you to select any date.
You can also set the properties for the calendar either by selecting the Auto Format property
by right clicking on the Calendar control or by manually setting them one by one.
Grouping Controls
1. Panel
2. PlaceHolder
1) Panel
The Panel control works as a container for other controls on the page. It controls the appearance and
visibility of the controls it contains.
If we want to Hide or visible more then one Control as same time, so we can place them to in
Panelcontrol and Just Visible or Hide Panel control the all the control will be Visible or Hide
according to Panel Control.
The Panel control is derived from the WebControl class. Hence it inherits all the properties, methods
and events of the same. It does not have any method or event of its own. However it has the
following properties of its own:
Properties Description
DefaultButton Gets or sets the identifier for the default button that is contained
in the Panel control.
Let us start with a simple scrollable panel of specific height and width and a border style. The
ScrollBars property is set to both the scrollbars, hence both the scrollbars are rendered.
The source file has the following code for the panel tag:
2. PlaceHolder
The PlaceHolder control is used to reserve space for controls added by code.
PlaceHolder is an asp.net web server control which used to store dynamically added web server
controls on the web page.
13
By using a PlaceHolder control we can dynamically add Label, TextBox, Button, RadioButton,
Image and many more web server controls in an asp.net web page.
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{ Image img = new Image();
img.ImageUrl = @"~/Images/sea.jpg";
img.BorderWidth = 3;
img.BorderColor = System.Drawing.Color.SaddleBrown; PlaceHolder1.Controls.Add(img);
}
</script>
Navigation Controls
1. TreeView
2. SiteMapPath
3. Menu
1. TreeView
The TreeView Web control is useful to display hierarchical data in a tree structure. A TreeView is a
collection of TreeNode objects. It provides a way to display information in a hierarchical structure by
using collapsible nodes. The top level in a treeview are root nodes that can be expanded or collapsed
if the nodes have child nodes.
The contents of the TreeView control can be specified directly in the control or bound to,
1. XML file
2. web.sitemap file
3. Database table
14
The user can expand the TreeNode by clicking the plus sign (+) button, if one is displayed next to the
TreeNode, or you can expand the TreeNode by calling the TreeNode.Expand method. You can also
navigate through tree views with various properties: FirstNode, LastNode, NextNode, PrevNode,
NextVisibleNode, PrevVisibleNode.
The fullpath method of treeview control provides the path from root node to the selected node.
Properties
BackgroundImage Gets or set the background image for the TreeView control.
BackgroundImageLayout Gets or sets the layout of the background image for the TreeView
control.
BorderStyle Gets or sets the border style of the tree view control.
ForeColor The current foreground color for this control, which is the color the
control uses to draw its text.
FullRowSelect Gets or sets a value indicating whether the selection highlight spans the
width of the tree view control.
HideSelection Gets or sets a value indicating whether the selected tree node remains
highlighted even when the tree view has lost the focus.
HotTracking Gets or sets a value indicating whether a tree node label takes on the
appearance of a hyperlink as the mouse pointer passes over it.
ImageIndex Gets or sets the image-list index value of the default image that is
displayed by the tree nodes.
ImageList Gets or sets the ImageList that contains the Image objects used by the
tree nodes.
Indent Gets or sets the distance to indent each of the child tree node levels.
ItemHeight Gets or sets the height of each tree node in the tree view control.
LabelEdit Gets or sets a value indicating whether the label text of the tree nodes
can be edited.
LineColor Gets or sets the color of the lines connecting the nodes of the
TreeView control.
Nodes Gets the collection of tree nodes that are assigned to the tree view
control.
15
Padding Gets or sets the spacing between the TreeView control's contents and
its edges.
PathSeparator Gets or sets the delimiter string that the tree node path uses.
Scrollable Gets or sets a value indicating whether the tree view control displays
scroll bars when they are needed.
SelectedImageIndex Gets or sets the image list index value of the image that is displayed
when a tree node is selected.
SelectedNode Gets or sets the tree node that is currently selected in the tree view
control.
ShowLines Gets or sets a value indicating whether lines are drawn between tree
nodes in the tree view control.
ShowNodeToolTips Gets or sets a value indicating ToolTips are shown when the mouse
pointer hovers over a TreeNode.
ShowPlusMinus Gets or sets a value indicating whether plus-sign (+) and minus-sign (-)
buttons are displayed next to tree nodes that contain child tree nodes.
ShowRootLines Gets or sets a value indicating whether lines are drawn between the
tree nodes that are at the root of the tree view.
Sorted Gets or sets a value indicating whether the tree nodes in the tree view
are sorted.
Text Gets or sets the text of the TreeView.
TopNode Gets or sets the first fully-visible tree node in the tree view control.
TreeViewNodeSorter Gets or sets the implementation of IComparer to perform a custom sort
of the TreeView nodes.
VisibleCount Gets the number of tree nodes that can be fully visible in the tree view
control.
Methods
BeginUpdate Disables any redrawing of the tree view.
CollapseAll Collapses all the tree nodes.
EndUpdate Enables the redrawing of the tree view.
ExpandAll Expands all the tree nodes.
GetNodeAt Retrieves the tree node that is at the specified location.
GetNodeCount Retrieves the number of tree nodes, optionally including those in all subtrees,
assigned to the tree view control.
HitTest Provides node information, given a point.
Sort Sorts the items in TreeView control.
16
Introduction to SiteMapPath
If you anytime traveled to unknown areas you know the importance of maps. They help you to travel
easily making your journey pleasurable. The same holds true for web sites. The visitors coming to
your web sites should be presented with simple yet flexible navigation structure so that they can
travel to various parts of your web site easily. ASP.NET provides a feature called SiteMap that help
you achieve this goal.
The SiteMap
A site map is an XML file that details the overall navigational layout of your web site. You can then
consume this site map file as per your requirement. The site map file must have extension .sitemap.
The XML file must contain a <siteMap> tag surrounding the content
The <siteMap> tag can only have one <siteMapNode> child node (the "home" page)
Each <siteMapNode> can have several child nodes (web pages)
Each <siteMapNode> has attributes defining page title and URL
Note: The sitemap file must be placed in the root directory of the web and the URL attributes must be
relative to the root directory.
SiteMapPath control
The SiteMapPath control displays the trail (navigation path) to the current page. The path acts as
clickable links to previous pages. The SiteMapPath control uses the web.sitemap file by default.
Code Example:
<form runat="server">
<asp:SiteMapPath runat="server" />
</form>
17
Property Description
ShowToolTips Set this to False if you don’t want the description text to appear when
the user hovers over a part of the site map path.
ParentLevelsDisplayed This sets the maximum number of levels above the current page that
will be shown at once. By default, this setting is -1, which means all
levels will be shown
RenderCurrentNodeAsL If True, the portion of the page that indicates the current page is turned
ink into a clickable link. By default, this is False because the user is already
at the current page.
PathDirection You have two choices: RootToCurrent (the default) and CurrentToRoot
(which reverses the order of levels in the path).
PathSeparator This indicates the characters that will be placed between each level in
the path. The default is the greater-than symbol (>). Another common
path separator is the colon (:).
NodeStyle NodeTemplate All parts of the path except the root and
current node
CurrentNodeStyle CurrentNodeTemplate The node representing the current page.
2. Menu Control
The Menu Control can be used to display hierarchical data. It is very easy to navigate through the Menu
Control when there are many options in the hierarchy. Menus occupy less space in the web page. Menus
can be expanded at various levels or collapsed as required by the user.The Menu control can be bound to
a SiteMapDataSource to display the information available in the Web.sitemap file. The DataSourceID
18
property of the Menu Control is set to the ID of the SiteMapDataSource control. Below is the tag for the
Menu Control.
Menu Styles
The Menu supports defining different menu styles for different menu levels.
Following Figure shows the menu with StaticDisplayLevels set to 2 (and some styles applied through the
Auto Format link).
19
Master Page
Master pages provide templates for other pages on your web site. Master pages allow you to create a
consistent look and behaviour for all the pages (or group of pages) in your web application.
A master page provides a template for other pages, with shared layout and functionality. The master page
defines placeholders for the content, which can be overridden by content pages. The output result is a
combination of the master page and the content page.
<html>
<body>
<h1>Standard Header From Masterpage</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
The content page above demonstrates how .NET controls can be inserted into the content page just like an
into an ordinary page.
Terminology
Let us look at the basic terminology that needs to be understood before jumping into master pages:
Masterpage: Gives us a way to create common set of UI elements that are required on multiple pages of our
website.
ContentPage: The ASP.NET web page that will use master page to have the common UI elements displayed
on rendering itself.
ContentPlaceHolder: A control that should be added on the MasterPage which will reserve the area for the
content pages to render their contents.
20
Creating a MasterPage
21
Now let's look at the stuff that is important. When we look at the MasterPage, we will see
that masterpage has a ContentPlaceHolder control. All the code that is common for the content pages is
outside the ContentPlaceHolder control (in our case, a simple menubar).
Designing
22