How to pass multiple values using GridView HyperLinkField in Query string in C#

While working with GridView, so many times we need to navigate to another page when we click on a control. Generally to do so we use HypeLinkField. But what if we have to pass data as query string and that is dynamic. In that case we have to write some more lines of code to set dynamic query string value.

 

Whole process can be done without writing single line of code except grid binding, if we have knowledge of DataNavigationUrlField and DataNavigateUrlFormatString properties of HyperLinkField. In DataNavigationUrlField, write comma separate fields name those you want to pass in query string. And in DataNavigateUrlFormatString, write url where you want to go and query string field like below:

DataNavigationUrlField = “Name, Country, Sex”

DataNavigateUrlFormatString = “test.aspx?name={0}&country={1}&gender={2}”

 

.axpx Page

<%@ Page Title=”How to pass multiple values using GridView HyperLinkField in Query string” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true” CodeFile=”Navigation_From_Grid.aspx.cs” Inherits=”Navigation_From_Grid” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”HeadContent” runat=”Server”>
   How to pass multiple values using <span class=”hiddenSpellError” pre=”using “>GridView</span> HyperLinkField in Query string
</asp:Content>
<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”Server”>
    <asp:GridView ID=”grdNav” runat=”server” AutoGenerateColumns=”false”>
        <Columns>
            <asp:TemplateField HeaderText=”Sr.No.”>
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField=”Name” HeaderText=”Name” />
            <asp:BoundField DataField=”Country” HeaderText=”Country” />
            <asp:HyperLinkField Text=”Show” HeaderText=”View Details” DataNavigateUrlFields=”Name,Country”DataNavigateUrlFormatString=”ViewDetails_From_Navigation_From_gridPage.aspx?name={0}&country={1}” />
        </Columns>
    </asp:GridView>
</asp:Content>

Code Behind Page:

using System;
using System.Data;

public partial class Navigation_From_Grid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        grdNav.DataSource = CreateTable();
        grdNav.DataBind();
    }

private DataTable CreateTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(“Name”, typeof(string));
        dt.Columns.Add(“Country”, typeof(string));

        DataRow dr;
        dr = dt.NewRow();
        dr[0] = “Sunil”;
   dr[1] = “India”;
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = “Maya”;
        dr[1] = “India”;
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = “Varsha”;
        dr[1] = “USA”;
        dt.Rows.Add(dr);

        return dt;
    }
}

 

1

 

In the above Image when you take mouse over hyperlink that is “Show” you can see generated url automatically populated with values.

Leave a comment