ASP.NET Web Controls Adrotator and its events

3:22 AM Posted by Isha

In asp.net adrotator webcontrol is a control used to control the ads n the web page.

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:
  1. AdvertisementFile
  2. KeywordFilter
  3. Target

AdvertisementFile

The AdvertisementFile property in asp.net represents the path to an Advertisement file. The Advertisement file is a well-formed XML document that contains information about the image to be displayed for advertisement and the page to which a user is redirected when the user clicks the banner or image. The following is the syntax of the Advertisement file:

URL of the image to display
  1. URL of the page to navigate to
  2. Text to be displayed as ToolTip
  3. keyword used to filter
  4. relative weighting of ad

Note The Advertisement file must be a well-formed XML document, as the AdvertisementFile property of the AdRotator control needs to be set to an XML file.

The following are the different elements used in the Advertisement file:

ImageUrl: Specifies an absolute or relative URL to an image file that presents the image for the advertisement. This element refers to the image that will be rendered in a browser.

NavigateUrl: Specifies the URL of a page to navigate to, if a user clicks the advertisement image. If this parameter is not set, the ad is not "live." Although this parameter is optional, it must be specified, because the ad must direct clients to a target URL when it is clicked.

AlternateText: Is an optional parameter that specifies some alternative text that will be displayed if the image specified in the ImageUrl parameter is not accessible. In some browsers, the AlternateText parameter appears as a ToolTip for the ad.

Keyword: Is an optional parameter that specifies categories, such as computers, books, and magazines that can be used to filter for specific ads.

Impressions: Is an optional parameter that provides a number that indicates the weight of the ad in the schedule of rotation relative to the other ads in the file. The larger the number, the more often the ad will be displayed.

Keyword Filter

The KeywordFilter property of active server pages dot net specifies a category filter to be passed to the source of the advertisement. A keyword filter allows the AdRotator control to display ads that match a given keyword. This enables the AdRotator control to display more context-sensitive ads, where the context is specified in the ASPX page containing the AdRotator control. When you use a keyword filter, three conditions arise:

Both the KeywordFilter and AdvertisementFile properties are set. In such a case, the AdRotator control renders the image that matches the keyword specified.

The AdvertisementFile property points to a valid Advertisement file, and the KeywordFilter property specifies a keyword that matches no images. In such a case, the control renders a blank
image, and a trace warning is generated.

The KeywordFilter property is empty. In such a case, keyword filtering will not be used to select an ad.

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:
  1. top: Loads the linked document into the topmost window.
  2. blank: Loads the linked document into a new browser window.
  3. self: Loads the linked document in the same window.
  4. parent: Loads the linked document in the parent window of the window that contains the link.
  5. After looking at the properties, let's understand the events associated with the AdRotator
  6. control.

Events of the AdRotator control

The AdRotator control supports the adCreated event that you can handle to monitor the activities of a user or a session. The adCreated event is generated with every round trip to the server, after the AdRotator control is created but before the page is rendered in the browser.

The event handler for the adCreated event is OnAdCreated and has the following syntax:

OnAdCreated (sender as Object, e as AdCreatedEventArgs)

The event handler takes two parameters. The first parameter represents the object that raises the event. The second parameter represents the AdCreatedEventArgs object that contains the data related to this event. The AdCreatedEventArgs object has a set of properties that provide information specific to the AdCreated event:

AdProperties: Is an IDictionary type object that provides all the advertisement properties that have been set for the currently selected advertisement.

AlternateText: Is a String type value that sets the ALT property of the image that is sent to the browser. In some browsers, this text is displayed as a ToolTip when the mouse cursor hovers over the image.

ImageUrl: Is a String value that sets the URL of the image that is displayed in the AdRotator control.

NavigateUrl: Is a String type value that specifies the URL of the Web page to navigate to when a user clicks the advertisement.

