views:

24

answers:

1

I am currently binding a Nullable bit column to a listview control. When you declare a list view item I need to handle the case when the null value is used instead of just true or false.

<asp:Checkbox ID="Chk1" runat="server" 
    Checked='<%# HandleNullableBool(Eval("IsUsed")) %>' />

Then in the page I add a HandleNullableBool() function inside the ASPX page.

protected static bool HandleNullableBool(object value) 
{
    return (value == null) ? false : (bool)value;
}

This works fine but I need to use this in several pages so I tried creating a utility class with a static HandleNullableBool. But using it in the asp page does not work. Is there a way to do this in another class instead of the ASPX page?

<asp:Checkbox ID="Chk1" runat="server" 
    Checked='<%# Util.HandleNullableBool(Eval("IsUsed")) %>' />
+1  A: 

You can simply write

<asp:Checkbox ID="Chk1" runat="server" 
    Checked='<%# Eval("IsUsed") ?? false %>' />

To answer your question, you need to include the namespace that contains the class, like this: (at the top of the file)

<%@ Import Namespace="Your.Namespace.Here" %>

You can also do this in Web.config:

<pages>
    <namespaces>
        <add namespace="Your.Namespace.Here" />
    </namespaces>
</pages>
SLaks
did not know you can do coallese in Eval expresssion. let me try
Nassign