6

我正在使用 a HyperLinkFieldinside a gridview,并且我想链接到另一个URL + 一个 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>

gridview正在使用datasourcedatabind。它抱怨:

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

我不确定在哪里使用<%# Eval("PID") %>,我确定有 PID 之类的东西,我已经仔细检查过。

如果我正在使用NavigateUrl="/MemberPages/profile.aspx?ID=<%# Eval("PID") %>",我也会得到同样的错误:

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

3 回答 3

10

如果需要在属性值内使用 ",请使用 ' 作为分隔符

Attribute='Some value with " symbol'

如果需要在属性值内使用 ',请使用 "

Attribute="Some value with ' symbol"

还要更改您的列定义

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

在 DataNavigateUrlFormatString 属性中,您使用 DataNavigateUrlFields 中指定的数据列(格式类似于 String.Format 方法)。

于 2013-01-10T20:35:11.073 回答
0

我要做的第一件事是替换下面的行

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

与下面的行

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

注意我用单引号和开头和结尾替换了双引号。

于 2013-01-10T20:29:37.987 回答
0

虽然接受的答案确实有效。对于我的情况,我需要使用不同的控件。此示例允许您将 Eval 与 URL 字符串一起使用。

<asp:LinkButton PostBackUrl='<%#"~/config.aspx?Id=" + Eval("Id") %>'  runat="server">Configuration</asp:LinkButton>
于 2014-10-15T14:35:39.687 回答