When I loop through my data in c# I have to use the below to get the value:
foreach (var item in documentQuery)
{
string value = item.getproperty("title");
//value = "hello world"
}
But if I want to bind the results to an asp:repeater in an ASPX page how would I call this same getproperty method in the ItemTemplate? Because if I just go <%# Eval("title") %> it gives an error...
Eval() is a shortcut to simplify binding. To do something more complex, you won't be able to use it.
I think you need to do something like:
<%# Container.DataItem.getproperty("title")) %>
Or you might have to cast Container.DataItem to your specific type of item:
<%# (Container.DataItem as XXXXXXX).getproperty("title")) %>
where XXXXXXX is whatever the type your item is.
Related
I am trying to use Web.Router and I am trying to set it in a hyperlink where some variables need to be binded from a dataTable. Also, this link is a part of a Gridview:-
<%# Eval("title")) %>
But I get error: Server Tag not well formed. What's wrong with the syntax?
Try changing
<%# Eval("title")) %>
To this
<%# Eval("title")) %>
You put a double quot instead of single quot
i'm trying to bind dataitem in GridView like that:
<%# DataBinder.Eval(Container, "DataItem.Project No.") %>
and getting the error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Project No'.
the field is Project No. (with a dot .).
how can i bind that field?
try this : [] to indicatethat it is a column name.
<%# DataBinder.Eval(Container, "DataItem.[Project No.]") %>
<%# Eval("Project No.") %>
note that your datatable/dataset should have Project No. exactly to work
Update
100% Working & Tested
<%#DataBinder.GetPropertyValue(Container.DataItem,"Project No.") %>
I'm using the Repeater control. All the property names in my data source are represented in my ImportColumns enum.
So I am trying to loop through the enums to pass the names to the Eval method.
This is in ItemTemplate section of my Repeater control.
Line 57: <tr class="ProductRow" data-id="<%# Eval("Id") %>">
Line 58: <% foreach (var column in (ImportColumns[])Enum.GetValues(typeof(ImportColumns))) { %>
Line 59: <td><%# Eval(column.ToString()) %></td>
Line 60: <% } %>
Line 61: </tr>
But line 59 gives me the following error.
CS0103: The name 'column' does not exist in the current context
Why doesn't the code recognize the column variable? I'm guessing it either has something to do with the fact that I'm in a repeater control or that I'm missing <% %> code and <%# %>, but it seems like this should work.
EDIT:
I see that if I change line 59.
<td><%= column.ToString() %></td>
There is no error. (Although it no longer does what I need.)
So this has something to do with Eval that prevents "regular" variables from working. Does anyone know of a workaround to this?
The column variable is not recognized because <%# ... %> data-binding expressions are evaluated when the container is data-bound, whereas <% ... %> and <%= ... %> code blocks are executed when the page is rendered. If you click "Show Complete Compilation Source" on the error page, you'll see that
foreach (var column in (ImportColumns[])Enum.GetValues(typeof(ImportColumns))) {
and
Eval(column.ToString())
are located in two separate methods.
One possible solution is to combine the loop and Eval into a single data-binding expression. For example:
<%#
string.Concat(Enum.GetValues(typeof(ImportColumns)).Cast<ImportColumns>()
.Select(column => "<td>" + Eval(column.ToString()) + "</td>"))
%>
I think what you are typing to do is use the name of the enumeration entries to be used as the property name for the Eval
If so then use the Enum.GetNames instead of the GetValues method.
Line 57: <tr class="ProductRow" data-id="<%# Eval("Id") %>">
Line 58: <% foreach (string column in Enum.GetNames(typeof(ImportColumns))) { %>
Line 59: <td><%# Eval(column) %></td>
Line 60: <% } %>
Line 61: </tr>
Edit:
You are right I didn’t test that. I also don’t use inline code so I don’t understand the intricacies. Here is a solution that works for me.
I would put that loop logic in a method in the code behind file that creates the complete row and then invoke it from the aspx page.
In the aspx
<tr class="ProductRow" data-id="<%# Eval("Id") %>">
<%# this.ColumnDetail() %>
</tr>
The method in the code behind
protected string ColumnDetail() {
string html = String.Empty;
foreach (string x in Enum.GetNames(typeof(ImportColumns))) {
html = (html + string.Format("<td>{0}</td>", Eval(x)));
}
return html;
}
I'm trying to set the PropertiesList of a node as the DataSource for my repeater.
rptDistributors.DataSource = node.PropertiesAsList;
rptDistributors.DataBind();
And in my repeater I try to get the umbDistributorCountry.
<asp:Repeater ID="rptDistributors" runat="server">
<%# Eval("umbDistributorCountry") %>
</asp:Repeater>
However I run into problems because it doesn't know any of the properties.
DataBinding: 'umbraco.NodeFactory.Property' does not contain a property with the name 'umbDistributorCountry'.
The content of the list looks like the following:
Any ideas?
Thanks,
Thomas
The clue is in the error ...
DataBinding: 'umbraco.NodeFactory.Property' does not contain a property with the name 'umbDistributorCountry'.
umbDistributorCountry is not a .NET property, but the value of the property called Alias. An Umbraco property contains the three .NET properties in your screenshot, so you only have access to these ...
<%# Eval("Alias") %>
<%# Eval("Value") %>
<%# Eval("Version") %>
Assuming you want to show all the Umbraco properties of that particular distributor (which is stored in node, I guess), you would need something like this ....
<asp:Repeater ID="rptDistributors" runat="server">
<ItemTemplate>
<%# Eval("Alias") %> : <%# Eval("Value") %> <br />
</ItemTemplate>
</asp:Repeater>
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?