How do I concatenate 2 resource strings together in an aspx page - asp.net

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

Related

WHy am I unable to add text along with <%# Container.DataItem %> in repeater in user control

I have a User Control which is dynamically placed by CodeBehind as follows:
Dim myControl As Control = CType(Page.LoadControl("~/Controls/mainMenu.ascx"), Control)
If InStr(Request.ServerVariables("url"), "/Login.aspx") <= 0 Then
mainMenu.Controls.Add(myControl)
End If
As per an example from my previous question on here.
Within this Control is a repeater which calls a database to generate values.
My Repeater mark-up is as follows
<asp:Repeater runat="server" ID="locationRepeater" OnItemDataBound="getQuestionCount">
<ItemTemplate>
<p id='locationQuestions' title='<%# Container.DataItem %>' runat='server'></p>
</ItemTemplate>
</asp:Repeater>
The example above works fine, but I want to be able to prepend text to <%# Container.DataItem %> in the title attribute of that <p> to print to the browser like this is some text DATA_ITEM_OUTPUT
When I try to do that though, it prints this is some text <%# Container.DataItem %> exactly like that, ie, turning <%# Container.DataItem %> into text, NOT the value from the repeater code.
It was working fine before I made it into a dynamically inserted control, so I am thinking I might have something being generated in the wrong order, but given that it works without any prepended text, I am stumped to fix it!
I'm new to .net and using vb.net, please could someone point me in the right direction?
It would help to see the broken version of your code, but I gather you're doing this:
<p id='locationQuestions' title='This is some text <%# Container.DataItem %>' runat='server'></p>
If that's the case, try it like this:
<p id='locationQuestions' title='<%# "This is some text " & Container.DataItem %>' runat='server'></p>
Following Statement include the server side code in your page code.
<%# Container.DataItem %>
When you Server side code on the page with some manipulation then put
it in single quote and code as you do code behind.
E.g.
Text = '<%# "Employee Name:" & Eval("EmpName") %>'
so you title markup code should be like this:
title='<%# "This is Title " & DataBinder.Eval(Container.DataItem, "Name") & "Text." %>'
Best of all, the Databinder.Eval syntax is the same for VB and C#.
check this link for more details.

How to create RouteUrls with databound parameters declaratively?

I'm using the new Routing feature in ASP.NET 4 (Web forms, not MVC). Now I have an asp:ListView which is bound to a datasource. One of the properties is a ClientID which I want to use to link from the ListView items to another page. In global.asax I have defined a route:
System.Web.Routing.RouteTable.Routes.MapPageRoute("ClientRoute",
"MyClientPage/{ClientID}", "~/Client.aspx");
so that for instance http://server/MyClientPage/2 is a valid URL if ClientID=2 exists.
In the ListView items I have an asp:HyperLink so that I can create the link:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%# "~/MyClientPage/"+Eval("ClientID") %>' >
Go to Client details
</asp:HyperLink>
Although this works I would prefer to use the RouteName instead of the hardcoded route by using a RouteUrl expression. For instance with a constant ClientID=2 I could write:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl="<%$ RouteUrl:ClientID=2,RouteName=ClientRoute %>" >
Go to Client details
</asp:HyperLink>
Now I am wondering if I can combine the route expression syntax and the databinding syntax. Basically I like to replace the constant 2 above by <%# Eval("ClientID") %>. But doing this in a naive way...
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%$ RouteUrl:ClientID=<%# Eval("ClientID") %>,RouteName=ClientRoute %>' >
Go to Client details
</asp:HyperLink>
... does not work: <%# Eval("ClientID") %> is not evaluated but considered as a string. Playing around with several flavors of quotation marks also didn't help so far (Parser errors in most cases).
Question: Is it possible at all what I am trying to achieve here? And if yes, what's the correct way?
Thank you in advance!
Use System.Web.UI.Control.GetRouteUrl:
VB:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%# GetRouteUrl("ClientRoute", New With {.ClientID = Eval("ClientID")}) %>' >
Go to Client details
</asp:HyperLink>
C#:
<asp:HyperLink ID="HyperLinkClient" runat="server"
NavigateUrl='<%# GetRouteUrl("ClientRoute", new {ClientID = Eval("ClientID")}) %>' >
Go to Client details
</asp:HyperLink>
I know it's basically the same as Samu Lan's solution but instead of using .net controls you could use regular HTML anchor control.
<a href='<%# GetRouteUrl("ClientRoute", new {ClientID = Eval("ClientID")}) %>'>
Go to Client details
</a>

ASP.NET FormView: Including a Member Variable in an XPath Expression

