ASP.NET Querystrings - asp.net

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>

Gah, my eyes! Try doing this in code behind instead:
link1.NavigateUrl = "Equipamentos.aspx?ID_Cliente=" & Request.QueryString("ID_Cliente").Trim().ToString()
You have to use "&" instead of "+" because this is VB.NET, not C#.

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());

Try this instead :
<asp:hyperlink ID="link1" runat="server"
NavigateUrl='<%= ("Equipamentos.aspx?ID_Cliente=")
+ Request.QueryString("ID_Cliente").Trim().ToString() %>'>
Equipamentos</asp:HyperLink>

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.

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();

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.

Related

use variable in mailto hyperlink in asp.net

I want to use a variable declared in my .cs page(C#) as cc to mailto, but dont know how to use. Please help.
In default.aspx.cs page:
var code=cc#test.com;
In default.aspx page:
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="mailto:to#test.com?subject=Hi&cc=code" ToolTip="Submit" Target="_blank">mail</asp:HyperLink>
This is not a good way to do this but you can set the NavigateUrl from code behind.
Try this:
HyperLink5.NavigateUrl="mailto:to#test.com?subject=Hi&cc="+code;
Have you tried embeding code like
<asp:HyperLink ID="HyperLink5" runat="server" NavigateUrl="mailto:to#test.com?subject=Hi&cc="<%=code%> ToolTip="Submit" Target="_blank">mail</asp:HyperLink>

asp:HyperLink run-time modification

<asp:HyperLink ID="HyperLink5" runat="server" CssClass="RegAgenda"
NavigateUrl='<%#"http://www.mysite.com/store/" &
DataBinder.Eval(Container.DataItem,"DESCRIPTION")& "?ALID=" &
DataBinder.Eval(Container.DataItem,"TransID")%>'
Target="_blank" Text="Info/Buy Now">
</asp:HyperLink>
In the above asp:HyperLink code, I would like to test if the current page has Chuy2 in the path and if so, change the base url to http://www.mysite2.com
how would I do that? I am a PHP guy and don't know ASP.Net.
Something like this should work:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# string.Format("{0}/{1}?alid={2}", Request.Url.AbsoluteUri.Contains("Chuy2") ? "http://www.mysite2.com" : "http://www.mysite.com/store", Eval("Description"), Eval("TransID")) %>'
rather than try to piece this together in the markup/template, I would build the url in the code behind, or view model and then bind that value. the markup might look like this
NavigateUrl='<%#Eval("Url")%>'

Using fixed text together with <%# when databinding in Asp.NET doesn't work

This works:
<asp:HyperLink ID="EditGridItemLink" runat="server" NavigateUrl="<%# GetCustomUrl() %>">
link
</asp:HyperLink>
link value = http://localhost/MyCustomUrl.aspx
This doesn't:
<asp:HyperLink ID="EditGridItemLink" runat="server" NavigateUrl="subfolder/<%# GetCustomUrl() %>">
link
</asp:HyperLink>
link value = http://localhost/subfolder/<%# GetCustomUrl() %>
I'm doing this in a Column/TemplateField of an Asp.NET GridView.
Am I doing something wrong or is this simply impossible.
Of course I'm using a workaround now where I set the rest of the NavigateUrl value as well, but I was wondering why this isn't working...
You can do this instead: NavigateUrl='<%# string.Format("subfolder/{0}", GetCustomUrl()) %>'.
Databinding expressions for properties of server controls must be the only thing in the property (no mixing with static text in the way you tried).
You should put the whole expression inside the databinding markup:
<asp:HyperLink ID="EditGridItemLink" runat="server"
NavigateUrl="<%# "subfolder/" + GetCustomUrl() %>">
link
</asp:HyperLink>

problem assigning declarative values in asp:hyperlink. error: this is not scriptlet. will output as plain text

I am trying to do this:
<asp:HyperLink NavigateUrl='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>' runat="server" Text='<%= GetProfileImage(WebContext.CurrentUser.AccountId) %>'></asp:HyperLink>
But am getting the error:
this is not scriptlet. will output as
plain text.
when I mouse over my declarative statements.
Any ideas? Thanks.
You cannot use <%= ... %> literals to set properties of server-side controls.
Instead, you can use a normal (client-side) <a> tag, like this:
<%= GetProfileImage(WebContext.CurrentUser.AccountId) %>
If GetProfileImage doesn't return HTML tags, make sure to escape it.
You can use data binding syntax <%# %>. Just be sure that your hyperlink is either in a databound control, such as a ListView item template, or that you explicitly call DataBind() on the control from code-behind.
You can still populate an <asp:HyperLink> if you provide the ID and runat="server" properties. You can then set any property of the HyperLink from code-behind.
ASP Code:
<asp:HyperLink ID="myLink" runat="server"/>
Code-behind:
public void Page_Init()
{
myLink.NavigateURL = WebContext.RootUrl + WebContext.CurrentUser.UserName;
myLink.Text = GetProfileImage(WebContext.CurrentUser.AccountId);
}
<a href='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>'><%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>

How do I concatenate 2 resource strings together in an aspx page

I have a localised ASP.net application (.net 2.0). I wish to concatenate 2 strings retrieved from the resource file together into one element, something like this.
Text="<%$ Resources:Resource, lw_name %>" + <%$ Resources:Resource, lw_required %>"
I have tried using Eval without success. Is what I am trying to do the "correct" approach or can I store strings with placeholders in the resource file and interpolate them "on the fly".
I am trying to do this in the aspx file rather than in code-behind.
ASP.NET tag attribute values that use <%$ Something: Something Else %> are of a special syntax called ASP.NET Expressions. Using them as attribute values are pretty much all-or-nothing; there's no way to add any code into the ASPX file to manipulate what those expressions evaluate to. You'll have to do this in the code-behind.
I search for the solution so long
This code works for me:
ToolTip='<%# Resources.Global.Btn_Edit + "/" + Resources.Global.Btn_contact %>'
< asp:HyperLink ToolTip='<%# "Some Text :" + Eval("id").ToString() %>' ....../>
Do you mean something like this.... ToolTip='...' -> Convert your return values to STRING... ( xxxx.ToString() )
Like this it displays: Some Text: 1234 --> on Tooltip
so you should do something like this in your case:
Text="<%$ (Resources:Resource, lw_name).ToString() %>" + <%$ (Resources:Resource, lw_required).ToString() %>"
I don't know if it's going to work but try to conver to ToString().
I know you said you tried eval but what about something like this:
Text='<%# string.Format("{0}{1}",Eval("lw_name"),Eval("lw_required")) %>'
I was having the same issue, and I solved it by using this option instead:
Text="<%= HttpContext.GetGlobalResourceObject("Resource", "lw_name") %> <%= HttpContext.GetGlobalResourceObject("Resource", "lw_required") %>"
For local Resources, use the GetLocalResourceObject method instead of GetGlobalResourceObject
Try
"#(Resources.ResourceString + Resources.ResourceString)"
Use this method to append 2 strings in ASPX.
Text='<%# String.Format("{0} {1}",
Resources.file01.string1,Resources.file01.string2)%>'
This Might Be of Help
<asp:Label ID="Mylabel" runat="server">
<%= "before Message String- "+ Resources.MyResources.Message +" -After Message String " %>
</asp:Label>
Note the concatenation is not on the Text attribute but between the label element
Full post can be found here

Resources