The OnAdCreated event handler can be used to select ads in a local code or to modify the rendering of an ad selected from the Advertisement file. If an advertisement file is set, the parameters of the AdCreated event handler are set to the selected ad when the event is generated. The source image that is specified by the Advertisement file is sized by the browser to the dimensions of the AdRotator control, regardless of the image's actual size. The ad is selected based on impressions weighting from the file.

If the values are not set in the Advertisement file, the developer can modify the values in the ImageUrl, NavigateUrl, and AlternateText properties to modify the rendering of the AdRotator control. A very common use of this is when developers need to populate the event arguments with values pulled from a database.

Rendering ads to client browsers using AdRotator

The following code uses the AdRotator server-side control to render ads to the client browsers. The AdRotator control uses an Advertisement file named Ads.xml.

<%@ Page Language="VB" %>

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.

Related Posts:

asp.net table controls and events
forms in asp.net part one two and three
asp.net introduction and application creation

ASP.NET Table Control and Evens

11:01 PM Posted by Isha

In ASP.NET table control is used to produce a a table in a web page and we can add some events to it as shown here. We are going to use some web controls in active server pages dot net.

A table is used to display information in a tabular format. A table consists of rows and columns. The intersection of a row and a column is called a cell. You can add a table to a Web Forms page by using the Table control in asp.net. This control displays information statically by setting the rows and columns at design time. Also, you can program the Table control to display information dynamically at run time.

You can add rows at design time by setting the Rows property, which represents a collection of TableRow objects; a TableRow object represents a row in the table. You can add cells to a table row by setting the Cells property of the TableRow object. The Cells property represents a collection of TableCell objects; a TableCell object represents a cell in a table row.

Thus, to set rows and columns of a table at the design time, you first add the Table control to the form. Then, set the Rows property of the Table control to add TableRow objects. Finally, set the Cells property of the TableRow objects to add TableCells objects.

The steps are given as follows:

1. Display the Properties window of the Table control.

2. Click the ellipsis button for the Rows property of the Table control.

3. In the TableRow Collection Editor dialog box, click Add to create a new row. A new row is created and its properties are displayed in the Properties pane of the dialog box.

4. Verify that the row is selected in the Members list, and then click the ellipsis button for the Cells property to add a cell for the row.

5. In the TableCell Collection Editor dialog box, click Add to create a new cell.

You can also add the rows and columns (cells) to a table at run time programmatically.

To do so, you first need to create the TableRow and TableCell objects:

Dim Table1 as New Table()
Dim TableRowObj As New TableRow()
Dim TableCellObj As New TableCell()
Then, you need to add the TableCell object to the TableRow object:
TableRowObj.Cells.Add(TableCellObj)

Finally, you need to add the TableRow object to the Table control.

Image control

The Image control allows users to display images in a Web Forms page and manage them at design time or at run time. After you add an Image control to a Web Forms page, you need to set the image to be displayed in the control. You can do so by using the ImageUrl property.

Button, LinkButton, and ImageButton controls

The Button control on a Web Forms page is used to perform an event, such as form submit, on the server. You can create three types of server control buttons:

Button: Represents a standard button.

LinkButton: Represents a button that can act as a hyperlink in a page. However, a LinkButton control causes the page to be submitted to the server.

ImageButton: Represents a graphical button to provide a rich button appearance. You can set the ImageUrl property to point to a specific image.

Working with Events in ASP.NET

A Web Forms application provides fast, dynamic, and user-interactive Web Forms pages. When users interact with different Web controls on a page, events are raised. In the traditional client forms or client-based Web applications, the events are raised and handled on the client side. However, in Web Forms applications, the events are raised either on the client or on the server, but are always handled on the server. ASP.NET server controls support only server-side events, while HTML server controls support both server-side and client-side events.

Round trips in active server pages :

Most Web pages require processing on the server. For example, consider an Orders Web page used to receive orders on the Web. When a user enters a value for the quantity of a product to be bought, the page must check on the server to see whether or not the quantity requested is available. This type of dynamic functionality is accomplished by handling server control events.

Whenever a user interaction requires some kind of processing on the server, the Web Forms page is submitted to the server, processed, and then returned to the browser (client).This sequence is called a round trip.

