Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Accessing a hyperlink in a template column in a Datagrid

Reply
Thread Tools

Accessing a hyperlink in a template column in a Datagrid

 
 
damonf
Guest
Posts: n/a
 
      10-23-2003
I'm currently trying to add an ASP hyperlink to a
template column in a datagrid. The normal hyperlink
column doesn't give me the ability to add attributes to
the item. In my grid there are four columns. Three are
databound to a dataset and one is a template column. I
need to be able to access each item in the template
column (getting access to the hyperlink) then adding an
attribute to call some client side code. Does anyone
know how I can achieve this? I can add the hyperlink to
the template, but then I'm not sure how I can access
items in the template column to modify the attributes.

Thanks in advance.
 
Reply With Quote
 
 
 
 
Mike Moore [MSFT]
Guest
Posts: n/a
 
      10-23-2003
Hi Damonf,

You can use the ItemDatabound event to gain full access to the controls in
the datagrid. In the following sample I have a grid based on the Pubs
sample database. The first column is a template column and the rest are
autogenerate. In the ItemDataBound event, I create a new hyperlink control
and set its properties. Then I add it to the template column.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionStri ng)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavi or.CloseConnection)
DataGrid1.DataSource = Qry1
DataGrid1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim h As New HyperLink
h.NavigateUrl = "http:/" & e.Item.DataItem("au_lname") & ".aspx"
h.Text = e.Item.DataItem("au_lname")
e.Item.Cells(0).Controls.Add(h)
End If
End Sub

---
I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computerís security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
> Content-Class: urn:content-classes:message
> From: "damonf" <(E-Mail Removed)>
> Sender: "damonf" <(E-Mail Removed)>
> Subject: Accessing a hyperlink in a template column in a Datagrid
> Date: Thu, 23 Oct 2003 03:46:19 -0700
> Lines: 13
> Message-ID: <08e701c39952$e439b120$(E-Mail Removed)>
> MIME-Version: 1.0
> Content-Type: text/plain;
> charset="iso-8859-1"
> Content-Transfer-Encoding: 7bit
> X-Newsreader: Microsoft CDO for Windows 2000
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> Thread-Index: AcOZUuQ5X1LhXxmNSX24GyDoNCRrzQ==
> Newsgroups: microsoft.public.dotnet.framework.aspnet
> Path: cpmsftngxa06.phx.gbl
> Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:185999
> NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
> X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
> I'm currently trying to add an ASP hyperlink to a
> template column in a datagrid. The normal hyperlink
> column doesn't give me the ability to add attributes to
> the item. In my grid there are four columns. Three are
> databound to a dataset and one is a template column. I
> need to be able to access each item in the template
> column (getting access to the hyperlink) then adding an
> attribute to call some client side code. Does anyone
> know how I can achieve this? I can add the hyperlink to
> the template, but then I'm not sure how I can access
> items in the template column to modify the attributes.
>
> Thanks in advance.
>


 
Reply With Quote
 
 
 
 
damonf
Guest
Posts: n/a
 
      10-24-2003
Thanks Mike, that was exactly what I was looking for.

Cheers

Damon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add a hyperlink column to asp.net datagrid - where only one value is a hyperlink davetichenor ASP .Net 1 10-30-2006 02:57 PM
How to add a hyperlink column to asp.net datagrid - where only one value is a hyperlink Dave ASP .Net Datagrid Control 0 10-21-2006 07:48 PM
How to assign a column of URLs to Hyperlink Column of a DataGrid c =?Utf-8?B?ZGF2aWQ=?= ASP .Net 11 07-17-2006 07:20 PM
HyperLink Column and Button Column in a DataGrid =?Utf-8?B?V2FyYW4=?= ASP .Net 0 08-09-2005 12:00 AM
Datagrid - Template or hyperlink Column? =?Utf-8?B?TG1jRQ==?= ASP .Net 2 03-10-2005 02:09 PM



Advertisments