Answered by:
how to let the url in gridview control become hyperlink

Question
-
User-1460471841 posted
In my gridview control there is one column, this column is the url for videos. i want to let the url become hyperlink,
now the url in gridview cannot click, I want it can be click and open the video page.
how can I do it?
Saturday, August 27, 2016 1:41 PM
Answers
-
User283571144 posted
Hi mengcp,
In my gridview control there is one column, this column is the url for videos. i want to let the url become hyperlink,
now the url in gridview cannot click, I want it can be click and open the video page.
According to your description, I suggest you could use TemplateField in gridview and add HyperLink control into TemplateField 's ItemTemplate.
More details about how to use this, you could refer to follow codes and link:
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" > <Columns> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="url" SortExpression="url"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("url") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("url") %>' Text='<%# Bind("url") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { DataTable d1 = new DataTable(); d1.Columns.Add("ID"); d1.Columns.Add("url"); d1.Rows.Add("1", "http://www.google.com"); d1.Rows.Add("2", "http://www.facebook.com"); GridView1.DataSource = d1; GridView1.DataBind(); }
Link:https://msdn.microsoft.com/en-us/library/bb288032.aspx
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 28, 2016 2:09 AM -
User283571144 posted
HI mengcp,
however I click "start debugging" button there is one error it said that:
"asp.net development server failed to start listening on port 1032. "
and "unable to connect to visual studio's localhost web server"
I think the reason is the port is not available.
So I suggest you could change the port for the web development server and it will work.
Go to the Solution Explorer in Visual Studio, click on the web site application, hit ‘F4’ key to bring up the application’s property window.
In this property window, set ‘Use Dynamic Port’ to ‘False’, and you will be able to set the Port number to a number other than the previous port number.
More details, you could refer to follow link:
https://msdn.microsoft.com/en-us/library/ms178109(v=vs.140).aspx
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 28, 2016 10:18 AM -
User283571144 posted
Hi mengcp,
Please can you give me VB language for that code behind that you write.You could refer to follow codes:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim d1 As New DataTable() d1.Columns.Add("ID") d1.Columns.Add("url") d1.Rows.Add("1", "http://www.google.com") d1.Rows.Add("2", "http://www.facebook.com") GridView1.DataSource = d1 GridView1.DataBind() End Sub
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 28, 2016 11:25 AM
All replies
-
User283571144 posted
Hi mengcp,
In my gridview control there is one column, this column is the url for videos. i want to let the url become hyperlink,
now the url in gridview cannot click, I want it can be click and open the video page.
According to your description, I suggest you could use TemplateField in gridview and add HyperLink control into TemplateField 's ItemTemplate.
More details about how to use this, you could refer to follow codes and link:
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" > <Columns> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="url" SortExpression="url"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("url") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("url") %>' Text='<%# Bind("url") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { DataTable d1 = new DataTable(); d1.Columns.Add("ID"); d1.Columns.Add("url"); d1.Rows.Add("1", "http://www.google.com"); d1.Rows.Add("2", "http://www.facebook.com"); GridView1.DataSource = d1; GridView1.DataBind(); }
Link:https://msdn.microsoft.com/en-us/library/bb288032.aspx
Result:
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 28, 2016 2:09 AM -
User-1460471841 posted
dear Brando
I try to use edit column on gridview control. and then add "hyperlinkfield" to selected fields.
then I send hyperlinkfeild properties. there is one property "datatextfield" I choose the "videourl" (the column that store video urls in my table).
finally I click ok, it is same perfect until now.
however I click "start debugging" button there is one error it said that:
"asp.net development server failed to start listening on port 1032. "
and "unable to connect to visual studio's localhost web server"
How can I solve it?
Sunday, August 28, 2016 4:56 AM -
User283571144 posted
HI mengcp,
however I click "start debugging" button there is one error it said that:
"asp.net development server failed to start listening on port 1032. "
and "unable to connect to visual studio's localhost web server"
I think the reason is the port is not available.
So I suggest you could change the port for the web development server and it will work.
Go to the Solution Explorer in Visual Studio, click on the web site application, hit ‘F4’ key to bring up the application’s property window.
In this property window, set ‘Use Dynamic Port’ to ‘False’, and you will be able to set the Port number to a number other than the previous port number.
More details, you could refer to follow link:
https://msdn.microsoft.com/en-us/library/ms178109(v=vs.140).aspx
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 28, 2016 10:18 AM -
User-1460471841 posted
dear brando,
Please can you give me VB language for that code behind that you write.
thank you!
Sunday, August 28, 2016 10:42 AM -
User-1460471841 posted
this answer is very perfect for that error message I met.
Sunday, August 28, 2016 10:44 AM -
User283571144 posted
Hi mengcp,
Please can you give me VB language for that code behind that you write.You could refer to follow codes:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim d1 As New DataTable() d1.Columns.Add("ID") d1.Columns.Add("url") d1.Rows.Add("1", "http://www.google.com") d1.Rows.Add("2", "http://www.facebook.com") GridView1.DataSource = d1 GridView1.DataBind() End Sub
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 28, 2016 11:25 AM