6

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

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>

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>

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}" />

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>

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?

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.

Taz
  • 3,718
  • 2
  • 37
  • 59
Matt
  • 5,052
  • 4
  • 36
  • 54

2 Answers2

8
<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>
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • How can this hyperlink be used to load a second gridview in the same page? – aspiring May 12 '15 at 10:38
  • 1
    @aspiring use a LinkButton or ImageButton, attach a handler for the ItemCommand event, then an UpdatePanel where you include the second grid. The event handler code is for binding the datasource of this grid. – Adrian Iftode May 12 '15 at 11:04
  • I am not sure what do you meant by an update panel. The current field is a simple Boundfield to show a name in the gridview. So if I am to use a linkbutton I will have to find a way to show the db data on the linkbutton as text or sth. Perhaps you could post an answer [here.](http://stackoverflow.com/q/30188620/2061309) I will search and try your suggestion. – aspiring May 12 '15 at 11:11
1

Wow, very strange, worse comes to worse, as a very last step, you can always tap into RowDataBound, and set the cell text to hyperlink HTML yourself, but in the meantime, try tapping into RowDataBound and examining the results there. Maybe you can encode the value at binding time, so that if there is an issue with :, encoding probably will resolve it?

You may also want to submit that as a bug to connect.microsoft.com...

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Thanks, @Brian, I'll give that a try. I suspect it has something to do with a colon being used as part the value placeholder in the `DataNavigateUrlFormatString`, e.g. if I had a currency value to format I might use {0:C} instead of just {0} in the format string. – Matt Jun 30 '11 at 19:30
  • Yes that might make sense, but I would think that would only apply if you had the {}, but who knows.. – Brian Mains Jul 01 '11 at 11:19