PassionateDeveloper
PassionateDeveloper

Reputation: 15138

GridView Hyperlink with ID from database

I have and oBjectDataSource which Gets my Information for my Grid View.

It shows all Information perfectly with this, automaticly generated:

<asp:BoundField DataField="Name" ItemStyle-Width="250px" HeaderText="Name" SortExpression="Name">
            <ItemStyle Width="250px"></ItemStyle>
        </asp:BoundField>

But I want to have an Hyperlink with the Field "Name" as Text and the field "UserID" as paramater in the navigateURL:

 <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink2" NavigateUrl="~/Test.asp?id='<%# Eval("userID") %>'" runat="server"><%# Eval("Name") %></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>

I got an error here...

whats wrong?

Upvotes: 0

Views: 2871

Answers (4)

mausinc
mausinc

Reputation: 537

Add this field within your gridview

In this example Id = ContentId

<asp:HyperLinkField DataNavigateUrlFields="ContentId" DataNavigateUrlFormatString="EditContent.aspx?ContentId={0}" Text="Edit" HeaderText="Edit" />

Upvotes: 0

Maisie John
Maisie John

Reputation: 1026

Try this. It seems like the problem was in using double quotes in Eval(“userID”) that conflicted with opening double quotes in NavigateUrl

<asp:HyperLink ID="HyperLink2" NavigateUrl='~/Test.asp?id=<%# Eval("userID") %>' runat="server"><%# Eval("Name") %></asp:HyperLink>

Upvotes: 0

codingbiz
codingbiz

Reputation: 26386

You can also achieve it this way - easier and simpler:

<asp:HyperlinkField DataTextField="Name"
        DataNavigateUrlFormatString="~/Test.asp?id={0}"      
        DataNavigateUrlFields="userID"  
        />

Upvotes: 1

Samiey Mehdi
Samiey Mehdi

Reputation: 9414

Try this:

<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "~/Test.asp?id="+Eval("userID") %>' Text='<%# Eval("Name") %>'></asp:HyperLink>

Upvotes: 2

Related Questions