Answered by:
HyperLink in GridView is empty

Question
-
User-879727125 posted
I have a GridView with a TemplateField column containing a HyperLink. The HyperLink is empty; the generated HTML has an empty TD for the column.
To research I am using the following:
<asp:GridView ID="PrintersGrid" runat="server" DataSourceID="MyDataSource" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <asp:ItemTemplate> <asp:Label runat="server" ID="Label1" Text="<%# Eval('Href') %> | <%# Eval('Model') %> | " /> <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>' /> </asp:ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
The BoundFields work but the Link column is empty. I added the Label for diagnostic purposes.
I have looked at:
- Hyperlink in Item Template
- TemplateField - as HyperlinkField
- Make hyperlink of TemplateField in GridView
- TemplateField - as HyperlinkField
- unable to get the hyperlink in gridview -templatefield
The last one seems to be the same as my situation and there is no an answer in it. The first one has a solution but I don't understand how that is done in a GridView. The other three have relevant code but are asking other questions.
So why does the TemplateField not work? How can I at least diagnose this further?
Thursday, October 18, 2018 7:20 PM
Answers
-
User2103319870 posted
I get the same results if I use a SqlDataSource. I created a table and put the data into it. I get the data for the BoundField columns so I know the data is working, but the TemplateField is blank.You are using wrong ItemTemplate tag, Instead of asp:ItemTemplate use ItemTemplate like below
<asp:GridView ID="LinkBugsGridView" runat="server" DataSourceID="LinkBugsSQLDataSource" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <ItemTemplate> <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 19, 2018 10:39 PM
All replies
-
User839733648 posted
Hi Sam Hobbs,
According to your description and code, I could not reproduce your problem.
I would like to know what the format of your link in database. And What is Model ?
If possible, please provide more details of your requirement like the screenshot of your running page or database.
Besides,I've made sample on my side, and I just set the href as some website links.
The GridView shows well. The related codes are as following.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" /> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <ItemTemplate> <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeManagementConnectionString %>" SelectCommand="SELECT * FROM [tb_HyperLink]"></asp:SqlDataSource>
result:
Best Reagrds,
Jenifer
Friday, October 19, 2018 2:50 AM -
User-879727125 posted
Thank you, Jenifer.
Okay I have a small sample that shows what I am seeing. First I will explain that I am using XML as the data source. The following is a small XML file's data.
<?xml version="1.0" encoding="utf-16"?> <LinkBugs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <LinkBug> <Model>Red</Model> <Href>about:blank</Href> </LinkBug> </LinkBugs>
And I am using the following to transform it.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <LinkBugs> <xsl:apply-templates select ="//LinkBug"/> </LinkBugs> </xsl:template> <xsl:template match ="//LinkBug"> <LinkBug> <xsl:attribute name="Model"> <xsl:value-of select="Model"/> </xsl:attribute> <xsl:attribute name="Href"> <xsl:value-of select="Href"/> </xsl:attribute> </LinkBug> </xsl:template> </xsl:stylesheet>
And the following is in my LinkBug.aspx:
asp:GridView ID="LinkBugsGridView" runat="server" DataSourceID="LinkBugsDataSource" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <asp:ItemTemplate> <asp:Label runat="server" ID="Label1" Text="<%# Eval('Href') %> | <%# Eval('Model') %> | " /> <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>' /> </asp:ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Friday, October 19, 2018 7:34 AM -
User-879727125 posted
Now that I have researched how to insert images I can do that for here.
The following is what I see from the page sample
The following is what the HTML looks like:
So there is nothing being generated for the relevant column.
Friday, October 19, 2018 2:36 PM -
User475983607 posted
Your question is very confusing. You are showing XML with XSLT and data binding using an XML source. Are you implementing an XML data source or XSLT?
The main issue I see is the the XML does not not contain an image URL or link URL. As far as I can tell the code is functioning as expected - as written.
Friday, October 19, 2018 2:51 PM -
User-879727125 posted
I get the same results if I use a SqlDataSource. I created a table and put the data into it. I get the data for the BoundField columns so I know the data is working, but the TemplateField is blank. So it might be a problem with my system.
<%@ Page Title="Link Bug" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="LinkBug.aspx.cs" Inherits="Workbench_LinkBug" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:XmlDataSource ID="LinkBugsDataSource" runat="server" DataFile="~/Workbench/LinkBug.xml" TransformFile="~/Workbench/LinkBug.xslt"> </asp:XmlDataSource> <asp:SqlDataSource ID="LinkBugsSQLDataSource" runat="server" SelectCommand="SELECT Model, Href FROM LinkBug" ConnectionString="Data Source=(localdb)\ProjectsV13;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" /> <asp:GridView ID="LinkBugsGridView" runat="server" DataSourceID="LinkBugsSQLDataSource" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <asp:ItemTemplate> <asp:Label runat="server" ID="Label1" Text="<%# Eval('Href') %> | <%# Eval('Model') %> | " /> <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>' /> </asp:ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </asp:Content>
Friday, October 19, 2018 4:00 PM -
User-879727125 posted
Your question is very confusing. You are showing XML with XSLT and data binding using an XML source. Are you implementing an XML data source or XSLT?
The main issue I see is the the XML does not not contain an image URL or link URL. As far as I can tell the code is functioning as expected - as written.
Your reply is very confusing. My question said nothing about images. And I did provide a link URL; the tag name is Href and the data is "about:blank".
I don't know what a XSLT data source is. The XmlDataSource control has a TransformFile attribute and I am using a XSLT file for it. The XmlDataSource Web Server Control Overview says:
If you want to bind to values that are not attributes, you can specify a transformation using an Extensible Stylesheet Language (XSL) style sheet.
And that is what I am doing. So I don't understand the problem with "XML with XSLT".
Friday, October 19, 2018 4:51 PM -
User2103319870 posted
I get the same results if I use a SqlDataSource. I created a table and put the data into it. I get the data for the BoundField columns so I know the data is working, but the TemplateField is blank.You are using wrong ItemTemplate tag, Instead of asp:ItemTemplate use ItemTemplate like below
<asp:GridView ID="LinkBugsGridView" runat="server" DataSourceID="LinkBugsSQLDataSource" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <ItemTemplate> <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 19, 2018 10:39 PM -
User-879727125 posted
The GridView shows well. The related codes are as following.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" /> <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" /> <asp:BoundField DataField="Href" HeaderText="Href" SortExpression="Href" /> <asp:TemplateField HeaderText="Link" SortExpression="Model"> <ItemTemplate> <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='<%# Eval("Href") %>' Text='<%# Eval("Model") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeManagementConnectionString %>" SelectCommand="SELECT * FROM [tb_HyperLink]"></asp:SqlDataSource>
What happens when you use the code I posted? You know, with asp:ItemTemplate, not ItemTemplate?
Saturday, October 20, 2018 4:34 AM -
User-879727125 posted
You are using wrong ItemTemplate tag, Instead of asp:ItemTemplate use ItemTemplate like belowYou could have said something like "Try using ItemTemplate instead of asp:ItemTemplate.". I am not commenting on grammar, I am trying to say that the word "wrong" has emotional connotations. So just remember that the word "wrong" is usually the wrong word; it can lead to problems.
You appear to be correct though. It works when I remove the "asp:". It is unfortunate that something like that can be overlooked so easily. I hope this helps others. If I could give bonus points then I would if you can tell us what "asp:ItemTemplate" is.
The label control in my sample code does not work when I make the preceding correction but that is a different problem entirely.
Saturday, October 20, 2018 4:43 AM -
User2103319870 posted
You could have said something like "Try using ItemTemplate instead of asp:ItemTemplate.". I am not commenting on grammar, I am trying to say that the word "wrong" has emotional connotations. So just remember that the word "wrong" is usually the wrong word; it can lead to problems.Agreed and Noted :)
Saturday, October 20, 2018 3:21 PM