DT Unit 4
DT Unit 4
DT Unit 4
• The AdRotator control is used to display flashing ads, such as banner ads and news
flashes on Web pages.
• The control is capable of displaying ads randomly, because the control refreshes the
display every time the Web page is refreshed, thereby displaying different ads for
different users.
• Also, you can assign priorities to the ads so that certain ads are displayed more
frequently than others.
• You can add the AdRotator control in an ASP.NET Web page by using the following
Syntax:
<asp:AdRotator
propertyname = propertyvalue
propertyname = propertyvalue>
</asp:AdRotator>
• Alternatively, you can use the toolbox provided with VS.NET to add the control to the
page.
• When you do so, the code is automatically generated and can be seen in the HTML
view of the ASPX file.
Properties of the AdRotator control:
Along with the properties that are inherited from the System.Web.UI.Control base class, the
AdRotator control has three additional properties:
• AdvertisementFile
• KeywordFilter
• Target
AdvertisementFile:
KeywordFilter:
Keyword Filter Functionality:
When both the "KeywordFilter" and "AdvertisementFile" properties are set, the AdRotator
control will display the image that matches the keyword specified in the "KeywordFilter"
property.
No Matching Keyword:
If the "AdvertisementFile" property points to a valid Advertisement file, but the
"KeywordFilter" property specifies a keyword that doesn't match any images in the file, the
control will render a blank image, and a trace warning will be generated.
Empty KeywordFilter:
If the "KeywordFilter" property is empty, keyword filtering will not be applied, and the
control will not use any keyword-based criteria to select an ad.
NOTE: The "KeywordFilter" property allows for context-sensitive ad display by filtering ads
based on specified keywords. It works in conjunction with the "AdvertisementFile" property
to determine which ad to display.
Target:
• The Target property specifies the name of the browser window or frame in which the
advertisement needs to be displayed.
• This parameter can also take any of the HTML frame-related keywords, such as the
following:
top: Loads the linked document into the topmost window.
_blank: Loads the linked document into a new browser window.
_self: Loads the linked document in the same window.
_parent: Loads the linked document in the parent window of the window that contains the
link.
• AdProperties:
An ‘IDictionary’ object containing all the advertisement properties set for the currently
selected ad.
• AlternateText:
A string value used to set the ALT property of the ad's image, which may display as a tooltip
in some browsers.
• ImageUrl:
A string value specifying the URL of the image displayed in the AdRotator control.
• NavigateUrl:
A string value specifying the URL of the web page to navigate to when a user clicks the
advertisement.
4. Customization:
Developers can use the "adCreated" event to select ads programmatically or modify the
rendering of selected ads from the Advertisement file.
5. Impressions Weighting:
If an Advertisement file is used, the ad selected during the "adCreated" event is based on
impressions weighting from the file.
6. Dynamic Modification:
Developers can modify the ImageUrl, NavigateUrl, and AlternateText properties within the
event handler to customize how the AdRotator control renders advertisements. This is useful
when pulling values from a database or dynamically altering ad behavior.
In summary, the "adCreated" event allows for dynamic manipulation of advertisements in
the AdRotator control, enabling customization and interaction monitoring.
• The following code describes the Ads.xml file that is used by the AdRotator control.
• The file contains two advertisements that will be dynamically shown to different
users.
• The first ad points to an image file named Saturn.gif.
• When users click this image, they are directed to the Saturn Web site.
• The second ad points to the image named Moon.jpg.
• When users click this image, they are directed to the Moon Web site.
CODE VIEW:
<Advertisements>
<Ad>
<ImageUrl> saturn.gif </ImageUrl>
<NavigateUrl> http://www.saturnrings.com/</NavigateUrl>
<AlternateText> Saturn Rings Web Site </AlternateText>
<Impressions>1</Impressions>
<Keyword>Saturn</Keyword>
</Ad>
<Ad>
<ImageUrl> Moon.jpg</ImageUrl>
<NavigateUrl> http://www.moon.com</NavigateUrl>
<AlternateText> Moon Explorers Web Site</AlternateText>
<Impressions>1</Impressions>
<Keyword>Moon</Keyword>
</Ad>
</Advertisements>
Output:
USING THE CALENDAR CONTROL:
The Calendar control is used to display a one-month calendar.
Users can use this control to view dates or select a specific day, week, or month.
The following is the syntax to add the Calendar control:
<asp:Calendar id="Calendar1" runat="server"
propertyname = propertyvalue
propertyname = propertyvalue />
Properties of Calendar Control:
1. CellPadding: Specifies space between cell contents and borders.
2. CellSpacing: Specifies space between cells.
3. DayNameFormat: Defines the format of day names.
4. FirstDayOfWeek: Sets the starting day of the week.
5. ShowNextPrevMonth: Determines if next and previous month hyperlinks are shown.
6. NextMonthText: HTML text for the "Next Month" hyperlink.
7. NextPrevFormat: Defines the format of next and previous month hyperlinks.
8. PrevMonthText: HTML text for the "Previous Month" hyperlink.
9. SelectedDate: Represents the selected date.
10. SelectedDates: A collection of highlighted dates.
11. SelectionMode: Specifies whether users can select a day, week, or month.
12. SelectMonthText: HTML text for month selection.
13. SelectWeekText: HTML text for week selection.
14. ShowDayHeader: Specifies if day names are displayed.
15. ShowGridLines: Controls display of gridlines around calendar days.
16. TitleFormat: Defines the format of the month name in the title bar.
17. TodaysDate: Represents the current date.
18. VisibleDate: Specifies the displayed month in the calendar.
STYLE OBJECTS:
1. DayHeaderStyle: Sets the appearance of the days of the current month.
2. DayStyle: Sets the appearance of the row above the calendar where the day names
appear.
3. NextPrevStyle: Sets the appearance of the sections at the left and right ends of the
title bar.
4. OtherMonthDayStyle: Sets the appearance of the days that are not in the displayed
month.
5. SelectedDayStyle: Sets the appearance of the day selected by the user.
6. SelectorStyle: Defines style properties for the week and month selector.
7. TitleStyle: Sets the appearance of the title bar at the top of the calendar, including
the month name and navigation links. If NextPrevStyle is set, it overrides the extreme
ends of the title bar.
8. TodayDayStyle: Sets the appearance of the current date.
9. WeekendDayStyle: Sets the appearance of the weekend days.
• The Calendar control supports certain events that make the control interactive on the Web
page.
• The supported events include the:
✓ DayRender
✓ SelectionChanged, and
✓ VisibleMonthChanged events
DayRender event:
• The DayRender event in ASP.NET is generated when a day cell in a calendar control is being
rendered.
• It allows developers to modify the format and content of a specific day cell before it's
displayed.
• Event Handler Syntax:
OnDayRender(sender as Object, e as DayRenderEventArgs)
DayRenderEventArgs Properties:
1. Cell: Represents a TableCell object that corresponds to the table cell where the day is being
rendered. TableCell properties include RowSpan, ColumnSpan, HorizontalAlign, VerticalAlign,
and Wrap, which control cell alignment and wrapping.
2. Day: Refers to a CalendarDay object representing the day being rendered.
DayNumberText: A string containing the day's numeric value (e.g., "15" for July 15, 2000).
IsOtherMonth: A Boolean indicating whether the rendered day is in the currently displayed month.
Developers can use the DayRender event and these properties to customize the appearance and
behavior of individual day cells within the calendar control based on specific criteria or conditions.
SelectionChanged event:
• The SelectionChanged event is generated when a user selects a day, week, or month by
clicking the Calendar control.
• You can handle this event to validate against business logic the date selected by users.
• The event handler for this event is OnSelectionChanged and has the following syntax:
• The sender parameter points to the control that generated this event, and any eventspecific
values are stored in the EventArgs object.
MonthChanged event:
• The MonthChanged event is generated when a user clicks the next or previous month
navigation controls on the title heading of the Calendar control.
• The event handler for this event is OnVisibleMonthChanged and has the following syntax:
➢ NewDate: Is a DateTime object that represents the new month that is selected.
➢ PreviousDate: Is a DateTime object that represents the previous month selected.
The following code uses the Calendar control to render a calendar in the client browsers:
DESIGN VIEW:
CODE VIEW:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
Calendar1.Visible = true;
TextBox1.Text = Calendar1.SelectedDate.ToString("dd-MM-yyyy");
Calendar1.Visible = false;
OUTPUT:
Using the TreeView Control:
• The TreeView control is used to display hierarchical data in a Windows Explorer-style format,
allowing items to be expanded and collapsed.
• It provides a rich user experience, particularly in modern browsers with DHTML (Dynamic
HTML) support.
Separate Installation:
Unlike standard ASP.NET controls, the TreeView and similar controls discussed in the following
sections are not part of the ASP.NET Framework by default. They need to be installed separately.
Importing Assemblies:
To use these controls in an ASP.NET page, you must explicitly import the assemblies that contain
them using the <%@import> and <%@Register> directives. These directives specify the namespaces
and assemblies containing the controls.
TreeView Structure:
The TreeView control is structured using elements like TreeView, TreeNode, and TreeNodeType.
Hierarchy:
The TreeView control consists of nodes, and some nodes can contain child nodes. Nodes with child
nodes are called parent nodes.
This control is particularly useful for presenting data with hierarchical relationships in a user-friendly
manner.
In addition to the properties that are inherited from the System.Web.UI.Control base class, the
TreeView control has properties that can be used to control the behavior of the control.
AutoPostBack: A Boolean property indicating whether the control posts back to the server on each
client request.
AutoSelect: A Boolean property indicating whether a tree node can be selected by simply pointing
the mouse to the node, without having to click it.
HoverStyle: Sets a style (e.g., font family, font size, color) for the elements in the tree when the
mouse hovers over them.
Indent: Specifies the number of pixels by which child nodes should be indented.
ShowLines: A Boolean property indicating whether lines are used to connect the nodes in the tree.
These properties allow developers to customize the appearance and behavior of the TreeView
control in their web applications, enhancing user interactivity and visual presentation.
• Collapse
• Expand
• SelectedIndexChanged.
Collapse event:
• The Collapse event is triggered when a user clicks to collapse a tree node in a TreeView
control.
• The event handler for the Collapse event is named OnCollapse.
• It has two arguments:
➢ sender (Object representing the event source) and
➢ e (TreeViewClickEventArgs providing information about the collapsed node).
Expand event:
• The Expand event is generated when a user clicks a tree node to expand it.
• You can trap this event to control the formatting and decide the contents of a particular node
and its child nodes.
• The event handler for this event is OnExpand and has the following syntax:
• OnExpand(sender As Object, e As TreeViewClickEventArgs)
• The second parameter is an object of the TreeViewClickEventArgs class and contains the data
pertaining to the Expand event.
SelectedIndexChanged event:
TreeViewSelectEventArgs properties include NewNode (representing the newly selected node) and
OldNode (representing the previously selected node).
<html>
// Append node index to the label control when a tree node is collapsed
// Append node index to the label control when a tree node is expanded
// Append node index to the label control when a new node is selected in the tree
</script>
<head>
</head>
<body>
<form runat="server">
<!-- Render TreeView control, set up event handlers for collapse, expand, and selectedindexchanged
events -->
SelectedStyle="font-face:Verdana;font-size:12pt;color:white;"
OnCollapse="OnCollapse"
OnExpand="OnExpand"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<mytree:treenode text="Asia">
</mytree:treenode>
<mytree:treenode text="Africa">
</mytree:treenode>
</mytree:treenode>
</mytree:TreeView>
<br />
</form>
</body>
</html>
Sample Output:
Using the Toolbar Control:
<tagprefix:Toolbar ..>
<tagprefix:ToolbarSeparator />
</tagprefix:Toolbar>
• As you can see in the preceding syntax, the Toolbar control is a container control that
contains elements to define a toolbar.
• These elements are described as follows:
In addition to the properties that are inherited from the System.Web.UI.Control base class, the
Toolbar control has additional properties.
AutoPostBack: Determines if the control triggers automatic postbacks to the server. "True" means it
posts back with client requests; "false" means it doesn't.
DefaultStyle: Sets the default visual style for the toolbar, applied to all elements by default.
HoverStyle: Defines the style for when the mouse hovers over toolbar items, providing visual
feedback.
SelectedStyle: Specifies the style applied to toolbar items when they are selected or activated.
Orientation: Determines the layout of the toolbar, either horizontal or vertical, affecting how items
are displayed.
Note: Every button on the toolbar has three states — Default, Selected, and Hover. You can define
appropriate CSS styles for each of the three states. Then, ASP.NET will apply the appropriate style
when rendering the button.
Events of the Toolbox Control:
• ButtonClick
• CheckChange events
ButtonClick Event:
CheckChange Event:
Rendering a toolbar:
SOURCE VIEW:
<html>
{
string sMsg;
ToolbarButton tb;
tb = (ToolbarButton)sender;
lblMessage.Text = sMsg;
</script>
<head>
</head>
<body>
<!-- Display toolbar control, set up event handler for ButtonClick Event -->
<form runat="server">
OnButtonClick="OnButtonClick">
<ie:ToolbarSeparator />
<ie:ToolbarSeparator />
</ie:Toolbar>
</form>
</body>
</html>
Output:
Overview:
• Visual Studio .NET offers standard controls (intrinsic controls) for creating user interfaces in
applications.
• Standard controls provide a wide range of functionality for designing user-friendly interfaces.
• Custom controls are available for cases where standard controls do not meet specific
requirements.
• Custom controls enable reusability, making them valuable for common elements across
multiple forms.
An example could be creating a custom calculator control to use across multiple web forms in a web
application.
In Visual Studio .NET, you can create various types of custom controls, including:
User control: A Web Forms page that can function as a control on other Web Forms pages, allowing
reusability.
Composite control: A combination of existing controls treated as a single control, which can be
created in any .NET programming language and used in ASP.NET pages.
• Custom controls serve to extend the functionality of existing Web Form controls to meet
specific requirements.
• You can inherit from existing controls, overriding properties, methods, or events to
customize them to your needs.
• Alternatively, you can develop a custom control by inheriting directly from one of the Control
base classes if none of the existing Web Forms controls fulfill your requirements.
• Using existing classes as a framework for custom controls allows you to focus on
programming the specific features you want to incorporate.
Before creating custom controls, it's essential to understand the base classes used by these controls.
• Classes used for Web Forms controls Each Web Forms control is a class that inherits from the
System.Web.UI. Control class directly or indirectly.
• We examine the System.Web.UI.Control class and its inherited classes that are used to create
a Web Forms control.
• The System.Web.UI.Control class defines all the properties, events, and methods that are
common to all the Web Forms controls.
• You need to inherit your control from this class in the following cases:
➢ When your control does not have a user interface
➢ When your control includes other controls that render their own user interface
Control Properties:
"ID":
"Parent":
"Visible":
Control Methods:
"Dispose":
"Equals":
Equals method is used to check whether an object is the same as the current object, and it is
overloaded.
"FindControl":
FindControl method is used to search a container for a specified server control, and it is overloaded.
"ToString":
ToString method is used to return the string representation of the current object.
Control events:
"Init":
Init event is fired when a control is initialized, which is the first step when a page needs to be
displayed in a browser.
"Load":
Load event is fired when the control is loaded on a page.
"Unload":
System.Web.UI.WebControls.WebControl class:
System.Web.UI.HtmlControls.HtmlControl class:
INamingContainer interface:
• Used for controls that provide data binding, are templated controls, or route events to child
controls.
• Creates a unique namespace and ensures unique IDs for child controls when implemented.
IPostBackDataHandler interface:
Implemented by controls that need to update their state or raise events on the server after
examining postback data.
IPostBackEventHandler interface:
• Create a user control by designing a Web Forms page and changing its extension to .ascx.
• Register the user control on a Web Forms page using the @ Register directive.
• Add the user control to the page using the specified tag name.
Implement the Render method if you want to control rendering without instantiating child controls.
• Controls have events associated with them, and events can be raised and handled on the
server.
• Custom events can be associated with controls.
• Event handlers can be added in the containing Web Forms page or the user control.
• Precautions may be necessary when using event handlers in user controls.
An example of adding a button and event handler in a user control is provided.
Validation controls in ASP.NET simplify page validation and reduce the need for extensive code.
These controls were developed to address common validation needs observed on various websites.
They can automatically detect the client's browser version and render the appropriate HTML for
client-side validation.
Basic properties like font, fore color, and back color are shared among these controls.
RequiredFieldValidator:
• Use the RequiredFieldValidator control when you need to ensure a value is provided for an
input element on a web page.
• The RequiredFieldValidator control checks if the associated input control has a different
value from its initial state.
• Properties of the RequiredFieldValidator control include ID, ControlToValidate, Display,
ErrorMessage, and RunAt.
• The ControlToValidate property specifies the control to validate, and the Display property
determines how the error message is shown (Dynamic, Static, None).
CompareValidator:
• Use the CompareValidator control to ensure a value matches a specified value or follows a
specific type.
• Common properties of the CompareValidator control include ID, ControlToValidate,
ControlToCompare, ValueToCompare, Display, ErrorMessage, and RunAt.
RangeValidator:
The RangeValidator control is used to check if a value falls within a specified range, with properties
such as ID, ControlToValidate, MaximumValue, MinimumValue, Display, ErrorMessage, and Type.
RegularExpressionValidator:
The RegularExpressionValidator control checks if a value matches a regular expression pattern, with
properties ID, ControlToValidate, ValidationExpression, Display, ErrorMessage, and RunAt.
CustomValidator:
The CustomValidator control allows custom validation logic on the client or server, with properties
like ID, ControlToValidate, ClientValidationFunction, OnServerValidate, Display, ErrorMessage, and
RunAt.
ValidationSummary:
The ValidationSummary control collects and displays validation error messages from other controls in
a structured format, with properties like ID, DisplayMode, HeaderText, ShowMessageBox,
ShowSummary, and RunAt.