CODEDIGEST
Home » Articles
Search
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using HyperLink Control in DataBound Controls

By ph phani
Posted On Apr 12,2009
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

Using HyperLink Control in DataBound Controls

 

Introduction

 In this article, I will explain different ways of using hyperlink control in data bound controls.

 

What is a Hyperlink Control?

A hyperlink control is used to navigate from the client to another page. Here is an example for a hyperlink control.

 

<asp:HyperLink ID="hyp1" runat="server" Text="test" NavigateUrl=”test.aspx”></asp:HyperLink>

 

Implementation

In data bound controls like datagrid and gridview, we have in build hyperlink column/hyperlink field. In the following example, I will explain how to use hyperlink field and attach a querystring that gets populated from datasource.

We will use DataNavigateUrlFields and DataNavigateUrlFormatString to do this in gridview. You can follow the same in case of DataGrid control.

 

<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:HyperLinkField HeaderText="Category"  DataTextField="CategoryName" DataNavigateUrlFields="CategoryID"   DataNavigateUrlFormatString="~/page.aspx?id={0}" />

</Columns>

</asp:GridView>

 



Codebehind

protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            BindGrid();

        }

    }

 

    public void BindGrid()

    {

        SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");

        SqlDataAdapter da = new SqlDataAdapter("", conn);

        da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);

        DataSet ds = new DataSet();

        da.Fill(ds, "data");

        gdview.DataSource = ds.Tables[0].DefaultView;

        gdview.DataBind();     

    }

 

Multiple Querystring Parameters

If you want to send mutliple items to the target page,

 <asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" >

<Columns>

<asp:HyperLinkField HeaderText="Category"  DataTextField="CategoryName" DataNavigateUrlFields="CategoryID,CategoryName"   DataNavigateUrlFormatString="~/page.aspx?id={0}&name={1}" />

</Columns>

</asp:GridView>

 

Using Hyperlink Control in Repeater and DataList Control

<asp:Repeater ID="rp1" runat="server">

<ItemTemplate>

<table>

<tr>

<td>

<asp:HyperLink ID="hyp1" runat="server" Text='<%#Eval("CategoryName")%>' NavigateUrl='<%#"page.aspx?id=" + DataBinder.Eval(Container.DataItem,"CategoryID") + "&name=" + DataBinder.Eval(Container.DataItem,"CategoryName")%>'  ></asp:HyperLink>

</td>

</tr>

</table>

</ItemTemplate>

</asp:Repeater>

 

CodeBehind

protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            BindRep();

        }

    }

    public void BindRep()

    {

        SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");

        SqlDataAdapter da = new SqlDataAdapter("", conn);

        da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);

        DataSet ds = new DataSet();

        da.Fill(ds, "data");

        rp1.DataSource = ds.Tables[0].DefaultView;

        rp1.DataBind();

    }

Summary

In this article, I tried to explain few of the methods normally used to transfer data items to another page using Hyperlink control. Hope you liked it.

Similar Articles