Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.
like image 220
MrProgram Avatar asked Jan 10 '13 20:01

MrProgram


1 Answers

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).

like image 126
semao Avatar answered Sep 28 '22 08:09

semao