Most of the user interactions with the server controls result in round trips. Because a round trip involves sending the Web Forms page to the server and then displaying the processed form in the browser, the server control events affect the response time in the form.

Therefore the number of events available in Web Forms server controls is limited, usually to Click events. The events that occur quite often, such as the OnMouseOver event, are not supported by server controls. However, some server controls support events that occur when the control's value changes.

By default, only the Click event of the Button, LinkButton, and ImageButton server controls causes the form to be submitted to the server for processing — the form is said to be posted back to the server. The Change events associated with other controls are captured and cached and do not cause the form to be submitted immediately.

When the form is posted back (as a result of a button click), all the pending events are raised and processed. No particular sequence exists for processing these Change events, such as TextChanged and CheckChanged on the server. The Click event is processed only after all the Change events are processed.

You can set the change events of server controls to result in the form post back to the server. To do so, modify the AutoPostBack property to True.

Event handlers in asp.net:

When the events are raised, you need to handle them for processing. The procedures that are executed when an event occurs are called event handlers. An event handler is associated with the corresponding event by using the WithEvents and Handles keywords. The WithEvents keyword is used to declare the control generating an event.

For example, when you declare a control, say Image1 as 'Protected WithEvents Image1 As System.Web.UI.WebControls.Image', the WithEvents keyword specifies that Image1 is an object variable used to respond to events raised by the instance assigned to the variable. The Handles keyword is used to associate the event handler with the event, which is raised by the control.

The control in turn is declared by using the WithEvents keywords.

Event handlers are automatically created when you double-click the control in Design mode of the form. For example, the following code is generated when you double-click a Button control whose ID is RegisterButton. You can then write the code in the event handler to perform the intended task.

Public Sub RegisterButton_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles RegisterButton.Click

Related posts

asp.net forms building part one two and three

ASP.NET building Forms with webcontrols part three

7:13 PM Posted by Isha

ASP.NET web forms can have web controls can be build and control at the server with the help of programming. We have previously discussed regarding active server page forms with web controls part one and asp.net forms part two and this post is in continuation of it.

Using Web Controls:

You can add ASP.NET server controls to a Web Forms page by using either of the following two features:
  1. The Web Forms section of the toolbox
  2. The ASP.NET code
You add controls from the toolbox in Design view of the Web Forms page (the ASPX file). The toolbox categorizes the different types of controls in separate tabs, such as Web Forms, HTML, Components, and Data. You can use the HTML tab to add HTML controls and use the Web Forms tab to add the ASP.NET server controls to Web Forms.

However, to make the HTML controls available for coding at the server end, these controls need to be converted to server controls. To do so, right-click the HTML control on the page and select Run As Server Control from the context menu.

While selecting between HTML and Web server controls, using Web server controls is preferred,because they provide a rich object model and are adaptable to multiple browsers depending on browser capabilities. However, HTML server controls are preferred when migrating from the existing ASP pages to ASP.NET pages, because, unlike Web server controls, HTML server controls map directly to HTML tags.

You can also add a Web control to a page by using the ASP.NET code. You can access the ASP.NET code in the HTML view of the page (ASPX file).

You can add ASP.NET server controls by using an Extensible Markup Language (XML) tag referenced as asp.

You can also programmatically add a control at run time. The following VB.NET code snippet demonstrates how to add a TextBox control at run time:

Dim TextBox1 as New TextBox()

Controls.Add(TextBox1)

Every control has specific properties and methods. You can set control properties to modify the appearance or behavior of controls. For example, you can set the font, color, and size of a control. You can use the control methods to perform a specific task, such as moving a control. You can set control properties at design times by using the Properties window or at run time by using the code. Every control has a property called ID that is used for the unique identification of the control. You can set the property of a control at run time by using the following syntax:

ControlID.PropertyName=Value

In this syntax:

ControlID represents the ID property of the control.

PropertyName represents the control property.

Value represents the value assigned to PropertyName, which is a control's property.

CheckBox and CheckBoxList controls

