Call a function on result of ListView Eval call - asp.net

i have a list view in my pages and i want to display some statistics, my Restaurant class has a Reservations property public List<Reservation> Reservations { get; private set; }, in my ListView, i would like to display number of reservations, but without making a new property like "ReservationsCount", nothing like this <%#Eval("Reservations")%>.Count doesn´t work
<td id="Td10" runat="server">
<%#Eval("Reservations")%>
</td>
Result of this <%#Eval("Reservations")%> is in the aspx page considered object, not a list of Restaurants, i don´t really know why, i am just beggining with asp.net so please help, maybe the solution is ridiculously easy:)

Try applying the count() directly after the Eval():
<%# ((List<Reservation>)Eval("Reservations")).Count() %>
As far as I know you would need to reference System.Linq for this to work. Without Linq you could try using the Count property of List<T>:
<%# ((List<Reservation>)Eval("Reservations")).Count %>

Related

Should I use a repeater or a string builder to build dynamic html

I think it's obvious that the framework wants use to keep the HTML in the aspx documents, and all code in the code behind to achieve a clean level of separation.
So what do we do when the GridView isn't good enough?
Should I:
Use a repeater control to keep HTML in the presentation layer, but be forced to mix business logic in with the HTML
Or should I mix HTML in with my code behind layer in the form of a StringBuilder?
Let's pretend that you said use the Repeater control.
Typically to return a product description I would do this:
<%# DataBinder.Eval(Container.DataItem, "desc") %>
But then I run into the issue of when I want to only return 150 characters of the desc in case it's too long. If my datasource was LINQ to SQL I could just create a new string:
string s = q.desc.lengh > 150 ? q.desc.SubString(0,150) + "..." : q.desc;
How would I do the same inside the repeater within the aspx document if it is preferred I use the repeater?
I would use a Repeater, making html in code behind is messy and hard to maintain.
No reason why you can't use helper methods in the code behind (or elsewhere) to do this
<%# SomeMethod(Eval("desc")) %>
Or on the property of the class you are repeating, have an alternative version with a getter
public class SomeItem
{
public string Desc { get; set; }
public string DescSummary
{
get
{
return Desc.Length > 150 ? string.Format("{0}...", Desc.Substring(0, 150)) : Desc;
}
}
}
And in the repeater eval DescSummary
Try
<%# Eval("desc").ToString().Length > 150 ? Eval("desc").ToString().Substring(0, 150) : Eval("desc") %>

Using c# dynamic with embedded code in an aspx page

In my code behind I have a public IEnumerable<dynamic> AllTransactions{ get; set; } that is composed of {Transaction, String} (created via linq-to-sql as a new anonymous object) where Transaction is a custom class I have created.
In my aspx page I would like to do the following
<% foreach (dynamic trans in AllTransactions) { %>
<span><%= trans.transaction.Amount %></span>
<% } %>
In my code behind I can refer to trans.transaction.Amount inside a foreach but I don't seem to have any luck on aspx pages. I get an exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'transaction'
At the same time, if I have the debugger running I can look at 'trans' and I see it has a Transaction object inside it called 'transaction' and it has my string as well. Further, the 'transaction' object inside it contains a value for Amount. It seems that Microsoft is somewhere converting my dynamic to an object.
One other test I did was spit out what the type of trans is with GetType() and got this:
<>f__AnonymousTypef`2[ProjectName.Packages.Investment.Business.Transaction,System.String]
This is not a regular 'object', so I'm not sure what the type of this is. Any ideas as to why my foreach doesn't work and what I can do differently?
Try binding your data to a repeater control. It was designed to output html for items in lists, and will help you separate your design from your logic, which is generally considered a much better practice than trying to dynamically generate html in script.
Can you try this as below?
<% foreach (dynamic trans in AllTransactions) { %>
<span><%= trans.Amount %></span>
<% } %>
Or can you check whether the definition of the custom class Transaction includes a member variable whose name is "Transaction"?

how to assign a control property value from a global variable in page code?

Greetings,
I have a control and list of variables and I want in the control property to be assigned to the variable value directly in the page not from the back code, something like this
My global variables
public string Banana = "banana_pie";
public string Apple = "apple_pie";
in my custom control instead of:
<uc:LoadPie id="pieBanana" type="banana_pie" />
To this
<uc:LoadPie id="pieBanana" type="<%=Banana %>" />
so is there a way or just assign the property in page back code.
Thanks
You can do it like this using data binding syntax.
<uc:LoadPie id="pieBanana" type='<%#Banana%>' runat="server"></uc:LoadPie>
But then in your code behind you have to call
pieBanana.DataBind();
in the page load in order for the databinding expression to be evaulated.
But if you are going to do this then you might as well assign the property in the page load.
I think you should go with a property (protected should be enought, but I'll say public in the following snippet) in your code behind:
Public Property myBanana() As String
Get
Return Pies.Banana;
End Get
End Property
Then you can use it in your controls, for example:
<uc:LoadPie id="pieBanana" type="<%= myBanana%>" />
Not quite what you want, but how about:
<% pieBanana["type"] = this.Banana %>

SubSonic 2.2 and ASP.NET gridview

I'm trying to show a custom column in my gridview which displays a content type based on a couple of boolean fields in my database. Everything works fine but it's causing a lot of overhead the way I do it now.. like this:
<ItemTemplate>
<asp:Label ID="lblType" runat="server" Text='<%# GetType((int)DataBinder.Eval(Container.DataItem))%>' />
</ItemTemplate>
This calls a function GetType which queries the database based on the ArticleID. Of course this happens for every item in the gridview. Now I would like to know if it's possible to send the current (subsonic) collection item to this function instead? Because the item is already available but I don't know how to put this in my itemtemplate.
My current item is DAL.Article which contains everything I need.
I hope I made myself clear a little !Thanks for your time.
Kind regards,
Mark
Subsonic generated classes are partial and thus extendable.
Let's say you have a DAL object called Person. You can create a new file Person.cs (in a different folder of course).
namespace Your.Dal.Namespace {
public partial class Person
{
public string DisplayName
{
get
{
return String.Format("{0}, {1}", this.LastName, this.FirstName);
}
}
}
}
Now you can access the DisplayName property of your class:
PersonCollection col = new PersonCollection().Load();
foreach(Person p in col)
Console.WriteLine(p.DisplayName);
I use this technique for binding Subsonic Collections to a Windows.Forms DataGridView a lot.
But it should work for asp.net, too.

ASP.NET binding to a UserControl property

This should be really easy but I can't figure out how to make it work...
I have an ASP.NET UserControl (.ascx) with the following property:
public string LabelCssClass
{
get
{
return _labelCssClass;
}
set
{
_labelCssClass = value;
}
}
I want to bind that property into the HTML of the UserControl at run time, using the <%# syntax. I imagine it must be something along these lines:
<td class="<%# Eval("LabelCssClass") %>" >
I've tried all different versions of Eval() and so on ... I'm not getting errors but the binding isn't working, and my breakpoints show that the property is not being accessed.
Whats the correct syntax? cheers
I think what you might want is this:
<td class="<%=LabelCssClass%>">
Kevin's answer is probably closer to what you are trying to achieve; however, you can successfully use the <%# %> syntax in the standard markup if you call DataBind() on the Page itself.

Resources