Accessing a hyperlink in a template column in a Datagrid

D

damonf

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.
 
M

Mike Moore [MSFT]

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(connectionString)
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.CommandBehavior.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.


--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top