Check boxes provide you with independent choices or options that you can select. You can add check boxes to a Web Forms page by using either the CheckBox control or the CheckBoxList control. The CheckBox control is a single check box that you can work with. On the other hand, the CheckBoxList control is a collection of several check boxes.

After you add the CheckBoxList control, you need to add a list of items to it. To do so:

1. Display the Properties window of the CheckBoxList control.

2. Click the ellipsis button for the Items property of the CheckBoxList control.

3. In the ListItem Collection Editor dialog box, click Add to create a new item. A new item is created and its properties are displayed in the Properties pane of the dialog box.

4. Verify that the item is selected in the Members list, and then set the item properties. Each item is a separate object and has following properties:

Text: Represents the text to be displayed for the item in the list.

Value: Represents the value associated with the item without displaying it. For example, you can set the Text property of an item as the city name and the Value property to the postal code of the city. Thus, you can keep the Text and Value properties different when you do not want the actual value to be displayed
to the user.

Selected: A Boolean value that indicates whether or not the item is selected.

RadioButton and RadioButtonList controls

Radio buttons provide a set of choices or options that you can select. You can add radio buttons to a Web Forms page by using either the RadioButton control or the RadioButtonList control. The RadioButton control is a single radio button. On the other hand, the RadioButtonList control is a collection of radio buttons. Radio buttons are seldom used singly.

Usually, you use radio buttons in a group. A group of radio buttons provides a set of mutually exclusive options — you can select only one radio button in a group. You can group a set of radio buttons in two ways:

Place a set of RadioButton controls on the page and assign them manually to a group. To do so, you can use the GroupName property.

Place a RadioButtonList control on the page; the radio buttons in the control are automatically grouped.

After you add a RadioButtonList control, you need to add the individual radio buttons.

You can do so by using the Items property in the same way as you do for the CheckBoxList control.

You add the items to a RadioButtonList control at run time in the same way as you add items to a CheckBoxList control. The following VB.NET code snippet demonstrates how to add items to a RadioButtonList control programmatic ally:

Dim RadioButtonList1 As New RadioButtonList()
Controls.Add(RadioButtonList1)
RadioButtonList1.Items.Add("Radio1")

You can use the Checked property of the RadioButton control to identify whether or not the control is selected. For the RadioButtonList control, you can access the index of the selected item by using the SelectedIndex property and access the selected item by using the SelectedItem property of the control.

Related Posts:

Building forms with asp.net part one
and two

ASP.NET Forms Building with webcontrols part two

6:59 PM Posted by Isha

Building forms with ASP.NET with web controls is introduced in the previous post and here is the continuation for that.

Web Forms components

An ASP.NET Web Forms page consists of a user interface and programming logic. The user interface helps display information to users, while the programming logic handles user interaction with the Web Forms pages. The user interface consists of a file containing a markup language, such as HTML or XML, and server controls. This file is called a page and has .aspx as its extension.

The functionality to respond to user interactions with the Web Forms pages is implemented by using programming languages, such as Visual Basic .NET and C#. You can implement the programming logic in the ASPX file or in a separate file written in any CLR-supported language, such as Visual Basic .NET or C#. This separate file is called the code behind file and has either .aspx.cs or .aspx.vb as its extension depending on the language used. Thus, a Web Forms page consists of a page (ASPX file) and a code behind file (.aspx.cs file or .aspx.vb file).

Web Forms server controls

You can design a Web Forms page by using controls called Web Forms server controls.
You can program the functionality to be provided for the server controls. The server controls are different from the usual Windows controls because they work within the ASP.NET Framework. The different types of server controls are described as follows:

HTML server controls:

These controls refer to the HTML elements that can be used in server code. The HTML elements can be converted into HTML server controls. To do so, you need to use attributes, such as ID and RUNAT, in the tags that are used to add the HTML controls.

ASP.NET server controls:

These controls do not map one-to-one to HTML server controls. ASP.NET server controls include traditional form controls, such as text boxes and buttons, and complex controls, such as tables.

