Saturday, June 20, 2009

ways of using hyperlink control in data controls.

Here are some different ways of using hyperlink control in data controls.

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

<asp:HyperLink ID="hyp1" runat="server" Text="test" ></asp:HyperLink>


In data controls like datagrid and gridview we have hyperlink column and hyperlink field.
We will have a gridview containing hyperlinkfield as shown below where DataNavigateUrlFields and DataNavigateUrlFormatString are used to get
Fields from datasource and construct the url for navigation to next page.In the datagrid hyperlink column we have the same fields and you can follow the same procedure.


<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>

in code behind:
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';Persist Security Info=False ");
SqlDataAdapter da = new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select * from emp", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
gdview.DataSource = ds.Tables[0].DefaultView;
gdview.DataBind();

}


Using Mutliple dataitems
If you want to send mutliple items to next page you have to do like this



<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>



In runtime
Inorder to access the hyperlinkfield in code behind we have to follow the below
Procedure

In design view:



<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" OnRowDataBound="gdview_RowDataBound" >
<Columns>
<asp:HyperLinkField HeaderText="Category" DataTextField="CategoryName" runat="server" />
</Columns>
</asp:GridView>



In code behind:

protected void gdview_RowDataBound(object sender, GridViewRowEventArgs e)
{
HyperLinkField hyp = (HyperLinkField)gdview.Columns[0];
string[] items = new string[2];
items[0] = "CategoryName";
items[1] = "CategoryID";
hyp.DataNavigateUrlFields = items;
hyp.DataNavigateUrlFormatString ="~/page.aspx?id={0}&name={1}";
}



Using Hyperlink control

In the case of repeater and datalist we don’t have hyperlink column.In that scenario we have to use hyperlink control in itemtemplate.

.


In design view:
<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>



in code behind:


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();
}


Here in this example we have passed multiple data items to page.aspx.In the same way you can create hyperlink control in datalist.

7 comments:

  1. Thanks for the page but what is the "using"?

    this is exactly what i am looking for

    ReplyDelete
  2. sorry i m not getting properly
    wht you want exactly ?

    ReplyDelete
  3. That was my mistake - sorry about that...it was an error in my code and VS2K5 picked it up as a missing reference (using System.Web.) - the question = null now...

    Do you have an example of displaying a second piece of data in the text field? say for example..

    you wanted to include a second database field in the text of the hyperlink text?

    ReplyDelete
  4. Also is this code for 2.0? or 3.5?

    ReplyDelete
  5. i linked to this page - i hope you dont mind

    im WPHC

    http://forums.asp.net/p/1478087/3444318.aspx

    ReplyDelete
  6. "~/page.aspx?id={0}&name={1}"
    like this way you can add it
    plz chk it 4th coding example

    ReplyDelete
  7. Sorry for delayed response...

    Thanks for the feedback...

    ReplyDelete