How to bind a HyperLinkField GridView URL when the linked value contains a colon?

I am trying to bind a GridView HyperLinkField so that the linked column is used as the parameter value in the url. Pretty standard stuff - nothing out of the ordinary, but the anchor fails when the associated column contains a colon, i.e. : . I am my special case, this value is a string representing the length of time, for example. "14:35" or "1:07:19".

Here is my GridView with the time value associated with 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 creates HTML like this. Note that the <a> tags do not have the 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 linked field from RunTime to Year, that is, to a column that does not contain a colon in the values, it works as expected. Take the GridView above and change the DataNavigateUrlFields attribute for HyperLinkField, for example:

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

And now the HTML output is correct, for example:

 <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 associate a data column with values ​​that contain a colon for the HyperLinkField URL? Or, if it is not, create the same linked hyperlink using a different method?

Changing the data format to not include a colon would be a last resort, since LinkedPage.aspx expects a parameter value in this format, and it has already been written, tested and used.

+6
source share
2 answers
 <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> 
+8
source

Wow, very strange, worse, worse, as the last step, you can always use RowDataBound and set the HTML hyperlink for the cell text yourself, but in the meantime try clicking on RowDataBound and examine the results there, Maybe you can encode the value during the binding, so if there is a problem with: maybe encoding will allow it?

You can also send this as an error for connect.microsoft.com ...

NTN.

+1
source

Source: https://habr.com/ru/post/891760/


All Articles