【问题标题】:URL Navigation with HyperLinkField inside a gridview在 gridview 中使用 HyperLinkField 进行 URL 导航
【发布时间】:2012-12-25 08:22:27
【问题描述】:

我在gridview 中使用HyperLinkField,我想链接到另一个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") %>"

我不确定在哪里使用&lt;%# Eval("PID") %&gt;,我确定有类似 PID 的东西,我已经仔细检查了。

如果我使用NavigateUrl="/MemberPages/profile.aspx?ID=&lt;%# Eval("PID") %&gt;",我也会收到同样的错误:

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

【问题讨论】:

    标签: c# asp.net gridview hyperlink


    【解决方案1】:

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

    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 方法)。

    【讨论】:

    • 数据绑定标签告诉我:数据绑定表达式仅在具有 DataBinding 事件的对象上受支持。 System.Web.UI.WebControls.HyperLinkField 没有 DataBinding 事件。
    • 非常感谢!你让我今天一整天都感觉很好。今天学习了12个小时,终于可以睡觉了:)谢谢!
    • 但是,您能告诉我 {0} 在做什么吗?
    • 你会发现描述here
    • 我必须在 gridview 的不同行上打开与该行特定单元格中的内容相关的不同页面。我该怎么做?
    【解决方案2】:

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

    DataNavigateUrlFormatString="/MemberPages/profile.aspx?ID="

    下面一行

    DataNavigateUrlFormatString='/MemberPages/profile.aspx?ID='

    请注意,我将双引号替换为单引号以及开头和结尾。

    【讨论】:

    • 哎呀。现在看到了。更改了它(并编辑了问题),但我仍然没有得到正确的 URL。 '/MemberPages/profile.aspx?ID=' 应该是 '/MemberPages/profile.aspx?ID=10'
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      相关资源
      最近更新 更多