Groups | Blog | Home
all groups > asp.net > may 2005 >

asp.net : Hyperlink column in Datatable???


Tim::..
5/30/2005 11:52:10 PM
I currently have the following datagrid but want to turn the name and email
column into a hyperlink in the codebehind!

Can someone please tell me how I achieve this!

Thanks

Private Sub createTable()
Dim tbcontacts As DataTable = New DataTable("contacts")
tbcontacts.Columns.Add(" ", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Name", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Dept.", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Ext", System.Type.GetType("System.String"))
tbcontacts.Columns.Add("Email", System.Type.GetType("System.String"))
ds.Tables.Add(tbcontacts)
Mona
5/31/2005 12:00:00 AM
Hi Tim,

The HyperLinkColumn in case of displaying the ID's as link on displaying the
details information would be done as given below:

Method 1:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="ID" DataNavigateUrlField="ID"
DataNavigateUrlFormatString="link.aspx?id={0}"
DataTextField="ID"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>


So in case of just displaying the Website we would normally write it as:

Method 2:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

Issue:

Using method 2 we would get the link which would be like
"http://servername/WebArticles/DataGrid/www.asp.net"

Note: If the Website Column has value as "http://www.asp.net" the Property
DataNavigateUrlFormatString="{0}" would give the proper result

But in our case the website values are "www.asp.net" so the way to handle
this is: Method 3:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:HyperLinkColumn HeaderText="Link" DataNavigateUrlField="Link"
DataNavigateUrlFormatString="http://{0}"
DataTextField="Link"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>

In this case none of the above given Methods would do the task. The
workaround is use TemplateColumn and Helper function

Method 4:
<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Link">
<ItemTemplate>
<asp:HyperLink Runat =server
NavigateUrl ='<%#GetURL(DataBinder.Eval(Container.DataItem, "Link"))%>' >
<%#DataBinder.Eval(Container.DataItem, "Link")%>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</asp:DataGrid>


Here GetURL is the helper function

Function GetURL (ByVal fldval As String) As String
If InStr(fldval, "http://")Then
Return fldval
Else
Return "http://" & fldval
End If
End Function


The code to bind data to DataGrid would be as.....

Private ds As DataSet = New DataSet()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
binddata()
End If
End Sub

Sub binddata()
Dim sqlStmt As String = "Select * from Documentlink "
Dim conString As String =
"server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub

HTH

Mona[Grapecity]

[quoted text, click to view]

Mona
5/31/2005 12:00:00 AM
Hi Tim,

The hyperlink columns cannot be set through the datatable, as the =
datatable is=20
your object that should contain all the relevant data which you then =
manipulate=20
and present to the end user. If you want hyperlink columns, you should =
do this=20
through the datagrid, since it is the presentation component for your =
application.=20
You can do this using the method I showed you in my previous email

HTH

Mona[Grapecity]


[quoted text, click to view]
Tim::..
5/31/2005 2:53:01 AM
Thank you for the reply but this isn't really what I asked for...

I want to know how to create a hyperlink column in the codebehind...

ie: How do I create a hyperlink column programatically in a datatable???

Thank you for the response...



[quoted text, click to view]
AddThis Social Bookmark Button