jump to navigation

How to pass multiple values using GridView HyperLinkField ? July 27, 2010

Posted by Abhijit Jana in ASP.NET, ASP.NET 4.0, C#.
Tags: , , , , ,
trackback

While working with GridView in ASP.NET, most of the time we used HyperlinkField column to navigate from one page to  different page with some value as argument.  In this blog post I have explained how can we pass multiple parameter with HyperLinkField in GridView.

To implement this features you need to know about  DataNavigationUrlField and  DataNavigateUrlFormatString properties of HyperLinkField. Let’s assume that you have a gridview as given below and you have already bind the datasource from code behind.

 <asp:gridview id="GrdEmp">
<columns runat="server" autogeneratecolumns="False" cellpadding="4">
<asp:boundfield datafield="ID" headertext="ID" />
<asp:boundfield datafield="Name" headertext="Name" />
<asp:boundfield datafield="ParentID" headertext="Base ID" />
</columns>
</asp:gridview>

Let’s first consider the case of passing single parameter. First thing that you need to do is, add a GridView “HyperLinkField “ column. After that you need to set the DataNavigationUrlField  and DataNavigateUrlFormatString properties for the same . In DataNavigationUrlField  you have to mention the name of the DataField which you want to bind as querystring. In DataNavigateUrlFormatString  you have to give the formatted URL String for the Navigation. The concept is same here as like FormatString the only difference is the data is coming from DataNavigationUrlField .

<asp:hyperlinkfield text="Details"
datanavigateurlfields="ID"
datanavigateurlformatstring="/Details.aspx?EmpId={0}">
</asp:hyperlinkfield>

In above example I have pass ID as DataNavingationUrlField. So during the Gridview Bind, for each row corresponding ID will be bind as url string with Employee ID. Below is the code snippet for the HTML View for about code snippet.

<td><a href="/Default.aspx?EmpID=123">Details</a></td>
 

To add multiple parameter you need to specify multiple DataField with comma (,) separated with in DataNavigateURLFields and also need to provide the DataNavigateURLFormatString with proper number of argument.

asp:hyperlinkfield datanavigateurlformatstring="/Details.aspx?EmpId={0}&amp;ParentID={1}" datanavigateurlfields="ID,ParentID" text="Details"></asp:hyperlinkfield>

GridField

Below is the HTML output for multiple parameter HyperLinkField.

 <td><a href="/Default.aspx?EmpID=123&ParentID=765">Details</a> </td> 

So, To summarize the post, You can use Comma separated  DataNavigateURLFields  to pass multiple DataField as an parameter with HyperlinkField in GridView. Also you need to use proper formatted URL with DataaNavigateURLFormatString

Hope this will help.

Cheers !
Shout it
kick it on DotNetKicks.com

Comments»

1. DotNetShoutout - July 28, 2010

How to pass multiple values using GridView HyperLinkField ? « Abhijit’s World of .NET…

Thank you for submitting this cool story – Trackback from DotNetShoutout…

2. Weekly Archive – 31st July 2010 « Abhijit's World of .NET - July 31, 2010

[...] 2. How to pass multiple values using GridView HyperLinkField ?  [...]

3. How to pass external values with GridView HyperLinkField which are not part of your Gridview DataSource member ? « Abhijit's World of .NET - August 3, 2010

[...] Parameter, NavigateURL, Tips and Tricks trackback Few days back I have published an article “How to pass multiple values using GridView HyperLinkField ?”,  where I have explained how you can pass multiple parameter with Gridview HyperLinkField [...]

4. Constantin - August 10, 2010

Thank you for submitting this

Abhijit Jana - August 11, 2010
5. tony - August 24, 2010

Thanks this is a great post.
I tried this and got it working right away. I have a list with a ‘contact id’, however some items returned in the gridview do not have contacts, so no id is returned and the page errors.
How do you handle an empty or invalid parameter?

6. jaejoong - October 15, 2010

Question:
How can I get the value outside the gridview..

example:

outside gridview:

thanks in advance..

Abhijit Jana - October 15, 2010

I think you are asking for passing the value which are not member of data source. If yes, please check my another blogpost
http://abhijitjana.net/2010/08/03/how-to-pass-external-values-with-gridview-hyperlinkfield-which-are-not-part-of-your-gridview-datasource-member/

Let me know if you need any more help !!

7. Kismat Raj - October 13, 2011

Thanks Great Help

8. amit - January 2, 2012

Thank you very much :-)

9. sachin mali - April 26, 2012

thank u sir

10. Anonymous - April 25, 2013

This worked, thank you..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,667 other followers

%d bloggers like this: