how to get the text of hyperlink databound columns in datagrid

Posted on 2009-02-21
Last Modified: 2013-11-07
I have a datagrid in my webpage and the items are bound to datagrid in a hyper link, i mean datagrid will display one column of table from database which will be in hyper link, i want to get the text of hyperlink  when user click the hyper link text , which event i should use to store the clicked hyperlink text in session, please guide me
Question by:muneerz
    LVL 15

    Expert Comment

    Use a template field and a link button, then intercept it in the ItemCommand function.

    Something like this:
            <asp:LinkButton runat="server" Text='<%# Eval("URL") %>' CommandName="URLCLICK" CommandArgument='<% Eval("URL") %>' />

    and in the code behind:
    Protected Sub datagrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles datagrid1.ItemCommand
        If e.CommandName = "URLCLICK" Then
            Session("theUrl") = e.CommandArgument
        End If
    End Sub

    Author Comment

    this is not working , sorry
    LVL 15

    Accepted Solution

    Did you add a # to databind the CommandArgument?  I left it out by mistake.

    <asp:LinkButton runat="server" Text='<%# Eval("URL") %>' CommandName="URLCLICK" CommandArgument='<%# Eval("URL") %>' />

    I've tested it on my end and it works fine.

    If e.CommandName = "URLCLICK" Then
                Session("theUrl") = e.CommandArgument
    End If

    Author Closing Comment


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    A short article about a problem I had getting the GPS LocationListener working.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now