This is a migrated thread and some comments may be shown as answers.

GridHyperLinkColumn - Retrieve email address in code behind

3 Answers 292 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amy
Top achievements
Rank 1
Amy asked on 17 Mar 2011, 09:48 PM
Hi!  Could someone help me with getting the value of an email address in a GridHyperLinkColumn from the code behind please?

Here's the aspx code:
<telerik:GridHyperLinkColumn DataNavigateUrlFields="Email1" UniqueName="Email1"   
   DataNavigateUrlFormatString="mailto:{0}" DataTextField="Name" HeaderText="Reviewer" SortExpression="Email1" 
   ShowFilterIcon="false" ShowSortIcon="false" AutoPostBackOnFilter="true" />

Essentially when a user clicks on a submit button for the RadGrid itself, I would like to grab the email address for each selected row in the grid and send an automated email.

Here's the code behind I have for finding the selected rows in the grid, however, I'm not sure how to retrieve the value of the email address from the GridHyperLinkColumn...
If (RadGrid1.SelectedItems.Count > 0) Then
 
            For Each item As GridDataItem In RadGrid1.SelectedItems
                'Add code here to retrieve email address
            Next
End If

Any help would be greatly appreciated.  Thanks!

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Mar 2011, 06:02 AM
Hello Amy,

Try the following code snippet to get the value of HyperLinkColumn.

VB.NET:
If (RadGrid1.SelectedItems.Count > 0) Then
    For Each item As GridDataItem In RadGrid1.SelectedItems
        Dim link As HyperLink = DirectCast(item("Email1").Controls(0), HyperLink)
        Dim email As String = link.Text
    Next
End If

Thanks,
Princy.
0
Amy
Top achievements
Rank 1
answered on 18 Mar 2011, 01:28 PM
Princy,  thank you!  I was trying to combine the two lines of code into one, which obviously wasn't/wouldn't work.  ;-)

However, I'm only getting the user's name and not the email address.  The 'DataTextField' is set to show the user's name.  If you click on the user's name it does generate an email with the user's email address.  Is there a different field/property I should be pulling from to get the email address?

Btw, if I add a GridBoundColumn containing the email address (visibility set to false so it doesn't affect how the users see the grid), and then pull from that column in the code behind I can get the email address.  Is there a better way to do this using the GridHyperLinkColumn without having to add this extra column?

Thank you again for your help!
0
Princy
Top achievements
Rank 2
answered on 21 Mar 2011, 05:54 AM
Hello Amy,

Youcan try the following code snippet to get the email address.

VB.NET:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim rowview As DataRowView = DirectCast(item.DataItem, DataRowView)
        Dim email As String = rowview("Email1").ToString()
        'or
        Dim link As HyperLink = DirectCast(item("Email1").Controls(0), HyperLink)
        Dim url As String = link.NavigateUrl
        Dim email As String = url.Split(":"C)(1)
    End If
End Sub

Thanks,
Princy.
Tags
Grid
Asked by
Amy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Amy
Top achievements
Rank 1
Share this question
or