Hi, I'm just getting into ASP.net webforms. I have a table in the database with customer's names and addresses. I have successfully casted two of the columns together with the name of the company and their address like this:
What I am trying to do is get just the address hyperlinked but not the business name. How do I break up the string so that only the address is hyperlinked like this: Bob's Plumbing
123 Anystreet, Thistown, State, 12345
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("address", typeof(string));
RickL66
Member
8 Points
7 Posts
Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 04:26 PM|LINK
Hi, I'm just getting into ASP.net webforms. I have a table in the database with customer's names and addresses. I have successfully casted two of the columns together with the name of the company and their address like this:
(business.name + ' ' + address.line1 + ' ' + address.line2 + ' ' + address.city + ' ' + address_state.postal + ' ' + cast(address.zip_code as CHAR)) as address
and in the asp:HyperLinkField I have this:
<asp:HyperLinkField DataNavigateUrlFields="address" DataNavigateUrlFormatString="https://maps.google.com/maps?q={0}" Target="_blank" DataTextField="address" ItemStyle-Width="10%" HeaderText="Name/Location" />What I am trying to do is get just the address hyperlinked but not the business name. How do I break up the string so that only the address is hyperlinked like this: Bob's Plumbing 123 Anystreet, Thistown, State, 12345
Thanks,
Rick
Vipindas
Contributor
5514 Points
810 Posts
Re: Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 04:44 PM|LINK
Use gridview templatefield
<asp:TemplateField HeaderText="Name/Location"> <ItemTemplate> <%#Eval("CustomerName") %> <asp:HyperLinkField Id="Hyperlink1" runat="server" NavigateUrl='<%#"https://maps.google.com/maps?q=" + Eval("address") %>' Target="_blank" Text='<%#Eval("address") %>' /> </ItemTemplate> </asp:TemplateField>Refer this
http://www.mandsconsulting.com/asp-net-hyperlink-in-gridview-using-hyperlinkfield-sqldatasource-boundfield-templatefield-and-hyperlink
http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 04:44 PM|LINK
Well if you are trying to get the address and all that to be part of your address argument, try it this way:
akshay22
Participant
914 Points
184 Posts
Re: Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 04:56 PM|LINK
Hi,
Try getting your result in two different columns instead of one, say "name" and "address" and bind it to grid like in the below sample.
HTML
<asp:GridView ID="gvData" runat="server"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%#Eval("name") %>'></asp:Label> <asp:HyperLink ID="lnkAddress" runat="server" Text='<%#Eval("address") %>' NavigateUrl="http://google.com"></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("address", typeof(string));
DataRow dr = dt.NewRow();
dr[0] = "Bob's Plumbing";
dr[1] = "123 Anystreet, Thistown, State, 12345";
dt.Rows.Add(dr);
gvData.AutoGenerateColumns = false;
gvData.DataSource = dt;
gvData.DataBind();
}
}
Hope this helps.
My Blog: Akshay's Notion
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 04:58 PM|LINK
The method akshay22 says is probably the estiest and best implementation.
RickL66
Member
8 Points
7 Posts
Re: Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 08:10 PM|LINK
I like your method, and I got this almost working. However, all links point to http://google.com . How do I change it so that this string:
NavigateUrl="http://maps.google.com" contains the address? I tried this:
NavigateUrl="http://maps.google.com/maps?q='<%#Eval("address") %>'" but that throws an error.
Basically, the Url should read "http://maps.google.com/maps?q=123 Anystreet, Thistown, State, 12345"
RickL66
Member
8 Points
7 Posts
Re: Gridview with two SQL columns, one needs to be hyperlinked
Dec 21, 2012 08:23 PM|LINK
That worked for me. I did have to take part of the string from Vipindas so the address is part of the URL like this:
NavigateUrl='<%#"https://maps.google.com/maps?q=" + Eval("address") %>'