Setting node.PropertiesAsList as Datasource for a repeater - c#

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>

Related

Ordered list using repeater in ASP.net

The code below keeps giving me an error:
Repeater code in .ASPX file
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate><ol class="DescriptionRepeater"></HeaderTemplate>
<ItemTemplate>
<li>
<%= this %>
</li>
</ItemTemplate>
<FooterTemplate><ol/></FooterTemplate>
</asp:Repeater>
Code to populate the repeater in .ASPX.CS file
im using visual studio 2017 the code is causing the ordered list to be filled with the path to the .aspx file
List<string> lstDescription = new List<string>();
lstDescription.Add("this is the first description");
lstDescription.Add("this is the second description");
rptDescription.DataSource = lstDescription;
rptDescription.DataBind();
It is not a practical scenario. Anyhow, you just use Container.DataItem, since DataItem itself is a string. Please make sure you use binding syntax <%# %> instead of <%= %>.
<asp:Repeater ID="rptDescription" runat="server">
<HeaderTemplate>
<ol class="DescriptionRepeater">
</HeaderTemplate>
<ItemTemplate>
<li>
<%# Container.DataItem %>
</li>
</ItemTemplate>
<FooterTemplate>
<ol />
</FooterTemplate>
</asp:Repeater

bind DataItem with a space in the name

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.") %>

Why can I not use Eval() in an if condition inside a ListView?

With a set up like this:
<asp:ListView runat="server" ID="lvArticles">
<LayoutTemplate>
<div runat="server" id="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<% if (Eval("Document") != null) { %>
<a href="/Documents/" + <%# Eval("Document.Id") %> + ".pdf">
<%# Eval("Document.Name") %>
</a>
<% } %>
</ItemTemplate>
</asp:ListView>
I get the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Why is databinding methods like Eval() not allowed in if conditions like that? Is the ListView not a databound control?
Because the # inside <%# Eval("value") %> is what tells the server to do the data binding.

Using a repeater with Entity Framework

How can I use a repeater with Entity Framework
<%# Page Title="" Language="C#" MasterPageFile="~/Admin.master" AutoEventWireup="true" CodeBehind="TagOp.aspx.cs" Inherits="canta.TagOp" %>
<%# Import Namespace="canta.POCO.Objects" %>
<asp:Content ID="Content1" ContentPlaceHolderID="AdminContentPlaceHolder" runat="server">
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="EntityDataSource1">
<ItemTemplate>
<div>
<%#(Container.DataItem as TagObject).IconPath %>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=EnCantamEntities"
DefaultContainerName="EnCantamEntities" EnableFlattening="False"
EntitySetName="Tags">
</asp:EntityDataSource>
</asp:Content>
The code above throws a NullReferenceException
This was my bad.
TagObject was my business layer and i tried to load it like it's an entity object.
I first loaded my data to List<TagObject> then binded the list to repeater.Then my problem solved.Guess I was still sleeping when i wrote that lol :)
This was my bad. TagObject was my business layer and i tried to load it like it's an entity object. I first loaded my data to List<TagObject> then binded the list to repeater.Then my problem solved.Guess I was still sleeping when i wrote that lol :)

ASP.Net C# ListView inline conditions

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?

Categories

Resources