Dynamic HyperLink - c#

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>

Related

gridview hyperlinkfield url path issue

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>

window.open() in <asp:Hyperlink> in GridView passing id

I am using <asp:HyperLink> in Gridview
<asp:HyperLink ID="hyperlink1" runat="server" ImageUrl="~/images/zoom.png" OnClick="javascript:window.open('ProductSummary.aspx?id=', 'open_window', 'width=640, height=480, left=0, top=0')"></asp:HyperLink>
I need to know that how to pass '<%# Eval("ProductId") %>' in query string
Probably the easiest way is to produce the entire OnClick event in the DataSource for the grid view, and bind to that:
<asp:HyperLink ID="hyperlink1" runat="server" ImageUrl="~/images/zoom.png"
OnClick='<%# Eval("PopupClickEvent") %>' ... />
Otherwise, it should still be possible to have
OnClick='<%# Eval("ProductID", "javascript:window.open('ProductSummary.aspx?id={0}', ...)") %>'
But the quotation escaping gets messy and confusing very quickly.
For reference, see http://msdn.microsoft.com/en-us/library/4hx47hfe(v=vs.110).aspx

ASP.net putting a dataitem in a repeater

I've got a:
Data repeater
Control in the repeater
And I'd like to put some values into the controls. At the moment I have a control in it as:
<asp:Repeater runat="server" ID="ArchiveEntryRepeater">
snip
<asp:HyperLink ID="HyperLink1" ToolTip="Comment on Some Entry Title Here" runat="server" NavigateUrl="~/Blog/7/How-to-know-when-this/Comments">
<strong><%# DataBinder.Eval(Container.DataItem, "Comments")%></strong> Comments
</asp:HyperLink>
snip
</asp:Repeater>
Which works fine, but I want to put
<%# DataBinder.Eval(Container.DataItem, "Title")%>
Into the NavigateURL property of the Hyperlink. Just putting it in doesn't seem to work!
Try enclosing your NavigateURL in apostrophes instead of double quotes to not confuse the ASP.NET parser:
<asp:HyperLink runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Title")%>' [...]>[...]</asp:HyperLink>
This might be your issue.
You can always bind on the back end using the event itemdatabound. It has some advantages especially where the data is not a neat view or needs some type of major manipulation.
Another option would be to loose the control and replace it with an html anchor control like this:
<asp:Repeater>
<ItemTemplate>
<a id="HyperLink1" title="Comment on Some Entry Title Here" href='<%# DataBinder.Eval(Container.DataItem, "Title")%>'
style="font-weight: bold">'<%# DataBinder.Eval(Container.DataItem, "Comments")%>'</a>
</ItemTemplate>
</asp:Repeater>
Once the server renders a hyperlink to the client there is generally no reason to have it "runat='server'" . The purpose is to navigate to another page so the overhead of using an asp:HyperLink vs. an nchor is wasted.

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