Solved

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

Posted on 2007-04-04
7
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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