Web Forms - Working with ASP.NET server controls

Summary

Today, continuing our coverage of all the Web server controls in Visual Basic .NET, we got a look at a number of important Web server controls—image controls, image buttons, list boxes, drop-down lists, hyperlinks, and link buttons.

Image controls are designed simply to display images, which is more or less it. They correspond to HTML <img> elements and don't support events such as the Click event. Image buttons, on the other hand, do support Click events, and you can use them to create clickable image maps. When an image button is clicked, the exact location of the mouse in the image is recorded and sent to the Click event's handler, allowing you to take the action you want in response to "hotspot" clicks.

Today, we also took a look at Web server list boxes today. By default, Web server list boxes support only single selections, and you can determine which item in a list was selected using the SelectedIndex property, which returns the index of the selected item; the SelectedItem property, which returns the ListItem object corresponding to the selected item; and the SelectedValue property, which returns the value of the selected item's Value property.

You can support multiple selections in list boxes (but not drop-down list boxes) by setting the SelectionMode property to ListSelectionMode.Multiple. To determine which items are selected in a multiple selection list box, you can loop over the Items collection of ListItem objects, checking each item's Selected property to see if that item is selected.

You can add items to list controls at design time using the ListItem Collection editor, or at runtime using the list control's Items collection's Add method. We saw how to do both today.

We also took a look at drop-down lists here. These controls are similar to list boxes, except that they omit the size HTML attribute, which makes the browser display the control as a drop-down list. Drop-down lists display an arrow button that, when clicked, opens a list from which the user can make a selection. The user can only make single selections from drop-down lists, however, because when you make a selection, the drop-down list closes immediately.

Similar to list boxes, you can make sure drop-down list box events, such as the SelectedIndexChanged event, are sent back to the server as soon as they occur by setting the AutoPostback property to True, but you usually let the user make selections in list boxes and handle those selections only after the user clicks a Submit button to send the page back to the server.

Today, we also took a look at working with hyperlink controls. These controls are handled by HTML <a> elements, and we saw that you can customize hyperlink controls with the Text, NavigateUrl, ImageUrl, and Target properties. However, hyperlink controls don't support the Click event—for that, you need to use link buttons.

Link buttons appear at runtime just like hyperlinks, but in Visual Basic code they function just like Web server buttons. Specifically, you can handle Click and Command events for link buttons just as you can with Web server buttons. You use link buttons when you want to execute code when the user clicks a hyperlink, not just let the browser automatically navigate to a new URL. You saw several examples showing how link buttons can be useful, including setting URL targets on-the-fly, according to the time of day, or making other controls, such as labels, visible when a hyperlink is clicked.

That's it for today's work—tomorrow, we're going to get more Web server power as we round off our Web application work by covering the final Web server controls—validation controls, calendars, and ad rotators.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”