Can't set Visible attribute in ASP.NET panels
I am having problems with the visible attribute of an ASP.NET control Panel
. I have a page that calls a database table and returns the results in a datagrid.
Requirements
If some of the return values null
, I need to hide the image that is next to it.
I am using Panel
to determine whether to hide or show an image, but I am having trouble with the assertion:
visible='<%# Eval("addr1") <> DBNull.Value %>'
I tried it too:
visible='<%# Eval("addr1") <> DBNull.Value %>'
visible='<%# IIf(Eval("addr1") Is DbNull.Value, "False","True") %>'
Code 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 am getting the following error:
Compiler Error Message: CS1026: ) expected
This will probably be easier to accomplish with server side code.
Handle the RowDataBound event on your grid (assuming you are using a gridview, for the DataGrid it is ItemDataBound) and then do the following:
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 from the top of my head, I might have a syntax error or 2. But you have an idea.
source to share