Validation controls:

These controls are used to validate users' input.

Validation controls can be attached to input controls to check the values entered.

User controls: These controls are created from the existing Web Forms pages and can be used in other Web Forms pages.

Creating Web Forms Application Projects

Before you use any server control to design a Web Forms page, you need to create an ASP.NET Web Application project. You can create either a Visual Basic .NET or a C# Web Application project, depending on the programming language you want to use. A Web Application project is always created on a Web server.

When you create a Web Application project, the Application Wizard creates the necessary project files along with the page file and code behind class file as described:

WebForm1.aspx:

This page file consists of the user interface for the visual representation of the Web Forms page. The file has two views, Design and HTML. The default view is Design view.

Design view:

This view represents the user interface for the Web Forms page. You can place controls directly from the toolbox to the Web Forms page in Design view. By default, the page layout of the Web Forms page is GridLayout. This layout enables you to accurately position controls on the page by using the absolute coordinates (X,Y) of the page. In addition to GridLayout, ASP.NET provides another page layout, which is called FlowLayout.

In FlowLayout, you can add text to the page directly in Design mode. You can change the page layout from the default GridLayout to FlowLayout. To do so, right-click the page in Design view and select Properties from the context menu. Next, in the DOCUMENT Property Pages dialog box, from the Page Layout list box, select FlowLayout.

HTML view:

This view represents the ASP.NET code for the Web Forms page. To open HTML view, click the HTML tab in the designer. When the Web Application project is a Visual Basic project or a C# project, the scripting language used in the HTML page is Visual Basic or C#, respectively.


WebForm1.aspx.cs or WebForm1.aspx.vb:

This file consists of the code to implement programming logic in the Web Forms page. You can view the code file by using the Show All Files icon in the Solution Explorer window. If the Web Application project is a Visual Basic project, you use Visual Basic .NET to implement the programming logic and the code file is called the WebForm1.aspx.vb file. Conversely, if the Web Application project is a C# project, you use C# to implement the programming logic and the code file is called the WebForm1.aspx.cs file.

ASP.NET Forms Building with webcontrols

6:52 PM Posted by Isha

ASP.NET can be used to build forms with web controls as explained below.

The increased use of the Internet in the business scenario has shifted focus from desktop applications to Web-based applications. Because of this shift in focus, a Web development technology is needed that can combine the capabilities of different languages and simplify application development.

Microsoft's response to this need is the release of ASP.NET, which provides a common Web development platform.

ASP.NET
is a powerful programming platform that is used to develop and run Web based
applications and services. ASP.NET provides improved features, such as convenient debugging tools, over the earlier Web development technologies. ASP.NET provides a rich set of controls to design Web pages. Visual Studio .NET provides visual WYSIWYG (What You See Is What You Get) HTML editors. Therefore, you can design Web pages by simply dragging and dropping the controls.

ASP.NET supports the C#, Visual Basic .NET, and JScript .NET languages, all of which you can use to build programming logic for your Web pages. You can choose which one of these languages to use based on your proficiency on a particular language. One of the most important features of ASP.NET is that it provides separate files for page presentation and programming logic, which simplifies Web application development.

Introducing ASP.NET Web Forms The ASP.NET Web Forms technology is used to create asp.net programmable Web pages that are dynamic, fast, and interactive. Web pages created using ASP.NET Web Forms are called ASP.NET Web Forms pages or simply Web Forms pages.

ASP.NET uses the .NET Framework and enables you to create Web pages that are browser independent. In addition to being browser independent, the following are some of the features that may lead you to select Web Forms over other technologies to create dynamic Web pages:

Web Forms can be designed and programmed using Rapid Application Development (RAD) tools.

Web Forms support a rich set of controls and are extensible, because they provide support for user-created and third-party controls.

Any of the .NET Framework language can be used to program the ASP.NET Web Forms pages.

ASP.NET uses the Common Language Runtime (CLR) of the .NET Framework and thus benefits from its features, such as type safety and inheritance.

Related Posts:

Creation of asp.net application