Setting DataNavigateUrlFields In Code

Posted by Sir_ross under Regular Expressions on 6/15/2010 | Views : 3774 | Status : [Member] | Replies : 1

I'm trying to dynamically create a grid view in C#, the grid will have two columns, one of which is a hyperlink field.
Now i've noticed when creating the grid view statically in aspx, the DataNavigateUrlFields property accepts a single string as it's value (the field in the data source with the URL in).

When creating it in code, the very same property is a string[] array, rather than a single string, and is expecting multiple fields which combine together to make the URL (if I've understood MSDN correctly), but what if I just want a single field for the URL?

Why does aspx let me do this, but the C# not?

Here's my code:

Name.HeaderText = "Exercise";
Name.DataField = "Name";
Link.HeaderText = "More Info";
Link.DataTextField = "Link";
Link.DataNavigateUrlFields = "Link";

The code above does NOT compile, I got the following error:

Error 2 Cannot implicitly convert type 'string' to 'string[]'
This is regarding the last line.

Here is the aspx code, which it's fine with:

<asp:HyperLinkField HeaderText="More Info" DataTextField="Link" DataNavigateUrlFields="Link"
target="_blank" />

Does anyone know how I can dynamically create a hypertext field for a grid view, where it only takes a signle field from the data source as the URL?

Thanks for your help,



Posted by: Sir_ross on: 6/15/2010 [Member] Starter

**************PROBLEM SOLVED**********************

I declared string[] url { "Link" };

and then:

Link.DataNavigateUrlFields = url;

This worked :-)

Sir_ross, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response