[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2491
  • Last Modified:

Adding dynamic hyperlink column

I would like to add an Edit column hyperlink dynammically so the user can select it to link to another page and edit the record select.  I have been looking at 2 methods for this but have to come up with any results.  The reson this is dynamic is becuase the data being read in has a dynamic number of columns.  But one column will always be a RecId, which is what I want to pass to the Edit page.

Here are the 2 methods I've been looking into:
1. Read data into a DataTable object.  Then add another column.  I can add another column easily enough.  I set the type as a Hyperlink and then looped through the DataTable, grabing the value of RecId, and appending that to a HyperLink object navigate url like this:
hl.NavigateUrl = "~/web/RecordEdit.aspx?rec=" + hlRec;  // where hlRec is a string of the RecID
After this, I set the the DataTable as the source of a GridView and DataBind for display.  The problem here is that GridView shows all the records except the additional column.  (No suntax or run-time errors were encountered.)

2.  Read data into a DataTable object, the set it as the DataSource for the GridView.  Before DataBind on the GridView, create a HyperLinkField, set the header text and text, and add it to the GridView.  Then DataBind the GridView.  This does add the additional column and it displays, but I'm having troubles setting the NavigateUrl on the HyperLinkField items so that they link to the Edit page, apending the RecId from another column.

I could please use some assistance with either of these methods or if you have another way to accomplish the functionality I'm looking for, that's cool as well.  There isn't a lot of records being returned now (in development) but there probably will be when this gets up and running (probably not over 10,000 or anything but still a 200 or 300 or 1000 is still enough to effect lag here).

I have been all over the web the  last few days and am finding similiar things but nothing quite like what I'm trying to do.  I've also read this result (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23249566.html) but I can quite figure out how to tailor it to what I'm doing.

I'm using ASP.Net 3.5 C#.  Thanks for your help.

  • 2
1 Solution
Referring to your second method:

when you are creating HyperLinkField...set two properties of the field:


For your GV make sure you have the property set:  DataKeyNames="RecId"
nkishAuthor Commented:
I knew I was really close with that second method... I just couldn't figure out how/where to set the DataNavigateUrlFields property.  I was trying to do it in the GridView RowDataBound or the DataBound events.

Thanks for the help with that last bit.  

I posted the code below for the complete solution.  The only differance is that DataNavigateUrlFields and DataKeyNames are expecting a string[] so I needed to create one.

// dtSiteDate is the DataTable

if (dtSiteData != null)
	GV_Data.DataSource = dtSiteData;

	HyperLinkField hlf = new HyperLinkField();
	hlf.HeaderText = "Test";
	hlf.Text = "Edit";
	hlf.DataNavigateUrlFormatString = "~/web/RecordEdit.aspx?r={0}";
	string[] dataFields = new string[] {"Record Id"};
	hlf.DataNavigateUrlFields = dataFields;
	hlf.Visible = true;
	GV_Data.DataKeyNames = dataFields;


Open in new window

Yes that's correct.
Setting DataKeyNames in code-behind will need a string array...
If you set it in .aspx GV markup directly it is just string(comma separated if multiple keys);


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now