views:

121

answers:

3

I have a delete button in each row of GridView (component ASP.NET). I want some of the delete buttons to be invisible. The visibility of the delete button should depend on the data that are back the row.

GridView is backed by EntityDataSource. GridView displays entities called Category, one instance in each row. Entity Category has (besides others) also a field of type EntityCollection. Name of that field is Items. Basically I want to allow user to delete a row only if the Items field of backing Category entity is an empty collection.

I cannot make up the binding of Visible property. I have no experience with bindings and Google does not really help. This is how the button looks right now:

<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" 
                    Visible=??? ></asp:Button>

I don't know what should replace ???. The button schold be visible only when this expression evaluates to true:

((SimpleEShop.Model.Category) dataItem).Items.LongCount() <= 0

where dataItem variable contains data of current row in the table. What is the binding that I need ?

+2  A: 

Usually I'd put this in the DataBound handler, but how about something like this:

Visible="<%# Eval("Items.LongCount") <= 0 ? "false" : "true";%>"

The quotes-within-quotes may cause errors, which is one reason I would put it in the ItemDataBound handler.

egrunin
How would you solve it using ItemDataBound handler?
drasto
There were some problems in expression but finally I have this: Visible='<%# (int)Eval("Items.Count") <= 0 ? true : false %>' .Now the problem is that this evaluates to true no matter how many elements are in "Items". How do I find where is the problem ? I cannot use debugger to breakpoint in this code...
drasto
I would just useVisible='<%# Eval("Items.Count") <= 0 %>'
Rajeshwaran S P
Thanks to everyone who helped on this. I try not to post without testing first, but there just wasn't time today :)
egrunin
+1  A: 

egrunin missed some things try it like this

Visible='<%# !(((System.Data.Objects.DataClasses.EntityCollection<YourItemType>)Eval("Items")).Count <= 0 )%>'
alejandrobog
This looks promising - it throws error saying that there is no property "LongCount" in class entity collection but that's my fault... I'll fix it(find correct property) and try again.
drasto
I modified my answer with what I think you want just replace "YourItemType" and that should work
alejandrobog
There were two more problems in expression but finally I have this: Visible='<%# (int)Eval("Items.Count") <= 0 ? true : false %>' .Now the problem is that this evaluates to true no matter how many elements are in "Items". How do I find where is the problem ? I cannot use debugger to breakpoint in this code...
drasto
Unfortunately similar to the previous case. The expression from some reason always evaluates to false so the delete button is always unvisible. I cannot find the reason because I don't know how to debug this.
drasto
But yes as I see it both Visible='<%# (int)Eval("Items.Count") <= 0 ? true : false %>' and your expression should do what I want but they just don't...
drasto
Try displaying Items.Count in another label to verify your items have the info that you expect
alejandrobog
Yes that what I was thinking. Entity "Category" is fetched from the database and as I don't know how exactly the fetching works in C# the property Items may be always empty because of lazy fetching... I'll try this with the label.
drasto
Im pretty sure the issue is lazy loading, you are going to need to use the method LoadWith<> when filling your datasource check out this site http://weblogs.asp.net/zeeshanhirani/archive/2008/07/13/eager-loading-child-entities-in-entity-framework.aspx
alejandrobog
Yes it is true: property Count is always empty, otherwise alejandrobog's solution would work as would the solution: Visible='<%# (int)Eval("Items.Count") <= 0 ? true : false %>'. The not initialized property Items is for other question so I accept this alejandrobog's answer.
drasto
It works! The problem with empty Items was lazy fetching from database. Thanks for help.
drasto
+1  A: 

Adding to egrunin's syntax, I would just use

Visible='<%# Eval("Items.Count") <= 0 %>'
Rajeshwaran S P