开发者

Bind button visibility to the expression (C#)

开发者 https://www.devze.com 2022-12-31 21:32 出处:网络
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 r

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 all开发者_开发百科ow 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 ?


egrunin missed some things try it like this

Visible='<%# !(((System.Data.Objects.DataClasses.EntityCollection<YourItemType>)Eval("Items")).Count <= 0 )%>'


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.


Adding to egrunin's syntax, I would just use

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

精彩评论

暂无评论...
验证码 换一张
取 消