Hyperlink Column not displaying

W

William LaMartin

I have a gridview (with no properties set) on an aspx page which I populate
from an XML file with the code below.

The data in the XML file looks like this

<description>National Trust for Historic Preservation</description>
<URL>http://www.nthp.org/</URL>
<category>Architecture</category>

Can anyone tell me why the other two columns are displayed, but the
hyperlink column does not display in the grid?

The code:

Dim DS As New Data.DataSet
DS.ReadXml(Server.MapPath(".") & "\data\Links.xml")
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
Dim hl As New HyperLink
Dim i As Integer

dt.Columns.Add(New Data.DataColumn("Description", GetType(String)))
dt.Columns.Add(New Data.DataColumn("URL", GetType(HyperLink)))
dt.Columns.Add(New Data.DataColumn("Category", GetType(String)))

For i = 0 To DS.Tables(0).Rows.Count - 1

dr = dt.NewRow
dr("Description") = DS.Tables(0).Rows.Item(i).Item(0) 'The link's
description
hl.NavigateUrl = DS.Tables(0).Rows.Item(i).Item(1) 'the link's URL
hl.Text = DS.Tables(0).Rows.Item(i).Item(0) 'description
hl.Target = "_Blank"
dr("URL") = hl
dr("Category") = DS.Tables(0).Rows.Item(i).Item(2) 'The link's category
dt.Rows.Add(dr)
Next

Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
 
W

William LaMartin

Because for some reason when I try to add my XML file as an XMLDataSource in
design mode I receive the following message:

There was an error rendering the control.
The data source for GridView with id 'GridView1' did not have any properties
or attributes from which to generate columns. Ensure that your data source
has content.

In researching this, I have read that instead of the structure my XML file
has, what is wanted is for each item to have attributes instead of elements.
I haven't looked into doing this, though.

Furthermore, I have found it more flexible to generate data connections in
code. And I am sure there is a way to get the hyperlink column to display
this way--I just haven't found it.
 
W

William LaMartin

The solution was to do a little hand coding on the source page of the aspx
file containing the GridView as below to create the hyperlink column. Then
create a dataset from the XML file as a datasource for the GridView and
importantly set AutoGenerateColumns=False to keep from getting redundant
data. (The field names in the XML file are and dataset are description, URL
and Category)

<asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 17px;
position: absolute;
top: 82px">

<Columns>
<asp:HyperLinkField
HeaderText="Site"
DataTextField="description"
DataTextFormatString="{0}"
DataNavigateUrlFields="URL"
DataNavigateUrlFormatString="{0}"
Target="_Blank"
/>
<asp:BoundField DataField="Category" HeaderText="Category">
</Columns>
</asp:GridView>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top