Create A Link

To create a hyperlink programmatically with Web server controls, you can use the HyperLink control. You can use this control to set properties for the hyperlink, including where the user is directed to when clicking the hyperlink and what text represents the hyperlink.

If you want to create a link programmatically, you can use the Page_Load event to set the hyperlink's properties. To create a HyperLink Web server control on an ASP.NET page, you must add the <ASP:HYPERLINK RUNAT="Server"> tag to the page. You should also add an id attribute for the control so that you can reference it in your code.

The href for the anchor tag represents the URL that the user gets sent to after clicking the anchor tag. The anchor tag surrounds the text, or HTML, that is hyperlinked (underlined) in the user's Web browser.

The following example lets you use the Page_Load event to set the properties for the HyperLink Web server control. You must set the href and the text properties. When you request the ASP.NET Web page from the server, the Page_Load event fires and the server creates the anchor tag using the code in the server-side event.

CREATE A LINK

CREATE A LINK

Q Open the WebVisit HompageLinkTemplate.aspx template from the Code Templates directory.

< Add the code to set the HREF and Text properties for the HyperLink control.

□ Save the file and request it from the Web server.

■ The page appears with a HyperLink.

Q Open the WebVisit HompageLinkTemplate.aspx template from the Code Templates directory.

L-0 Add a HyperLink Web server control to the form.

< Add the code to set the HREF and Text properties for the HyperLink control.

□ Save the file and request it from the Web server.

Note: See pages 20 to 25 for instructions on saving a file to the Web server and then requesting the file using the IIS Admin.

■ The page appears with a HyperLink.

NDER AN IMAGE

You can use the Image Web server control to work with your images. For example, you can use the Image control to specify what image appears when the page first displays. Then, through code, you can easily replace this image.

The Image Web server control functions very similarly to the HTMLimage control. The Image Web control will reside on a server form on your ASP.NET page. The Image control is created with the <ASP: image > tag. You will then need to add an id attribute and set the runat attribute to Server. You can then add code on your page that modifies this control. You can

WORK WITH WEB CONTROLS

WORK WITH WEB CONTROLS

set the imageurl attribute for mapping the path of the image source file.

The Image Web server control does not support any events, which separates it from most of the other Web server controls. One attribute that is useful is the alternatetext attribute. This displays if the user's Web browser has graphics turned off or the source for the graphic is missing. This text for this attribute will also show as pop-up help text (if the user holds the mouse pointer over the image). You can make the pop-up help text different than the alternative text by using the tooltip attribute.

RENDER AN IMAGE

£ Untitled - Notepad

-lffl*l

I File Edit Format Help

«SCRIPT LANGUAGE-'C#" RUNAT="Server"=-void SubmltButton_Cllck(Object Sender, EventArgs e) {

ImageGoallmages ImageUrl - 'images/11 + dropdownlistGoallmages.Selectedltem. Value,

(FONT FACE-'Verdana"-

<H3>Welcome to mylitetimegoals.com</H3> Select your goal type

<FORM RUNAT-'Server">

(ASP:DROPDOWNLISTD-'dropdownllstGoallrnages" RUNAT-'Serve r"-(ASP:LISTITEM VALUE="traveLqif"=-Travel(/ASP:LISTITEM=-<ASP:LISTITEM VALUE="career.git">Career</ASP:LISTITEM> (ASP:LISTITEM VALUE="educatlonal glf'ïEducationaWASP:LISTITEMï -1 (/ASP DROPDOWNLISTs

<ASP:BUTTON TEXT="Apply" ONCLICK="SubmitButton Click" RUNAT-'Serve r"/> <P/5

(ASP:IMAGE ID-'¡mageGoallmages" IMAGEURL-'Imaqes/travelflif" ALTERNATETEXT-'Travel" RUNAT="Server" />| r

WebGoalTypesTemplate.aspx template from the Code Templates directory.

H Add an Image control to the server form.

D Open the

WebGoalTypesTemplate.aspx template from the Code Templates directory.

H Add an Image control to the server form.

S Save the file and request it from the Web server.

> Click to select the Educational goal type from the drop-down list box.

S Save the file and request it from the Web server.

Note: See pages 20 to 25 for instructions on saving a file to the Web server and then requesting the file using the IIS Admin.

■ The page appears with the Travel image.

> Click to select the Educational goal type from the drop-down list box.

—0 Click the Apply button.

-■ The image is updated with the Education image.

BUILD A TABLE

You can use the Table Web server control to dynamically create and modify tables in your ASP pages. You can also use the Table Web server control to set table properties such as background color, cell spacing and padding, and border size and color. You can use the HTML server control HTMLTable for the same purpose as this control.

You can populate the table in one of the events, such as Page_Load, before the page is generated. The Table Web control will reside on a server form on your ASP.NET page. The Table control is created with the <ASP: Table> tag. You need to add an id attribute and set the runat attribute to Server for the control to work properly.

When working with the Table Web control, you can work with several other controls. This includes the object that represents a cell, the TableCell control, and an object that represents a row, the TableRow control.

Because you normally work with these members of the TableCell and TableRow controls as collections, you can use their respective collection objects, the TableCellCollection and TableRowCollection objects. With these collections, you can create an iterative statement that will loop through all members of the collections and make programmatic modifications to each item in the table.

BUILD A TABLE

BUILD A TABLE

D Open the

GenericTemplate.aspx template from the Code Templates directory.

^0 Add a table to the page and set the BORDER attribute equal to 1.

-0 Add the Page_Load function.

'—0 Create and initialize a variable for counting rows.

° Create and initialize variables for number of rows and number of cells.

D Open the

GenericTemplate.aspx template from the Code Templates directory.

^0 Add a message to the user.

^0 Add a table to the page and set the BORDER attribute equal to 1.

Note: You need to give the form an ID attribute and set the RUNAT attribute to "Server".

-0 Add the Page_Load function.

'—0 Create and initialize a variable for counting rows.

° Create and initialize variables for number of rows and number of cells.

'—0 Create and initialize an array for the contents of the table.

Was this article helpful?

0 0

Post a comment