Solved

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

Posted on 2007-04-04
7
784 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
Comment Utility
0
 
LVL 12

Accepted Solution

by:
sandip132 earned 250 total points
Comment Utility
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
Comment Utility
0
 
LVL 13

Expert Comment

by:KarinLoos
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now