ASP.Net C# ListView inline conditions - c#

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?

Related

Use method to get value in asp:repeater

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.

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

Setting node.PropertiesAsList as Datasource for a repeater

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>

Need some detail on usage of <% in aspx page [duplicate]

This question already has answers here:
how to Embedded Code Blocks in ASP.NET Web Pages?
(2 answers)
Closed 8 years ago.
I am a beginner level programmer. I was using C# variable in the aspx page.
I have seen the usage of <% in the aspx page alot.
I need the detail of when to use <% in what requirement like
<% 'When to use this?' %>
<%= 'When to use this?' %>
<%# 'When to use this?' %>
<%# 'When to use this?' %>
I am searching for a useful link regarding this but did not found any help
I hope this could be Helpful.......
http://www.codeproject.com/Articles/384425/Server-side-Delimiters-in-ASP-NET`
You can search with name "delimiter in asp.net" probably google will give you lot of results.
<% %> is server code that executes during the page's render phase which can execute the statements written inside the block which help in interacting with server side at runtime.
<% { Response.Write("Hello !"; }%>
and like wise if you have script function in your page and you want to call that function you can ue this
<% =Callfunc()%>
And by default in all pages and user controls you can see directives. more here
MSDN
<% 'When to use this?' %> Similar to classic ASP and is used to add server-side code within your ASPX page such as:
<% for (int i=0; i < 10; i++) { %>
<p>I am added to the page 10 times</p>
<% } %>
<%= 'When to use this?' %> Similar to the example above only the = allows you to "inject" or reference an expression or variable and not a chunk of code. Belows example refers to MyAnchor which can be declared in the code behind. <a href='<%= MyAnchor %>'></a>
<%# 'When to use this?' %> This is used for page and control declarations: <%# Page Language="vb" AutoEventWireup="false"
<%# 'When to use this?' %> This is used for databinding
<asp:GridView ID="gvMyGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="E-mail" SortExpression="Email">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Email").ToString()%>' NavigateUrl='<%#Eval("Email", "mailto:{0}").ToString() %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

asp.net web user control with loop

Totally new to webforms user controls, I am bit confused, on how to create a user control and fill some data on it.
for(int i = 0; i < Price.EpList.Count(); i++)
{
Price.EpList[i].Amount.ToString();
Price.EpList[i].Code.ToString();
Price.EpList[i].Desc.ToString();
Price.EpList[i].ID.ToString();
}
EpList is a list that contains info that i want to display in webpage on tabular format with checkboxes on each row.
Take a look at the Repeater Control. You don't have to loop through your list, you just bind the list to the repeater and define the html template you want for each repeated item.
http://www.w3schools.com/aspnet/aspnet_repeater.asp
EDIT: That article uses Visual Basic, so here's the C# translation:
Assuming this repeater:
<asp:Repeater runat="server" ID="uxEpList">
<ItemTemplate>
<%--Html goes here--%>
<%# Eval("Amount")%>
<%# Eval("Code")%>
<%# Eval("Desc")%>
<%# Eval("ID")%>
</ItemTemplate>
</asp:Repeater>
In code behind:
uxEpList.DataSource = Price.Eplist;
uxEpList.DataBind();
If you need to nest a repeater inside another one (using the Desc property from your comment) you can do it like this, by setting the DataSource property declaratively (note the single quotes):
<asp:Repeater runat="server" ID="uxEpList">
<ItemTemplate>
<asp:Repeater Datasource='<%# Eval("Desc")%>' runat="server">
<ItemTemplate>
//etc...

Categories

Resources