Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-04
7
Medium Priority
?
836 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 750 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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