Solved

Gridview: How to add a HyperLinkField to a TemplateField in CODE?

Posted on 2007-04-04
7
806 Views
Last Modified: 2008-01-09
Could you give me an example on how I'd go about dynamically creating a TemplateField with a HyperLinkField added into it...the TemplateField would then get added to a column in the GridView...? It turns out the TemplateField's ItemTemplate can only be assigned an ITemplate, and the HyperLinkField does not implement that interface. I am looking for code, not tags in the aspx page.

The DataNavigateUrlFormatString property in HyperLinkField cannot be assigned a value with ":", forexample if you wish to add "javascript:MyFunc();", and Microsoft gives the above solution as a workaround...

Appreciate it...Thanks!
0
Comment
Question by:pwerstreak
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:sandip132
ID: 18855819
0
 
LVL 12

Accepted Solution

by:
sandip132 earned 250 total points
ID: 18855822
Example from MSDN
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>

<head>

<script runat="server">

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("PriceValue", typeof(Double)));

for (int i = 0; i < 3; i++)
{
dr = dt.NewRow();

dr[0] = i;
dr[1] = (Double)i * 1.23;

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

void Page_Load(Object sender, EventArgs e)
{
MyDataGrid.DataSource = CreateDataSource();
MyDataGrid.DataBind();
}

</script>

</head>

<body>

<form runat="server">

<h3>HyperLinkColumn Example<h3>

<asp:DataGrid id="MyDataGrid"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
AutoGenerateColumns="false"
runat="server">

<HeaderStyle BackColor="#aaaadd"/>

<Columns>

<asp:HyperLinkColumn
HeaderText="Select an Item"
DataNavigateUrlField="IntegerValue"
DataNavigateUrlFormatString="detailspage.aspx?id={0}"
DataTextField="PriceValue"
DataTextFormatString="{0:c}"
Target="_blank"/>

</Columns>

</asp:DataGrid>

</form>

</body>
</html>

detailspage.aspx
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>

<script runat="server">

void Page_Load(Object sender, EventArgs e)
{
Label1.Text = "You selected item: " + Request.QueryString["id"];
}

</script>

</head>
<body>

<h3>Details page for DataGrid</h3>

<asp:Label id="Label1"
runat="server"/>

</body>
</html>



HyperLinkColumn.DataNavigateUrlField Property : Gets or sets the field from a data source to bind to the URL of the hyperlinks in the HyperLinkColumn.

HyperLinkColumn.DataNavigateUrlFormatString Property : Gets or sets the display format for the URL of the hyperlinks in the HyperLinkColumn when the URL is data-bound to a field in a data source

One more thing is that you may use Template Column. Just convert a normal column to template col. Add hyperlink in this col. Set the datavalue for this hyperlink. It's also pretty easy..

hope it would help..
0
 
LVL 13

Expert Comment

by:KarinLoos
ID: 18856017
0
 
LVL 13

Expert Comment

by:KarinLoos
ID: 19252358
I gave him 2 links with examples on dynamic templates.
Sandip explained the hyperlink column.  My suggestion is if participant doesnt respond to split points.
0
 

Author Comment

by:pwerstreak
ID: 19356821
I was looking for a solution (C#) that allowed me to call a jscript function with "javascript:MyFunc();" when user clicks on the HyperLink... the "for example" part may have thrown you off.

My points go to Sandip even though he doesn't provide a solution for the "javacript:" issue...

KarinLoos, I was not looking for a general way to dynamically add columns to a datagrid (specifically TemplateField with a HyperLinkField).
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
HTML - Color not displaying correctly in EMAIL. 6 38
.NET 2008 VB and C# 6 28
asp.net input html code have it display the content 2 13
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question