Thursday, 26 May 2016

How To Use Hyper Link Tag In Gridview In Asp.Net C#

5/26/2016 - By Pranav Singh 0

This article will show you how you can bind a hyper link in gridview in asp.net using c#.net. In this article I have used html hyper link tag l to generate a hyper link inside the TemplateField’s ItemTemplate.

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>
<a href='http://www.aspdotnet-pools.com?CountryCode=<%#Eval("Code") %>'><%#Eval("CountryName") %></a>                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

In above code inside the ItemTemplate I have used html hyper link tag, and tag is again bind the the data fields.

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";
            dt.Rows.Add(dataRow1);

            DataRow dataRow2 = dt.NewRow();
            dataRow2["CountryName"] = "Pakistan";
            dataRow2["Population"] = "50 Cr";
            dataRow2["Code"] = "PK";
            dt.Rows.Add(dataRow2);

            DataRow dataRow3 = dt.NewRow();
            dataRow3["CountryName"] = "United States";
            dataRow3["Population"] = "25 Cr";
            dataRow3["Code"] = "US";
            dt.Rows.Add(dataRow3);

            return dt;
        }
    }
}

In above code I have prepared a datatable. This datatable I will bind to grid view. Now we have done run the application to check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top