In this article I will explain with an example, how to use HyperLink inside Repeater control in ASP.Net using C# and VB.Net.
This article will illustrate how to bind QueryString Parameters (Single or Multiple) using NavigateUrl property of HyperLink and Eval function inside Repeater control in ASP.Net using C# and VB.Net.
Source Page
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
The HTML Table inside Repeater consists of 4 columns. The first and fourth column have HyperLink controls.
NavigateUrl with Single QueryString Parameter
In the first column, the value of ID field is set as QueryString parameter.
NavigateUrl='<%# Eval("Id", "~/Details.aspx?Id={0}") %>'
NavigateUrl with Multiple QueryString Parameters
In the fourth column the ID, Name and Country fields are set as QueryString parameters.
NavigateUrl='<%# string.Format("~/Details.aspx?Id={0}&Name={1}&Country={2}",
HttpUtility.UrlEncode(Eval("Id").ToString()), HttpUtility.UrlEncode(Eval("Name").ToString()), HttpUtility.UrlEncode(Eval("Country").ToString())) %>'
Note: The values of all the ID, Name and Country fields are encoded and then set as QueryString parameters.
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 30px">
Id
</th>
<th scope="col" style="width: 120px">
Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
<th scope="col" style="width: 80px">
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Id", "~/Details.aspx?Id={0}") %>'
Text='<%# Eval("Id") %>' />
</td>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Country") %>
</td>
<td>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# string.Format("~/Details.aspx?Id={0}&Name={1}&Country={2}",
HttpUtility.UrlEncode(Eval("Id").ToString()), HttpUtility.UrlEncode(Eval("Name").ToString()), HttpUtility.UrlEncode(Eval("Country").ToString())) %>'
Text="View Details" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net Repeater control
The Repeater is populated with a dynamic DataTable with some dummy data inside the Page Load event handler.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Repeater1.DataSource = dt
Repeater1.DataBind()
End If
End Sub
Destination Page
HTML Markup
The following HTML Markup consists of three Label controls for displaying the QueryString parameter values sent from Source page.
<table>
<tr>
<td>
<b>Id</b>
</td>
<td>
<asp:Label ID="lblId" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
<asp:Label ID="lblName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Country</b>
</td>
<td>
<asp:Label ID="lblCountry" runat="server"></asp:Label>
</td>
</tr>
</table>
Fetching the QueryString parameter values
Inside the Page Load event, the values of the QueryString parameters sent from the Source page are extracted, decoded and assigned to the respective Label controls.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lblId.Text = HttpUtility.UrlDecode(Request.QueryString["Id"]);
lblName.Text = HttpUtility.UrlDecode(Request.QueryString["Name"]);
lblCountry.Text = HttpUtility.UrlDecode(Request.QueryString["Country"]);
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblId.Text = HttpUtility.UrlDecode(Request.QueryString("Id"))
lblName.Text = HttpUtility.UrlDecode(Request.QueryString("Name"))
lblCountry.Text = HttpUtility.UrlDecode(Request.QueryString("Country"))
End If
End Sub
Screenshots
The Repeater control with HyperLinks
The values of QueryString parameters
Demo
Downloads