asp:HyperLink build NavigateUrl within Repeater using XPATH data - asp.net

I am using a repeater for some products I am listing.
I'm trying to build an asp:HyperLink NavigateUrl using both hardcoded text as well as XPATH data.
NavigateUrl='mypage.aspx?ID=<%#XPath("THEID")%>&name=<%#XPath("THENAME")%>'
Obviously this isn't working.
Does anyone know how to make this work?

This should work:
<asp:HyperLink
runat="server"
NavigateUrl='<%# string.Format("mypage.aspx?ID={0}&name={1}", XPath("THEID"), XPath("THENAME")) %>'
Text="some link"
/>

Related

If statement, or some conditional inside asp:repeater BuildLink (multiple evals)

I need to make some sort of conditional when the eval returns nothing. Currently it creates a link "My Link" That leads to nowhere when the url is blank. I would prefer the "My Link" to not show up at all when the url is blank.
I've tried to implement something like what is found in the first answer to this question...#Eval if statement in repeater but either the buildlink() or the multiple eval() statements are throwing me some errors.
Here is the code I currently have
<asp:HyperLink runat="server"
NavigateUrl='<%# BuildLink(Eval("TaskDefinition.Url").ToString(), Eval ("TaskInstanceID").ToString())%>'>
My Link
</asp:HyperLink>
you need to add visibility attribute
<asp:HyperLink runat="server"
NavigateUrl='<%# BuildLink(Eval("TaskDefinition.Url").ToString(), Eval("TaskInstanceID").ToString())%>'
Visible='<%# String.IsNullOrEmpty(BuildLink(Eval("TaskDefinition.Url").ToString(), Eval("TaskInstanceID").ToString())) %>'
>My Link
</asp:HyperLink>

What is wrong with my Hyperlink

<asp:HyperLink ID="TestHyperLink" runat="server" NavigateUrl='<%# "javascript:updateApplet();" %>' Text="Send" />
It says "The server tag is not well formatted."
Please help.
Missing ID Before "TestHyperLink"
<asp:HyperLink ID="TestHyperLink" runat="server" NavigateUrl='<%# "javascript:updateApplet();" %>' Text="Send" />
There are so many wrong things, have you ever, at least, bother to look in Google ?
<asp:HyperLink
id="TestHyperLink"
runat="server"
NavigateUrl="#"
OnClientClick="updateApplet()"
Text="Send" />
but for simple things like this, you don't need an ASP.NET Control, unless you are doing something from your code file... a simple <a> will do the trick.
Just understand that ASP.NET Controls are useful when we want to tweak or use then in our application as variables so we can control them dynamically. If we simply need a link that will never change, there is no need for a control, a html tag will do.

Eval property and extra text on a button's text property

I'm using an asp Button like follows:
<asp:Button id="someButton" runat="Server" Text='<%# Eval("Size") %>' />
This is great but before the Eval Property result I wanted to add the Text "Download " with possibly the units after it.
eg. Download 123KB.
Can someone please tell me how to go about this?
In this case it might be easier to set the text property of the button on the server in the page load event.
insted of button use linkButton.
**asp:LinkButton ID="HyperLink2" OnClick="HyperlinkSelectedCustomers_Click" runat="server" CommandArgument='<%#Eval("id") %>' ><%#Eval("selectedCustomers") %>/asp:LinkButton

Build a HyperLink in a GridView in ASP.NET webforms - Eval not working

I have a gridview with a template field that has a HyperLink:
<asp:TemplateField ItemStyle-Width="12%" HeaderText="VER" HeaderStyle-HorizontalAlign="Center" SortExpression="Ver" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" NavigateUrl="~/Admin/Teste/Teste.aspx?rac=<%#Eval('idApontamento')%>" runat="server">TEXT</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I am getting The server tag is not well formed. in the HyperLink line.
What should I do in order to directly build a querystring in a HyperLink ?
Build your hyperlink like this:
<asp:HyperLinkField HeaderText="Title"
DataTextField="Some Text"
DataNavigateUrlFields="idApontamento,CustomerID"
DataNavigateUrlFormatString="~/Admin/Teste/Teste.aspx?rac={0}&CustomerID={1}" />
Keep adding comma delimited values to the DataNavigateUrlFields property, and markup the DataNavigateUrlFormatString as you would string.Format()
I don't think you can embed an expression like that, you have to pick to give it all text, or all binding expression.
Thankfully, you can contatonate string in a binding expression. Try something like this:
NavigateUrl='<%# String.Concat("~/Admin/Teste/Teste.aspx?rac=", Eval("idApontamento")) %>'
You've got an extra double-quote after the pound (#) symbol. Does removing that help?

Dynamically assigning properties on a non data bound ASP.NET control

For example, let's say I have a HyperLink:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl="foo.aspx" />
How can I set the NavigateUrl on the server side, without having to go the code-behind?
This doesn't work of course:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl="<%= urlString %>" />
(where urlString might be a string I created earlier in the page)
And this doesn't work because the HyperLink is not within a data bound control:
<asp:HyperLink runat="server" Text="Foo" NavigateUrl='<%# urlString %>' />
I guess I could just use a standard anchor element:
Foo
But I'd rather not mix up HTML and ASP.NET controls, and it would be handy to be able to do this for other controls.
Surely there must be a way?
Try setting the property in an inline code block:
<asp:HyperLink runat="server" ID="MyLink" Text="Foo" />
<% MyLink.NavigateUrl="foo.aspx"; %>
This doesn't work of course:
Of course it does.
And this doesn't work because the
HyperLink is not within a data bound
control:
Consider the Page as your data bound control.
You need to calls it's DataBind method.
Page.DataBind();
Maybe you need to add an ID attribute as well. If that does not work, try to display a property instead of a variable.

Resources