MrProgram
MrProgram

Reputation: 5242

URL Navigation with HyperLinkField inside a gridview

I'm using a HyperLinkField inside a gridview, and I want to link to another URL + an ID.

<div id="searchResults" runat="server">
    <asp:GridView ID="gvSearchResult" runat="server" AutoGenerateColumns = "false" 
    CaptionAlign="NotSet" CellPadding="5">
    <Columns>
        <asp:TemplateField HeaderText="Användare">
            <ItemTemplate>
                <%# Eval("UName")%>
                <br />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField DataNavigateUrlFields="UName" 
                            DataNavigateUrlFormatString='/MemberPages/profile.aspx?ID=<%# Eval("PID") %>'
                            DataTextField="UName" 
                            HeaderText="Besök sida" 
                            SortExpression="Name" 
                            ItemStyle-Width="100px"
                            ItemStyle-Wrap="true" />
    </Columns>
    </asp:GridView>
</div>

The gridview is using datasource and databind. It's complaining about:

DataNavigateUrlFormatString="/MemberPages/profile.aspx?ID=<%# Eval("PID") %>"

I'm not sure where to use <%# Eval("PID") %>, I'm sure there is something like PID, I've doublechecked.

If I'm using NavigateUrl="/MemberPages/profile.aspx?ID=<%# Eval("PID") %>" I also get the same error:

Literal content ('<asp:HyperLinkField DataNavigateUrlFields="UName" 
                               DataNavigateUrlFormatString="/MemberPages/profile.aspx?ID=') is not allowed within a 'System.Web.UI.WebControls.DataControlFieldCollection'.

Upvotes: 6

Views: 14535

Answers (3)

Dan
Dan

Reputation: 79

While the accepted answer does work. For my case I needed to use a different control. This example lets you use the Eval with the URL string.

<asp:LinkButton PostBackUrl='<%#"~/config.aspx?Id=" + Eval("Id") %>'  runat="server">Configuration</asp:LinkButton>

Upvotes: 0

semao
semao

Reputation: 1757

If you need to use " inside attribute value, use ' as delimiter

Attribute='Some value with " symbol'

If you need to use ' inside attribute value, use "

Attribute="Some value with ' symbol"

Also change your column definition

<asp:HyperLinkField DataNavigateUrlFields="PID" 
                    DataNavigateUrlFormatString="/MemberPages/profile.aspx?ID={0}"
                    DataTextField="UName" 
                    HeaderText="Besök sida" 
                    SortExpression="Name" 
                    ItemStyle-Width="100px"
                    ItemStyle-Wrap="true" />

In DataNavigateUrlFormatString attribute you use data column specified in DataNavigateUrlFields (the formating is similar to String.Format method).

Upvotes: 11

will simmons
will simmons

Reputation: 187

The first thing I would do is replace the below line

DataNavigateUrlFormatString="/MemberPages/profile.aspx?ID=<%# Eval("PID") %>"

with the line below

DataNavigateUrlFormatString='/MemberPages/profile.aspx?ID=<%# Eval("PID") %>'

notice I replaced the double quotes with single quotes and the beginning and end.

Upvotes: 0

Related Questions