Skip to main content

How to use HyperLink in asp.net

HyperLink Web Server Control
HyperLink is an asp.net web server control. we can display a hyperlink on a web form page by inserting a HyperLink server control on the page. hyperlink sever control can render as either text or as graphics. hypelink control generate a link that allow users to navigate another page or another location on the same page. NavigateUrl property value store the link destination. to display hyperlink control as a text we need to set the Text property value and to display as an image set the ImageUrl property value. if we set both ImageUrl and Text property values, ImageUrl takes precedence.

hyperlink server control's another important property is the Target property. .net developers can specify the frame or window to display the linked page by this Target property settings. target property have four possible values those are _blank, _parent, _self and _top. _blank value display the destination page in a new window without frames. _parent show the linked page in immediate frameset parent. _self show the page in frame with focus and _top show linked page in full window without frames.

we can use tilde(~) wildcard to specify application root. it is very useful to set hyperlink NavigateUrl property value. so we don't need to hard code a directory name into the application relative url.

we can set the hyperlink control's properties programmatically at run time such as NavigateUrl, Text and ImageUrl. the main advantage of hyperlink server control is that we can set link properties in server code. as example we can dynamically change the text and destination url of a link. we can data bind hyperlink control to specify target url. to create data bound hyperlink controls, we can add them as children of Repeater, DataList, DetailsView, GridView or Formview control. hyperlink does not raise a click event in server code when someone click the link.

the following c# example source code demonstrate us how can we use hyperlink web server control in asp.net. it also show declarative syntax of hyperlink control.
HyperLink.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net HyperLink example: how to use</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">HyperLink example</h2>
        <asp:HyperLink 
            ID="HyperLink1" 
            runat="server"
            Text="Visit Flying Crow Page"
            NavigateUrl="~/FlyingCrow.aspx"
            >
        </asp:HyperLink>
        <br />
        <asp:HyperLink 
            ID="HyperLink2"
            runat="server"
            >
        </asp:HyperLink>
    </div>
    </form>
</body>
</html>
FlyingCrow.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net HyperLink example: FlyingCrow</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">Flying Crow</h2>
        <asp:Image 
             ID="Image1" 
             runat="server" 
             ImageUrl="~/Images/FlyingCrow.jpg"
             BorderWidth="3"
             BorderColor="SaddleBrown"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples

Popular posts from this blog

How to use Table in asp.net

Table Web Server Control ASP.NET Table web server control display a table on a web page. It is a .NET web server control so we can program it in server code such as add, remove table row, cell programmatically. TableRow and TableCell web server controls allow us to display content for the Table. Table server control display tabular data and we can format Table server control using it's built in methods and properties. The big difference between HTML table and asp.net Table server control is that we can manipulate Table server control using an object model. Generally we should use Table server control when we intend to add remove rows and cells (columns) programmatically at run time.

Table server control acts as a parent (container) for TableRows controls. Table control's Rows property is a collection of TableRow objects. each TableRow control has a collection named Cells. Cells collection contains TableCell objects.

Actually table server control display the TableCell con…

How to use RadioButton control in asp.net c#

RadioButton web server control RadioButton control lets you make a group of radio buttons with other RadioButton. If you set it's GroupName property same for multiple radio buttons then all radio buttons with same name act as a single group. Within a group you can only select one RadioButton at a time. RadioButton group work as like RadioButtonList. With RadioButton control you can more customize it than RadioButtonList items.

Back color, border color, border style, font names, font bold, font italic, fore color (text color) and many more property help you to design RadioButton. It also support CSS class, theme and skin. Even validation are works with RadioButton control. RadioButton have AutoPostBack property and CheckChanged event. By using those feature you can determine immediately which RadioButton is selected from a group.

This example uses a RadioButton group with two RadioButton. Here we assign AutoPostBack property value to true and setup a CheckChanged event. When …

How to use RadioButtonList control in asp.net c#

RadioButtonList web server control ASP.NET RadioButtonList control enable user to select an item from list. RadioButtonList support data bind programmatically from database. We can also populate it manually by input list item inside RadioButtonList tag. RadioButtonList is a single selection radio button group. RadioButtonList have an items collection. We can determine which item is selected by test it's SelectedItem property.

We can change RadioButtonList design by setting up it's various property as like BackColor, BorderColor, BorderStyle, BorderWidth, CellPadding, CellSpacing, CssClass, Font-Bold, Font-Italic, Font-Names, Font-Overline, Font-Size etc. there are more three properties RepeatColumns, RepeatDirection and RepeatLayout which help you to place list item vertically or horizontally.

RadioButtonList have standard AutoPostBack property. As like other list control RadioButtonList have an excellent event SelectedIndexChanged. So that when someone select an item it…