How do I display the value of a resource without a ASP.NET control, i.e. I want to avoid this:
<asp:Label text="<%$ Resources: Messages, ThankYouLabel %>" id="label1" runat="server" />
Instead I would prefer to do just this in my .aspx pages:
<%$ Resources: Messages, ThankYouLabel %>
... but I can’t, a parser error is thrown:
Literal expressions like '<%$ Resources: Messages, ThankYouLabel %>' are not allowed.
Use <asp:Literal runat="server" Text="<%$ Resources: Messages, ThankYouLabel %>" /> instead.
Use HttpContext.GetGlobalResourceObject instead:
<asp:Label text='<%= GetGlobalResourceObject("Messages", "ThankYouLabel") %>'
id="label1"
runat="server" />
It's not possible. you have to use atleast Literal, Another option is to use GetGlobalResurceObject, so that you can use directly in a page.
<%= GetGlobalResourceObject("Messages", "ThankYouLabel")%>
In code behind You can Use
`GetLocalResourceObject("YourKeyInLocalResource")`
and also
`GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey")`
and then use a simple aspnet variable in your Asp.net Markup like <%= Resourcevalue %>
The you can assign your resource value to your Aspnet Variable like
Resourcevalue = GetGlobalResourceObject("GlobalResourceFileName", "YourResourceKey").ToString();
Another method is :-
<asp:Label text='<%= Resources.Messages.ThankYouLabel %>'
id="label1"
runat="server" />
Related
Noob question.
Why does this not work into my .aspx file?
<body>
<asp:Label ID="Label1" runat="server" Text='<%=System.DateTime.Today.Day.ToString()%>' ></asp:Label>
</body>
It does display the <%=System.DateTime.Today.Day.ToString()%> string which is obviously not what I want.
Same result if I try to display the content of a code behind variable:
<asp:Label ID="label" runat="server" Text='<%= versionNumber %>' >
versionNumber being properly instanced and set into the code behind.
You cannot mix server controls with code blocks.
There are two ways to work around that limitation:
Just use <%=System.DateTime.Today.Day.ToString()%> without a Label around it
Use codebehind to set Label1.Text = System.DateTime.Today.Day.ToString();
The first way will display the date to the user, but you cannot further change it from codebehind.
The second way does enable you to alter the text from codebehind.
It is true you can't mix server controls with Code blocks,
If its compulsory for you to use Server side control, and you don't even want to set value from code behind then you can go for this solution.
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>
Similarly you can use code behind variable as follows ,
<asp:Label ID="Label1" runat="server"><%=versionNumber %></asp:Label>
If you really want to use a asp:Label
Use it as follows:
<asp:Label ID="Label1" runat="server"><%=System.DateTime.Today.Day.ToString() %></asp:Label>
How can I add the & character after an eval in ASP.NET C#
I have this line
<asp:Image ID="img1" runat="server" CssClass="item" ImageUrl="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
data-src='<%# "http://www.example.com/images/"+Eval("ThumbnailFile")+"&width=200" %>'
AlternateText='<%# Eval("Title") %>' />
and I get the link like this http://www.example.com/images/thumbnail.jpg&width=200
but I need the link to be http://www.example.com/images/thumbnail.jpg&width=200
You could try using HttpUtility.HtmlDecode in your data-src attribute:
data-src='<%# HttpUtility.HtmlDecode("http://www.example.com/images/"+Eval("ThumbnailFile")+"&width=200") %>'
I have the following line in my aspx file:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem) %>' Height="114" Width="152"/>
Is it possible to add another line to the inline c# something like this?
<asp:Image ID="Image1" runat="server" ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem); SetImageSize(this) %>' Height="114" Width="152"/>
I am afraid that this is not possible. But you could write another method on this helper class which will invoke the two operations at once.
<asp:Image
ID="Image1"
runat="server"
ImageUrl='<%# MediaHelper.GetMediaUrlAndSetImageSize(Container.DataItem, this) %>'
Height="114"
Width="152"
/>
Also mixing C# code with ASPX might lead to spaghetti. I would tend to avoid it as much as possible.
You could use multiple method calls to accomplish what you are trying to do:
<asp:Image
ID="Image1"
runat="server"
ImageUrl='<%# MediaHelper.GetMediaUrl(Container.DataItem) %>'
Height="<%# MediaHelper.GetMediaHeight(Container.DataItem) %>"
Width="<%# MediaHelper.GetMediaWidth(Container.DataItem) %>"
/>
Or just bind an object to the control that has all of those values exposed as properties.
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.
Basically I would like to find a way to ddo something like:
<asp:Label ID="lID" runat="server" AssociatedControlID="txtId" Text="<%# MyProperty %>"></asp:Label>
I know I could set it from code behind (writing lId.Text = MyProperty), but I'd prefer doing it in the markup and I just can't seem to find the solution.
(MyProperty is a string property)
cheers
You can do
<asp:Label runat="server" Text='<%# MyProperty %>' />
And then a Page.DataBind() in the codebehind.
Code expressions are an option as well. These can be used inside of quotes in ASP tags, unlike standard <%= %> tags.
The general syntax is:
<%$ resources: ResourceKey %>
There is a built-in expression for appSettings:
<%$ appSettings: AppSettingsKey %>
More info on this here: http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
Leave the markup as is and make a call to Page.DataBind(); in your code behind.
<asp:Label id="lID" runat="server"><%= MyProperty %></asp:Label>
since asp.net tags do not allow <% %> constructs, you cannot use Text="<%= MyProperty %>".
<div> <%=MyProperty"%></div>
Call lID.Databind() from code-behind
When you use <%# MyProperty %> declaration you need to databind it, but when using <%= MyProperty %> you don't (which is similar to just writing Response.Write(MyProperty).
You can do this:
<asp:Label ID="lblCurrentTime" runat="server">
Last update: <%=DateTime.Now.ToString()%>
</asp:Label>