I have this databinding expression:
<%# (Boolean.Parse(DataBinder.Eval(Container.DataItem, "HasFoo").ToString())) ? "Yes" : "No" %>
I want to use an asp.net resource binding tag for "yes" and "no," so that in other languages I can use the correct language (I know, I know, mostly everyone uses yes and no but this is just an example).
I've tried putting in
"<%$ Resources: General, Yes %>"
It is in an item template:
<ItemTemplate>
<%# (Boolean.Parse(DataBinder.Eval(Container.DataItem, "HasFoo").ToString())) ? "Yes" : "No" %>
</ItemTemplate>
But it seems I can't do that inside the databinding expression. Anyone have any ideas other than loading these resources at page_init and then hiding them in a serverside hidden panel?
You can retrieve any static Resource value using:
GetLocalResourceObject:
<%= HttpContext.GetLocalResourceObject(virtualPath, resourceKey, CultureInfo) %>
GetGlobalResourceObject:
<%= HttpContext.GetGlobalResourceObject(classKey, resourceKey, CultureInfo) %>
Take a look here for more information: How to: Retrieve Resource Values Programmatically
Related
I have a repeater with a column that I needs to have the following expression, but not sure of the syntax.
If value="DispForm.aspx" then
Show "No Document"
Else
Show the real value.
I tried to use all expresion in one line, but not sure what am I missing here
<a href=" <%# DataBinder.Eval(Container.DataItem, "Path") %>">
<%#
if (DataBinder.Eval(Container.DataItem, "FileName") == "DispForm.aspx")
{
"No document";
}
else
{
DataBinder.Eval(Container.DataItem, "FileName");}%>
</a>
Error :
http://screencast.com/t/ZERZjzZxST
It would be much simpler to use the ItemDataBound event, add an id and runat="server" to the anchor element, then you can use e.Item.FindControl("anchorname") to get the htmlanchor element. You can then set it's text, href, visibility etc. in codebehind far easier than trying to get some of the more difficult databinding logic to work effectively.
I solved it my self like this: item databound events will be fired on the server side for each row, and this report returns 2000 rows. Not a good thing.
<a href="<%# DataBinder.Eval(Container.DataItem, "fileNameUrl") %>">
<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() == "DispForm.aspx" ? "No document" : DataBinder.Eval(Container.DataItem, "FileName").ToString() %>
</a>
this is definitely an easy question but I still don't know what exactly it is for. Can anybody tell me what ImageUrl='<%# Eval("FileName") %>' means? I still don't get the idea why we need to include %#.
<%# Eval("FileName") %> is used in the context of binding data from a collection to a control. Probably the value for the imageurl is coming from a property of an object in the collection
For example, List<Photo> where Photo has a property of FileName. If you are binding that to a gridview, a repeater, etc, you'll access that property for each item in the collection when binding to such controls
Asp.net data binding overview
in this Line...
ImageUrl='<%# Eval("FileName") %>'
ImageURL the attribute of your asp:ImageButton control that is used to specify the Url of the Image File to be Used
Code between '<% and %>' tags are writtent to be Executed on the Server
'#' is used to specify that the result of server side execution will be bound hear
Eval KeyWord is Used To Evaluate the perticular Column Value (that you specify ("--hear--")) from The DataSourse
When you are using a Template Control like Repeater , GridView , etc. you are actually iterating in a list of data records, and <%# Eval("FileName") %> here means give me the value of the column named FileName.
Here we have used the Eval function which is used for one way databinding. FileName is the field name you are associating. Anything that's written inside <%# %> is parsed by asp.net engine before generating the webpage source which is pure client side script and html tags.
So Eval function is executed at server end by ASP.net engine.
Right, please don't point me there:
Disable a HyperLink from code behind
My problem is as follows.
I have a hyperlink on my aspx page:
<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.
On my page I am getting following error:
Parser Error Message: Cannot create an object of type 'System.Boolean'
from its string representation '<%= _myUser.hasPermission("Intranet
Management") %>' for the 'Visible' property.
Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...
Thanks for any help.
The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try
<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...
And make sure to call Page.DataBind().
In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.
If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.
use the following instead:
<%# _myUser.hasPermission("Intranet Management") %>
Got it from here
This should work ...
<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
Perhaps this is better after all
<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>
I am trying to get an explicit localization expression for a page title, but can't seem to figure out how, and a google search comes up with nothing.
With a control, it's nice and easy:
<asp:literal runat="server" text="<%$ Resources:MyResource, StringId %>" />
But how does one do this for the page title? I've tried specifying it in the page directive, but that of course doesn't work:
<%# Page Title="<%$ Resources:MyResource, StringId %>" ...
Is there a way to do this? Or is it simply not possible?
You can also use either
Page.Title = Resources.MyResource.StringId;
or
<title><%= Resources.MyResource.StringId %></title>
simply in the page load event write
Page.Title="any String u want";
In have a list of objects bound to a ListView that is used to make a nice list of these items. In this ListView I have 1 column that shoud have a specific condition to display a specific string. Is this possible using inline code or should I get a workaround using the codebehind?
This is what I would like to do:
<% if (((Recipe)Container.DataItem).Status == RecipesModel.REJECTED) { %>
Something goes here
<% } %>
But this returns this exception:
The name 'Container' does not exist in the current context
EDIT: this code is used inside <ItemTemplate>
EDIT 2: I found myself using the following code for this problem:
<asp:PlaceHolder id="place_public" runat="server" Visible='<%# ((Recipe)Container.DataItem).Status == RecipesModel.VALIDATED %>'>
Something here
</asp:PlaceHolder>
you cannot use Container.DataItem outside of data binding context
try something like
<%# Container.DataItem ... %>
eg:
<%# ((String)Container.DataItem).ToUpper() == "test" ? "IsTest" : "NotTest" %>
You may want to import the namespace of the container class in your .aspx page.
for example:
<%# Import Namespace="Container Class namespace" %>
It looks like you are trying to use the Container object outside of its scope. Can you post the rest of the code so we can see what is happening on the page?