Is something like this possible?
<asp:FormView ID="myFormView" runat="server">
<ItemTemplate>
<p><%# XPath("Root/Path/Item[#id=<%=this.m_myId%>]/HelloWorld")%></p>
</ItemTemplate>
</asp:FormView>
m_myId is a member variable on the page. I can't seem to get it to work without a parsing error.
You can't have nested asp tags. Try something like this:
<%# XPath("Root/Path/Item[#id= " + this.m_myId + "]/HelloWorld") %>

ASP.NET Querystrings

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.

How to pass multiple parameters to Eval()?

i have a bit of aspx code that uses Eval to generate a call to a javascript function:
ASP.NET (wrapped for readability):
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
"return DoStuff(this, \"{0}\");") %>'
Text="Do stuff" />
this generates javascript similar to:
Javascript (wrapped for readability):
return DoStuff(this,
"3F2504E0-4F89-11D3-9A0C-0305E82C3301"
);
Note: i've converted the generated " entities references into quotes for readability.
i now need to add a 3nd parameter to the javascript function call, a caption:
Javascript (wrapped for readability)
return DoStuff(this,
"3F2504E0-4F89-11D3-9A0C-0305E82C3301",
"AllisonAngel.jpg"
);
Note: i've converted the generated " entities references into quotes for readability.
There already exists a function in the code-behind file that is used to return the caption for an item:
C# (code omitted for readability):
protected string GetItemText(MySomething item)
{
...
}
i know that the above function can be called from the aspx file using a syntax similar to:
ASP.NET (wrapped, code omitted, for readability):
<asp:LinkButton ... runat="server"
Text="<%# GetItemText((MySomething)Container.DataItem) %>"
... />
So now i want to use this function to include the 3rd parameter to the javascript function.
Starting from:
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
"return DoStuff(this, \"{0}\", \"Todo - Insert caption here\");") %>'
Text="Do stuff" />
i need to change: "Todo - Insert caption here"
into a call to: <%# GetItemText((MySomething)Container.DataItem) %>
Blindly trying the obvious:
ASP.NET (wrapped for readability):
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
GetItemText((MySomething)Container.DataItem),
"return DoStuff(this, \"{0}\", \"{1}\");") %>'
Text="Do stuff" />
But that complains, since Eval() only takes two parameters.
i tried the slightly less obivous:
ASP.NET (wrapped for readability)
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
"return DoStuff(this,
\"{0}\",
\""+GetItemText((MySomething)Container.DataItem)+"\");") %>'
Text="Do stuff" />
But that doesn't work either.
Related Questions
ASP.NET: How to access repeater generated elements from javascript?
asp.NET: How to access repeater generated elements?
The trick isn't to pass multiple items to an eval, but to pass multiple eval's to whatever you want to use to format the data. You could also have just done it this way - which would have kept the presentation in the aspx file like you wanted...
<asp:LinkButton
runat="server"
OnClientClick='<%# string.Format(
"return DoStuff(this, \"{0}\", \"{1}\");",
Eval("NodeGUID"),
GetItemText((MySomething)Container.DataItem)) %>'
Text="Do stuff" />
My Trick
Eval("FLName", Eval("FLFDID","{0}")+"/{0}")
Eval inside Eval
It Work for me !!
Robert C. Barth gave me the idea that solves the problem:
<asp:LinkButton runat="server"
OnClientClick="<%# GetItemClientClick((MySomething)Container.DataItem) %>"
Text="Do stuff" />
and then the code-behind file contains:
protected string GetItemClientClick(MySomething item)
{
...
String szOnClientClick =
"return DeleteItem(this, "+
Toolkit.QuotedStr(item.NodeGUID.ToString()) + ", "+
Toolkit.QuotedStr(GetItemText(item))+");";
return szOnClientClick;
}
i really would have preferred to keep presentation in the aspx, and business logic in the code-behind - but the real world often doesn't conform to that model.
If the LinkButton is not inside some other thing (like a grid), just set the OnClientClick attribute in the code-behind like normal, using string.Format.
If it IS in a grid (or repeater, or something of that nature), set it in the RowDataBound event using FindControl or e.Row.Cells[] and string.Format. If you use e.Row.Cells[], you can probably dump the LinkButton server control and just output a normal anchor tag.
Continued from Scott Ivey's solution, you can also use this to include if statements in your Eval statement:
For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.
<%# string.Format(Eval("Url") != null ? "{1}" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">
you can try this
'<'asp:HyperLink ID="hp" runat="server" CommandArgument='<%# Eval("SysID", "{0}")%>'
NavigateUrl='<%# String.Format("~/ReportViewer.aspx?RevID= {0}
& User={1}", Eval("ID"), Eval("USER")) %>' Target="_blank" %>'>
</asp:HyperLink>
http://afzal-gujrat.blogspot.com/2012/10/pass-more-evals-in-querystring-with.html

Resources