This
article will show you how you can bind a hyperlink control with url parameter
or requestquery string value to gridview in asp.net using c#.net. In this
article I have used asp.net hyperlink control to generate a hyper link with
parameter inside the TemplateField’s ItemTemplate.
Some of my
previous articles are as follows: Ajax
GridView Shorting Without Page Refresh Using jQuery In Asp.net and C#.Net, How
To Use Hyper Link Tag In Gridview In Asp.Net C#, Enable
Or Disable GridView Button By Row CheckBox Using jQuery In Asp.Net, Browse,
Read and Populate or Show or Bind CSV File Data In GridView Using C#.Net in
Asp.net, Bind
Class Property to GeidView Using C#.net In Asp.net, Gridview
Auto Generate Row Number In Asp.Net Using C#.Net, Bind
TextBox Control Inside TemplateField Of GridView Using C#.Net In Asp.Net, Short
GridView Data From Header Using C#.Net In Asp.Net, Export
GridView Or Table Data Into PDF By C#.Net In Asp.Net Using jQuery, Reading
XML Document in C# Using Linq and Bind To GridView In Asp.Net.
So for this
article first I have created an asp.net application and add the below code of
gridview which have TemplateField.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="WebApplication7.WebForm7" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Bind HyperLink
Control To GridView In Asp.net Using C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="CountryName"
HeaderText="Country Name"
/>
<asp:BoundField DataField="Population"
HeaderText="Population"
/>
<asp:BoundField DataField="Code" HeaderText="Code" />
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1"
runat="server" NavigateUrl='<%# Eval("Link") %>'>Detail</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
In above code inside the ItemTemplate I have added an asp.net hyperlink control. This control’s NavigateUrl I have bind with the link field.
With the
help of edit template we can bind the navigation url of hyperlink.
Now check
the code to bind the grid view.
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class WebForm7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt =
GetCountryData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetCountryData()
{
DataTable
dt = new DataTable();
dt.Clear();
dt.Columns.Add("CountryName");
dt.Columns.Add("Population");
dt.Columns.Add("Code");
dt.Columns.Add("Link");
DataRow dataRow1 =
dt.NewRow();
dataRow1["CountryName"] = "India";
dataRow1["Population"] = "125
Cr";
dataRow1["Code"] = "IN";
dataRow1["Link"] = "http://www.aspdotnet-pools.com?country=India";
dt.Rows.Add(dataRow1);
DataRow dataRow2 =
dt.NewRow();
dataRow2["CountryName"] = "Pakistan";
dataRow2["Population"] = "50
Cr";
dataRow2["Code"] = "PK";
dataRow2["Link"] = "http://www.aspdotnet-pools.com?country=Pakistan";
dt.Rows.Add(dataRow2);
DataRow dataRow3 =
dt.NewRow();
dataRow3["CountryName"] = "United
States";
dataRow3["Population"] = "25
Cr";
dataRow3["Code"] = "US";
dataRow3["Link"] = "http://www.aspdotnet-pools.com?country=United-States";
dt.Rows.Add(dataRow3);
return dt;
}
}
}
|
In above code I have added a new column “Link”, which is having the navigation url with parameter to bind with the hyperlink control. Now we have done run the application to check the output.
0 comments:
Please let me know your view