views:

879

answers:

2

Hello,

I am using asp.net and I'm working with a gridview. I have one of the fields in my GridView set up as a HyperLinkField that represents the name of the category. I pull the name and Id from a database and I can never be sure what they are because they are added separately. I want to be able to pass a changing CategoryId to another page through the query string. How can I do this with a GridView?

A: 

You can do it by using a <asp:TemplateField /> as one of your columns instead of a <asp:BoundField />

Code example:

<asp:TemplateField HeaderText="Name">
    <ItemTemplate>
       <a href="/new_page?categoryId=<%#Container.DataItem("id")"><%#Container.DataItem("name")</a>
    </ItemTemplate>
</asp:TemplateField>

There might be a cleaner way of doing this rather than using the anchor element, but you should get the idea.

regex
+1  A: 

In your Hyperlink field, set the DataNavigateUrlFields to your ID column and set the DataNavigateUrlFormatString to the Url to navigate to (where {0} will be replaced by ID)

<asp:HyperLinkField 
    DataNavigateUrlFields="ID" 
    DataNavigateUrlFormatString="Target.aspx?ID={0}"
    DataTextField="Name" 
    Target="_blank">
</asp:HyperLinkField>
Gordon Bell
do you mean DataTextField="Name", then you don't need DataTextFormatString
bendewey