views:

449

answers:

7

Hey guys... I got this code in order to build an url for the link using a querystring from the current page. The problem is.... It doens't work. Any suggestions?

<asp:hyperlink ID="link1" runat="server" NavigateUrl='<%@("Equipamentos.aspx?ID_Cliente=")+Request.QueryString    ("ID_Cliente").trim.tostring()%>'>Equipamentos</asp:HyperLink>
A: 

Try this instead :

<asp:hyperlink ID="link1" runat="server" 
  NavigateUrl='<%= ("Equipamentos.aspx?ID_Cliente=") 
  + Request.QueryString("ID_Cliente").Trim().ToString() %>'>
  Equipamentos</asp:HyperLink>
Canavar
Why downvote without an explanation ?
Canavar
Why downvote without an explanation ? Exactly, I have done this way in many places in my project.
Shiva
+1  A: 
mgroves
Thank you very much... At least someone nice with a useful answer
v3ga
The "ToString()" at the end is when you are not sure that Trim() returns String?:)
Kamarey
RolandTumble
A: 

The

<%@ %>

tags are for directives, such as registering controls. You need a

<%= %>

tag, which is called a code evaluation block.

Something like

<%= (5+5).ToString() %>

is what you need - try your code in there.

JoshJordan
I have tried that... it doesn't...and also with de %#
v3ga
A: 

You are not going to be able to set the NavigateUrl of the link in this way. Try something like this:

<asp:hyperlink 
    ID="link1" 
    runat="server">Equipamentos</asp:HyperLink>

And then in your codebehing or a script tag do this:

link1.NavigateUrl = "Equipamentos.aspx?ID_Cliente=" 
    + Request.QueryString("ID_Cliente").Trim().ToString();
Andrew Hare
A: 

Same question - Do you mean it crashes with an error message or gives you a link but not the one you want ?

excalibur
+1  A: 

Your ASP.NET code should look like this:

<asp:HyperLink ID="link1" runat="server" NavigateUrl=''>Equipamentos</asp:HyperLink>

And then add this in code behind:

this.link1.NavigateUrl = string.Format("Equipamentos.aspx?ID_Cliente={0}", Request.QueryString["ID_Cliente"].Trim());
Nelson Reis
+1 for good string formating.
Phil
A: 

As I know you can't use "<%= %>" with server controls. So you can:

1. Leave it as a server control and follow Andrew Hare's (or similar) answer.
2. Use client control: "<a />" and "<%= %>" should work.
Kamarey