简体   繁体   中英

GridView hyperlink for Date field is not displaying using vb.net

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="637px" CssClass="auto-style1">
    <Columns>
        <asp:HyperLinkField DataNavigateUrlFields="TimeStamp" DataNavigateUrlFormatString="Logs.aspx?TimeStamp={0}" DataTextField="TimeStamp" HeaderText="TIME STAMP"></asp:HyperLinkField>
    </Columns>
</asp:GridView>

<asp:SqlDataSource  ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SoneilCloudRemConnectionString %>"
                    SelectCommand="SELECT [TimeStamp] FROM [SensorLogData] WHERE ([deviceId] = @deviceId)">
    <SelectParameters>
        <asp:Parameter DefaultValue="121313131" Name="deviceId" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

The above is the code snippet used for displaying date with hyperlink. For me it displays just the date instead of it to be hyperlink. In my SQL the datatype for this date is in "datetime" format.

I tried using other field to display in hyperlink, it's working. Finding issue only for datetime.

The Gridview won't build a URL containing : (which is in the time portion of Timestamp ). This results in an <a> tag being created with no href .

If you can live without the time, you can change the DataNavigateUrlFormatString to only use the date portion:

DataNavigateUrlFormatString="Logs.aspx?TimeStamp={0:d}"

Another option is described here . It would involve formatting Timestamp to a specific format & then parsing the querystring Timestamp using `DateTime.ParseExact().

DataNavigateUrlFormatString="Logs.aspx?TimeStamp={0:yyyy-MM-dd hh-mm-ss}"

Then on Logs.aspx :

DateTime.ParseExact(Request.QueryString("Timestamp").ToString(), "yyyy-MM-dd hh-mm-ss", System.Globalization.CultureInfo.CurrentCulture)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM