views:

850

answers:

4

I am having trouble with visible attribute of an ASP.NET Panel control. I have a page that calls a database table and returns the results in a datagrid.

Requirements

If some of the returned values are null I need to hide the image that's next to it.

I am using a Panel to determine whether to hide or show the image but am having trouble with the statement:

visible='<%# Eval("addr1") <> DBNull.Value %>'

I have tried these as well:

visible='<%# Eval("addr1") <> DBNull.Value %>'
visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'

Code is below:

<asp:TemplateField >
     <ItemTemplate>
          <%# Eval("Name")%>
               <p>
                   <asp:Panel runat="server" ID="Panel1" 
                        visible='<%# Eval("addr1") <> DBNull.Value %>'>
               <asp:Image Id="imgHouse" runat="server" 
                        AlternateText="Address" SkinId="imgHouse"/>                
               </asp:Panel>
           <%# Eval("addr1") %><p>                                             
</ItemTemplate>
</asp:TemplateField>

What am I doing wrong?

Edit

If I use visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'

I get the following error:

Compiler Error Message: CS1026: ) expected
A: 

try comparing the result of the eval to blank as opposed to null.

Victor
A: 

This would probably be easier to accomplish with server side code.

Handle the RowDataBound event in your grid (assuming you're using gridview, for DataGrid it's ItemDataBound) and then do this:

public void grid1_RowDataBound(object sender, GridViewRowDataBoundEventArgs e)
{
   if(e.Row.RowType == RowType.DataRow)
   {
        object itemFromDb = e.Row.DataItem;  //you'll need to cast this to your type
        Panel p = (Panel)e.Row.FindControl("myPanel");
        if(itemFromDb.SomeItem == null)
           p.Visible = false;
   }
}

This is off of the top of my head, I might have a syntax error or 2 in there. But you get the idea.

Ben Scheirman
+1  A: 

try:

<%# String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem,"addr1").ToString()) #>

deadbug
A: 

Hmmm...

visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'

Should work. What error are you getting?

Christopher Edwards