Mailto link with data from SQLDataSource

G

Guest

Hi,

I want to create a mailto link from data coming from an SQLDataSource. How
do I have to change the following code to get it working? The mailto works,
outlook starts with a new message, but the email address has not been filled
in because I don’t know how to pass the email variable into the mailto
command.

<asp:HyperLink runat="server">
<A href="mailto:">
<asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email")
%>'></asp:Label><br />
</asp:Hyperlink>
</A>

Thanks in advance,
Regards,
Peter
 
G

Guest

Peter,

The Bind() and Eval() functions take an optional format string that can be
used to format the data that is bound to a target property. In your case you
can format the NavigateUrl on a Hyperlink control to be something like
"mailto:[email protected]". Change your Bind(..) or Eval(..) call to
Bind("Email", "mailto:{0}"). The token {0} in the format string is replaced
the value of "Email" from your SqlDataSource.

Also, binding to SqlDataSource only works with server controls that support
Data Binding (Server controls that have a DataSourceID, such as the GridView,
FormView, DetailsView, DropDownList, CheckBoxList, BulletedList, etc.).

Here is an example of a GridView that has a bound mailto column using a
Hyperlink control.

Hope this helps,
Jason Vermillion

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Binding Using a format string">
<ItemTemplate>
<asp:HyperLink ID="lnkEmail" runat="server" NavigateUrl='<%#
Eval("Email", "mailto:{0}") %>' Text='<%# Eval("Email", "Send an Email to
{0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
 
G

Guest

Thanks a lot Jason. It works.
Regards,
Peter

Jason Vermillion said:
Peter,

The Bind() and Eval() functions take an optional format string that can be
used to format the data that is bound to a target property. In your case you
can format the NavigateUrl on a Hyperlink control to be something like
"mailto:[email protected]". Change your Bind(..) or Eval(..) call to
Bind("Email", "mailto:{0}"). The token {0} in the format string is replaced
the value of "Email" from your SqlDataSource.

Also, binding to SqlDataSource only works with server controls that support
Data Binding (Server controls that have a DataSourceID, such as the GridView,
FormView, DetailsView, DropDownList, CheckBoxList, BulletedList, etc.).

Here is an example of a GridView that has a bound mailto column using a
Hyperlink control.

Hope this helps,
Jason Vermillion

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Binding Using a format string">
<ItemTemplate>
<asp:HyperLink ID="lnkEmail" runat="server" NavigateUrl='<%#
Eval("Email", "mailto:{0}") %>' Text='<%# Eval("Email", "Send an Email to
{0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
 

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

Top