Posted: 6/27/2011
I need one of the fields in my SQL DB to hold hyperlinks that will be clickable (executable) when they are rendered in the GridView.
1. Is there a certain data type that the column has to be?
2. Does the link have to be inserted into the DB in a certain format? ie http://www.test.aspx.com
3. What are the steps I have to take so I can insert a hyperlink in my DB and when it is redered in the GridView to the end user they can just click the link and navigate to the url?
I made a column using the varchar(225) and in the GridView its clickable but, I get a HTTP 404 The Resource cannot be found.
Thanks in advance.
I do not know what the deal is. I will try to address a "problem" I am having. Sometimes for days. Then after I cannot reference a book, find a reference on the internet, or an answer to a previous post to solve my problem. I will post her e asking the question. Then for sure I figure it out somewhere in between posting the question and getting my first reply. I guess I should be greatful. Its better then being forever stuck.
I thought I would post the solutiion to my question.
Inside my Gridview I added and asp:Templated field. Then inside that and Item template. Then inside that an asp:hyperlink.
Here is my code:
<asp:TemplateField> <ItemTemplate> <asp:HyperLink ID="WebLink" runat="server" NavigateUrl='<%#Eval("Coupons") %>' Target="_work" Text='<%# Eval("Coupons") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField>
In my DB I had a column just for my WebLikns. The data type I used is varchar(255). You have to format your entry: http://www.webaddress.com
I also tried this in detailsview and it worked fine.
Hope this helps anyone who gets stuck on this like me.
Hi Mike,
Try to use
<a href='<%# Eval("Coupons") %>'><%# Eval("Coupons") %> </a>
By the way <a> </a> is slightly quicker that using a Server Control such as an ASP Hyperklink.
Best Regards,
Gjorgji
Thanks again! I sure like faster! I'll give it a try.
Have a good one.
Mike Riemer said: Is there a certain data type that the column has to be?
Is there a certain data type that the column has to be?
Mike Riemer said: I made a column using the varchar(225)
I made a column using the varchar(225)
I don't think 225 will serve you. The length of URLs can be more and from WWW FAQs: What is the maximum length of a URL?Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browserand for IE8 and IE9 from here Maximum URL length is 2,083 characters in Internet Explorer Microsoft Internet Explorer has a maximum uniform resource locator (URL) length of 2,083 characters. Internet Explorer also has a maximum path length of 2,048 characters. This limit applies to both POST request and GET request URLs. So IMHO you can go with Varchar(MAX) (to be totally safe) field or Varchar(2048) (being practical to what actually the length of URLs can be) . But hold on, if you are expecting Non-ASCII characters, chinese language characters etc in the URL, for that VARCHAR is not the one for you, you need to go with NVARCHAR for it. So to be safe in all cases go with NVARCHAR(MAX).And here is the code for you:SQL:
USE [Test] GO /****** Object: Table [dbo].[UserTable] Script Date: 06/27/2011 12:00:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[MyTable] ( [ID] [int] IDENTITY(1, 1) NOT NULL , [HyperLink] [nvarchar](MAX) NULL ) ON [PRIMARY] GO INSERT INTO [dbo].[MyTable] ( [HyperLink] ) SELECT 'http://www.google.co.in' UNION SELECT 'http://www.bing.com/' UNION SELECT 'http://www.yahoo.com/' GO CREATE PROC [GetItems] AS BEGIN SELECT ID , HyperLink FROM [MyTable] END GO
ASPX:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="HtmlForm" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:HyperLink ID="WebLink" runat="server" NavigateUrl='<%#Eval("HyperLink") %>' Target="_work" Text='<%# Eval("HyperLink") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form> </body> </html>
Server side:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = GetItems(); GridView1.DataBind(); } } private List<MyClass> GetItems() { var items = new List<MyClass>(); string connectionString = "Data Source=Raghav-PC; Initial Catalog=Test; User ID=test; Password=test"; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand()) { command.Connection = connection; command.CommandText = "GetItems"; command.CommandType = CommandType.StoredProcedure; connection.Open(); using (SqlDataReader dataReader = command.ExecuteReader()) { MyClass item; while (dataReader.Read()) { item = new MyClass { ID = (int) dataReader["ID"], HyperLink = Convert.ToString(dataReader["HyperLink"]) }; items.Add(item); } } } } return items; } public class MyClass { public int ID { get; set; } public string HyperLink { get; set; } }
Posted: 6/28/2011
Thanks for all the help. Everything you guys said works great.