当绑定值包含冒号时,如何绑定 GridView HyperLinkField 的 URL? [英] How to bind the URL of a GridView HyperLinkField when the bound value contains a colon?

查看:18
本文介绍了当绑定值包含冒号时,如何绑定 GridView HyperLinkField 的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定 GridView HyperLinkField,以便将绑定列用作 URL 中的参数值.非常标准的东西 - 没什么特别的,但是当绑定列包含冒号时绑定失败,即 :.我是我的特例,这个值是一个表示持续时间的字符串,例如14:35"或1:07:19".

I'm trying to bind a GridView HyperLinkField such that the bound column is used as a parameter value in the URL. Pretty standard stuff - nothing fancy, but the binding fails when the bound column contains a colon, i.e. :. I'm my particular case, this value is a string representing a duration of time, e.g. "14:35", or "1:07:19".

这是我的 GridView,时间值绑定到 HyperLinkField url.

Here's my GridView, with the time value bound to the HyperLinkField url.

<asp:GridView ID="ResultsGridView" runat="server" AutoGenerateColumns="False" 
    DataSourceID="ResultsDataSource" EnableModelValidation="True" 
        AllowPaging="True">
    <Columns>
        <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
        <asp:HyperLinkField DataNavigateUrlFields="RunTime" 
            DataTextField="RunTime" HeaderText="Hyperlink" 
            DataNavigateUrlFormatString="LinkedPage.aspx?param={0}" />
        <asp:BoundField DataField="RunTime" HeaderText="Time" 
            SortExpression="RunTime" />
        <asp:BoundField DataField="FullName" HeaderText="Name" 
            SortExpression="FullName" ReadOnly="True" />
    </Columns>
</asp:GridView>

它生成这样的 HTML.请注意, 标签没有 href 属性.

It produces HTML like this. Note that the <a> tags have no href attribute.

<tr>
    <td>2010</td><td><a>34:58</a></td><td>34:58</td><td>Joe Schmoe</td>
</tr><tr>
    <td>2010</td><td><a>35:30</a></td><td>35:30</td><td>Rod Krueger</td>
</tr><tr>
    <td>2010</td><td><a>35:38</a></td><td>35:38</td><td>Mike Johnson</td>
</tr>

但是如果我将绑定字段从 RunTime 切换到 Year,即切换到值中不包含冒号的列,它会按预期工作.以上面的 GridView 为例,更改 HyperLinkField 的 DataNavigateUrlFields 属性,如下所示:

But if I switch the bound field from RunTime to Year, i.e. to a column that doesn't contain a colon in the values, it works as expected. Take the GridView above, and change the DataNavigateUrlFields attribute of the HyperLinkField, like so:

    <asp:HyperLinkField DataNavigateUrlFields="Year" 
        DataTextField="RunTime" HeaderText="Hyperlink" 
        DataNavigateUrlFormatString="LinkedPage.aspx?param={0}" />

现在 HTML 输出是正确的,如下所示:

And now the HTML output is correct, like this:

<tr>
    <td>2010</td><td><a href="LinkedPage.aspx?param=2010">34:58</a></td><td>34:58</td><td>Joe Schmoe</td>
</tr><tr>
    <td>2010</td><td><a href="LinkedPage.aspx?param=2010">35:30</a></td><td>35:30</td><td>Rod Krueger</td>
</tr><tr>
    <td>2010</td><td><a href="LinkedPage.aspx?param=2010">35:38</a></td><td>35:38</td><td>Mike Johnson</td>
</tr><tr>

所以我的问题的核心是:如何将包含冒号的值的数据列绑定到 HyperLinkField 的 URL?或者,失败了,通过另一种方法创建相同的绑定超链接?

So the nut of my question is this: how do I bind a data column with values that contain a colon to the URL of a HyperLinkField? Or, failing that, create the same bound hyperlink by another method?

将数据格式更改为不包含冒号将是最后的手段,因为 LinkedPage.aspx 需要该格式的参数值,并且它已经被编写、测试和使用.

Changing the format of the data to not include a colon would be a last resort, because LinkedPage.aspx expects the parameter value in that format, and it's already written, tested and in use.

推荐答案

<asp:TemplateField HeaderText="Hyperlink">
        <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" 
                NavigateUrl='<%# Eval("RunTime", @"LinkedPage.aspx?param={0:hh:mm}") %>' 
                Text='<%# Eval("RunTime", @"{0:hh:mm}") %>'></asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>

这篇关于当绑定值包含冒号时,如何绑定 GridView HyperLinkField 的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