gridview hyperlinkfield url path issue - c#

lets say i have the following link stackoverflow.com stored in db.
When i click the link from grid-view it redirect me to below path
http://localhost:30987/Main/stackoverflow.com
Note I am taking user input so i cant add http:// to user input because i cant determine the user website use HTTP or HTTPS
How can i solve this issue ?
<asp:TemplateField HeaderText="Website" SortExpression="Website">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Target="_blank" Text='<%# Bind("Websitelink") %>' NavigateUrl='<%# Bind("Websitelink") %>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

As like Andrei mentioned in the comment, use HtmlAnchor(<a>) instead for <asp:HyperLink to make it work. You code will looks like the following:
<asp:TemplateField HeaderText="Website" SortExpression="Website">
<ItemTemplate>
<%#Eval("Websitelink") %>
</ItemTemplate>
</asp:TemplateField>

Related

Get redirect to another page from Hyperlink button in gridview

I have one gridview in which i'm binding a List of Website names like
www.google.com
www.facebook.com
www.gmail.com
www.google.com
www.facebook.com
www.gmail.com
Now, I want to redirect to www.google.com when i click on this
I used this one but its not working
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Website", #"{0:hh\:mm}") %>' Text='<%# Eval("Website", #"{0:hh\:mm}") %>' Target="_blank"></asp:HyperLink>
You should include "http://" before your redirect link.Please go through the below example,
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Website","http://{0}") %>' Text='<%# Eval("Website") %>' Target="_blank"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
OR
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Website") %>
</ItemTemplate>
</asp:TemplateField>
Make sure the NavigateUrl has http:// or https:// in it, otherwise the browser will think it is a link in the same domain.
NavigateUrl='<%# "https://" + Eval("Website", #"{0:hh\:mm}") %>'
Or if some website do have that http in them but others don't, you can check for that first
NavigateUrl='<%# Eval("Website").ToString().Contains("http") ? Eval("Website") : "https://" + Eval("Website") %>'

Popup window from asp:Hyperlink with parameter in c#

I need open in my GridView the asp:Hyperlink with parameter to window popup.
Here is a working sample for a popup window with parameter, but I have error.
The error message says:
Server tag is not correctly formatted.
What's the problem ?
How to do resolve this ?
Can you help me ?
Thank you in advance.
My code below.
<asp:TemplateField HeaderText="btest">
<ItemTemplate>
<asp:HyperLink runat="server" ID="btest" Text="btest"
NavigateUrl='<%#"javascript:_popupWin=window.open('btest.aspx?Sample_ID=" + Eval("Sample_ID") + "',
'_popupWin','width=300,height=300,resizable=yes,location=yes,scrollbars=yes');_
popupWin.focus();" %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Try this :
<asp:TemplateField HeaderText="btest">
<ItemTemplate>
<asp:HyperLink runat="server" ID="btest" Text="btest"
NavigateUrl='<%# String.Format("btest.aspx?sID={0}", Eval("Sample_ID"))%>'
onclick="javascript:w= window.open(this.href,'Sample_ID',
'left=20,top=20,width=1500,height=300,toolbar=0,resizable=0');return false;">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Dynamic HyperLink

I have a HyperLink along with other controls such as Label etc in a GridView. The Label's in the GridView are dynamically populated like this:
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
I am now trying to do something similar with HyperLink, as in:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='http://www.company.com?clientname=<%# Bind("ClientName") %>'>Client Name</asp:HyperLink>
This doesn't give me any errors, but the link becomes this:
http://www.company.com/?clientname=<%# Bind("ClientName") %>
instead of something like this:
http://www.company.com/?clientname=oshiro
Anyone know how to get the link to work properly instead of just outputing the asp.net code?
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("ClientName", "http://www.company.com/?id={0}") %>'>Client Name</asp:HyperLink>

HyperLink with NavigateUrl with Eval(). Where is the mistake?

First I was changing HyperLink.NavigateUrl in code-behind on Page_Load().
But after I decided to do it in design using Eval() method.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Eval("type"), Eval("id")) %>' Text="Refuse" />
or
<asp:HyperLink ID="urlRefuse" runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
where id and type - are variables from Request.
But it doesn't work. Only raw text 'Refuse' is shown. Where is my mistake? Thanks in advance.
this is working great
NavigateUrl='<%# Eval("type","~/Refuse.aspx?type={0}") %>'
This worked for me
NavigateUrl='<%# String.Format("{0}.aspx?ID={1}", DataBinder.Eval(Container.DataItem, "Category"), DataBinder.Eval(Container.DataItem, "Post_ID")) %>'
Try and ViewSource in your browser, what's being rendered to the client in your href? Is it what you expected?. If you are trying to use variables from the request collection you can't use Eval, you need to use the Request query string parameters.
<asp:HyperLink runat="server"
NavigateUrl='<%# String.Format("~/Refuse.aspx?type={0}&id={1}", Request["type"], Request["id"]) %>' Text="Refuse" />
Try this:
HttpUtility.UrlEncode(Eval("type")
Try this one:
<asp:HyperLink ID="HyperLink2" runat="server" onclick='<%# String.Format("AcceptUser({0},{1})",Eval("UserId"), Eval("TsId")) %>' NavigateUrl="javascript:void(0)" Visible='<%# (bool)Eval("CanDelete") %>'>Accept</asp:HyperLink>
Try this it worked for me:
Eval("type").ToString()

Send value of gridview row using hyperlink within it

I am not able to sent the value of the MachineID to another page using the hyperlink in gridview.
<!-- <asp:TemplateField HeaderText="FailedFiles"
SortExpression="NumFailedFilesOverSLA">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%#Bind("NumFailedFilesOverSLA") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I have tried putting
DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}"
but don't know why this is not working??
Please suggest...
thanks
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Inventory_ID", "/default.aspx?ID={0}") %>'
Text="Details"></asp:HyperLink>
</ItemTemplate>
This should solve your problem. This is exactly how I used it.
If this doesn't work, then check that you are actually getting a value back from the DB for MachineID:
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("NumFailedFilesOverSLA") %>'
runat="server" DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}">
</asp:HyperLink>
First, try to put the default gridview in the page and attach it to the data source, so you can test if there is data to display.
If you are assigning the data source from code behind don't forget to call the DataBind() method after that.

Categories

Resources