2

I added this template field to a gridview column and need to access the value, an email address, from the column in code behind. I initially added a DataKeyNames, but this only pulls the first record value. It does not seem to select the value for each record when running through a loop.

I would like to add the email to a label so that I can perhaps use a FindControl statement, unless someone knows of an easier way. I cannot get the email hyperlink to show up in the label. Works fine without the label tag except for not being able to read the email address.

<asp:TemplateField HeaderText="Email">
    <ItemTemplate>      
        <a href="mailto:<%# Eval("email") %>"><%#Eval("email")%> </a>     
    </ItemTemplate>
</asp:TemplateField>

Tried variations of:

<asp:TemplateField HeaderText="Email">
    <ItemTemplate> 
        <asp:Label ID="Email99" runat="server" <a href="mailto:<%# Eval("email") %>"><%#Eval("email")%> </a> ></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Doesn't show any errors in the inline code, simply reports: Parser Error Message: The server tag is not well formed.

UPDATE: Here is the ASP after adding HyperLinkField to columns

<Columns>     
   <asp:BoundField DataField="usersLogonName" HeaderText="Logon Name" >
       <ControlStyle Width="50px" />
   </asp:BoundField>
   <asp:BoundField DataField="userDBLanguage" HeaderText="Language" >
       <ControlStyle Width="30px" />
   </asp:BoundField>

   <asp:HyperLinkField runat="server" DataNavigateUrlFields="email" DataNavigateUrlFormatString="mailto:{0}" DataTextField="email" />     

   <asp:BoundField DataField="LastActivityDate" HeaderText="Last Activity" />                                                                                                              
</Columns>

This row of code reads the value from the Templated Field solution provided by James Johnson...

 Dim emailAdd As String = GridView4.DataKeys(dr.RowIndex)("Email")

He deserves credit if this thread ever gets unlocked.

This is an ASP page using VB.net

Thanks for any help.

htm11h
  • 1,739
  • 8
  • 47
  • 104

1 Answers1

4

Use the HyperLinkField:

<asp:GridView ID="GridView1" runat="server" ...>
    <Columns>
        <asp:HyperLinkField DataNavigateUrlFields="email" DataNavigateUrlFormatString="mailto:{0}" DataTextField="email" />
    </Columns>
</asp:GridView>

If you're having problems with the above, you can just use a TemplateField with a HyperLink control in it:

<asp:TemplateField HeaderText="Email"> 
    <ItemTemplate>
        <asp:HyperLink runat="server" Text='<%# Eval("email") %>' NavigateUrl='<%# string.Format("mailto:{0}", Eval("email"))%>' />
    </ItemTemplate>
</asp:TemplateField>
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • okay, I fixed that part but my original issue stands. In code behind I need to read that email address in the hyperlink while looping through the rows. How can I get that email address value? – htm11h Apr 23 '12 at 19:18
  • Use the datakey collection, and you can get the email address by the row index. Give me one second, and I'll update my example. – James Johnson Apr 23 '12 at 19:20