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" <>
> Sender: "damonf" <>
> 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$>
> 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.
>