Accessing a hyperlink in a template column in a Datagrid

Discussion in 'ASP .Net' started by damonf, Oct 23, 2003.

  1. damonf

    damonf Guest

    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.
     
    damonf, Oct 23, 2003
    #1
    1. Advertising

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


    --------------------
    > 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.
    >
     
    Mike Moore [MSFT], Oct 23, 2003
    #2
    1. Advertising

  3. damonf

    damonf Guest

    Thanks Mike, that was exactly what I was looking for.

    Cheers

    Damon
     
    damonf, Oct 24, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TG1jRQ==?=

    Datagrid - Template or hyperlink Column?

    =?Utf-8?B?TG1jRQ==?=, Mar 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,365
    =?Utf-8?B?TG1jRQ==?=
    Mar 10, 2005
  2. =?Utf-8?B?V2FyYW4=?=

    HyperLink Column and Button Column in a DataGrid

    =?Utf-8?B?V2FyYW4=?=, Aug 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    612
    =?Utf-8?B?V2FyYW4=?=
    Aug 9, 2005
  3. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    11
    Views:
    1,018
    deandany
    Jul 17, 2006
  4. davetichenor
    Replies:
    1
    Views:
    856
    Eliyahu Goldin
    Oct 30, 2006
  5. Dave
    Replies:
    0
    Views:
    968
Loading...

Share This Page