Link to home
Start Free TrialLog in
Avatar of Nugs
Nugs

asked on

Hide Region if field is empty (Cont.) Multiple fields...

https://www.experts-exchange.com/questions/21381826/Hide-Region-if-field-is-empty-Cont-Multiple-fields.html

Hey there guys... I posted the above question in ASP... BlueDevilFan was kind enough to give me a function that will work if i can get it to work, but he was not knowledable in .Net the help me further, hense the post here...

Here was my question:

-----------------------
https://www.experts-exchange.com/questions/21372277/Hide-Region-if-field-is-empty.html

Ok guys... This is a continuation of my last post about this... I solved this problem of hiding a region of HTML if a field in the DB was blank... I now want to check multiple fields (5 to be exact) and if any of them has a value then visible=true and if all are blank visible=false...

Below is what i am doing to check only one field and hide or show depending on if the field holds a value... I wonder if you guys can help me modify this to check 5 fields or more...
------------------------------------------------------------------------------------------------------
...
Public Function DisplayVisible(ByVal fieldname As Object) As Boolean
        If fieldname Is DBNull.Value Then
            Return False
        Else
            Return True
        End If
End Function

...

<asp:Label id="File1" Runat ="server" Visible ='<%#DisplayVisible(Container.dataitem("Fld_File1"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File1")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
------------------------------------------------------------------------------------------------------
----------------------------

This is the function he gave me:

----------------------------
Public Function DisplayVisible() As Boolean
        If (fieldname1 Is DBNull.Value) And (fieldname2 Is DBNull.Value) And (fieldname3 Is DBNull.Value) And (fieldname4 Is DBNull.Value) And (fieldname5 Is DBNull.Value) Then
            Return False
        Else
            Return True
        End If
End Function
----------------------------

I ended up modifying it slightly but get this error:
----------------------------------------------------------------------------------------------
Compiler Error Message: BC30451: Name 'Container' is not declared.

Source Error:

Line 78:
Line 79: Public Function DisplayVisible2() As Boolean
Line 80:         If (Container.dataitem("Fld_File1") Is DBNull.Value) And (Container.dataitem("Fld_File2") Is DBNull.Value) And (Container.dataitem("Fld_File3") Is DBNull.Value) And (Container.dataitem("Fld_File4") Is DBNull.Value) And (Container.dataitem("Fld_File5") Is DBNull.Value) Then
Line 81:             Return False
Line 82:         Else
-----------------------------------------------------------------------------------------------

Can you guys give me the right syntax for this?
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

I think:

<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1"),Container.dataitem("Fld_File2"),Container.dataitem("Fld_File3"),Container.dataitem("Fld_File4"),Container.dataitem("Fld_File5"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File1")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>

Public Function DisplayVisible(ByVal fieldname1 As Object, ByVal fieldname2 As Object, ByVal fieldname3 As Object, ByVal fieldname4 As Object, ByVal fieldname5 As Object) As Boolean
        Return (fieldname1 Is DBNull.Value) And (fieldname2 Is DBNull.Value) And (fieldname3 Is DBNull.Value) And (fieldname4 Is DBNull.Value) And (fieldname5 Is DBNull.Value)
End Function

NOTE: My primary language is C#, but I guess this should work.
Avatar of Nugs
Nugs

ASKER

Well that sort of worked but i need it to return a true or false like this:

        If fieldname Is DBNull.Value Then
            Return False
        Else
            Return True
        End If

----------

I tried doing sometihgn like this but that obviousley didn't work...

----------

Public Function DisplayVisible2(ByVal fieldname1 As Object, ByVal fieldname2 As Object, ByVal fieldname3 As Object, ByVal fieldname4 As Object, ByVal fieldname5 As Object) As Boolean
        If (fieldname1 Is DBNull.Value) And (fieldname2 Is DBNull.Value) And (fieldname3 Is DBNull.Value) And (fieldname4 Is DBNull.Value) And (fieldname5 Is DBNull.Value)
            Then
            Return False
        Else
            Return True
        End If
End Function
Let me see if I understand you correct.

You have 5 links, and if any of these is flled in the database you want to show all fife links right?

That would be:

<asp:Label id="Files" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1") Or Container.dataitem("Fld_File2") Or Container.dataitem("Fld_File3") Or Container.dataitem("Fld_File4") Or Container.dataitem("Fld_File5"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File1")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File2")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File3")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File4")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File5")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>

Or only show each label if an attachment is present. So something like:

<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File1")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File2" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File2"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File2")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File3" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File3"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File3")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File4" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File4"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File4")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File5" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File5"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File5")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>

Both would use the same function in the code-behind:

Public Function DisplayVisible(ByVal fieldname As Object) As Boolean
        Return Not fieldname Is DBNull.Value
End Function
Avatar of Nugs

ASKER

uumm no, i have 5 fields in the DB ..... I have a lable.... I want to check all five fields in the DB and set the lables visible to 'true' if any of those fields hold a value...

I am doing this perfectly when checking only one field in the Db but now i want to check 5...
That would be the final option I posted. Just to make sure you get the right one:

<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File1")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File2" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File2"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File2")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File3" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File3"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File3")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File4" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File4"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File4")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>
<asp:Label id="File5" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File5"))%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File5")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</asp:Label>

Both would use the same function in the code-behind:

Public Function DisplayVisible(ByVal fieldname As Object) As Boolean
        Return Not fieldname Is DBNull.Value
End Function
Avatar of Nugs

ASKER

Only one lable though....
one label, multiple files right? Try this:

<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1") Or Container.dataitem("Fld_File2") Or Container.dataitem("Fld_File3") Or Container.dataitem("Fld_File4") Or Container.dataitem("Fld_File5"))%>'>

<span id="File1" RunAt="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1")%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File1")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</span>

<span  id="File2" RunAt="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File2")%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File2")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</span >

<span  id="File3" RunAt="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File3")%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File3")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</span >

<span  id="File4" RunAt="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File4")%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File4")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</span >

<span  id="File5" RunAt="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File5")%>'>
<a href="Upload/<%# DataBinder.Eval(Container.DataItem,"FLD_File5")%>"><img height="16" border="0" src="../images/attachment.gif" width="14"></a>
</span >

</asp:Label>

And in the codebehind:

Public Function DisplayVisible(ByVal fieldname As Object) As Boolean
        Return Not fieldname Is DBNull.Value
End Function
made one tiny typo. The first span and the label have the same id. Just rename the label to FilesLabel or something to make it work.
Avatar of Nugs

ASKER

ToAoM, no here let me explain...

i have ONE image inside a lable. If any of the field (File1 through File5) in the database have a value then the image/ lable is displayed...

Somehting like this:
------------------------------------------------------------------------------------------------------------------------------------------------
<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible2(Container.dataitem("Fld_File1"),Container.dataitem("Fld_File2"),Container.dataitem("Fld_File3"),Container.dataitem("Fld_File4"),Container.dataitem("Fld_File5"))%>'>
<img height="16" border="0" src="../images/attachment.gif" width="14">
</asp:Label>
------------------------------------------------------------------------------------------------------------------------------------------------
currently this is executing DisplayVisable2 which looks like this:
------------------------------------------------------------------------------------------------------------------------------------------------
Public Function DisplayVisible2(ByVal fieldname1 As Object, ByVal fieldname2 As Object, ByVal fieldname3 As Object, ByVal fieldname4 As Object, ByVal fieldname5 As Object) As Boolean
        Return (fieldname1 Is DBNull.Value) And (fieldname2 Is DBNull.Value) And (fieldname3 Is DBNull.Value) And (fieldname4 Is DBNull.Value) And (fieldname5 Is DBNull.Value)
End Function
------------------------------------------------------------------------------------------------------------------------------------------------

So, one lable one image inside the lable checking 5 DB fields...

Nugs
<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1") Or Container.dataitem("Fld_File2") Or Container.dataitem("Fld_File3") Or Container.dataitem("Fld_File4") Or Container.dataitem("Fld_File5"))%>'>
<img height="16" border="0" src="../images/attachment.gif" width="14">
</asp:Label>

Public Function DisplayVisible(ByRef field As Object) As Boolean
        Return Not field Is DBNull.Value
End Function
Avatar of Nugs

ASKER

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 336:                        <td width="70%"><a href="ViewRecord.aspx?Fld_id=<%# DataBinder.Eval(Container.DataItem,"FLD_id")%>"><%# DataBinder.Eval(Container.DataItem,"FLD_Title")%></a></td>
Line 337:                        <td width="10%" align="center" valign="middle" nowrap>
Line 338:<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible2(Container.dataitem("Fld_File1") Or Container.dataitem("Fld_File2") Or Container.dataitem("Fld_File3") Or Container.dataitem("Fld_File4") Or Container.dataitem("Fld_File5"))%>'>
Line 339:<img height="16" border="0" src="../images/attachment.gif" width="14">
Line 340:</asp:Label>
 
DisplayVisible2

shouldn't that be:

DisplayVisible
Avatar of Nugs

ASKER

I changed the function name:

Public Function DisplayVisible2(ByRef field As Object) As Boolean
        Return Not field Is DBNull.Value
End Function
DisplayVisible2(Container.dataitem("Fld_File1") Or Container.dataitem("Fld_File2") Or Container.dataitem("Fld_File3") Or Container.dataitem("Fld_File4") Or Container.dataitem("Fld_File5")).ToString()

It might need a ToString, but I'm a bit puzzled byu the VB.net syntax
Avatar of Nugs

ASKER

Exception Details: System.FormatException: Input string was not in a correct format.

could you pst the a little more details of the ASPX page you're working on, I'm trying to build a simple testcase here, but it's not working out as planned. Is the label inside a repeater or something?
Avatar of Nugs

ASKER

It's inside a datalist.... The one i am posting works for onefield in the Db but as you know i am trying to make it work to check multiple fields in the DB...

The Function:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Public Function DisplayVisible(ByVal fieldname As Object) As Boolean
        If fieldname Is DBNull.Value Then
            Return False
        Else
            Return True
        End If
End Function
----------------------------------------------------------------------------------------------------------------------------------------------------------------

.....
.....

The Datalist:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
                    <asp:DataList ID="DataList2"
                              runat="server"
                              Width="100%"
                              RepeatColumns="1"
                              RepeatDirection="Vertical"
                              RepeatLayout="Table">
                      <headertemplate>
                           <table width="100%" border="0" cellpadding="4" cellspacing="0">
                      <tr valign="top" bgcolor="#FFFFEC">

                        <td nowrap width="70%" bgcolor="#FFFFEC" class="Text style1">Title:</td>
                        <td nowrap width="10%" class="Text style1">Files:</td>
                        <td nowrap width="20%" class="Text style1">Date:</td>
                      </tr>
                              </table>
                      </headertemplate>
                      <itemtemplate>
                           <table width="100%" border="0" cellpadding="4" cellspacing="0">                                 
                      <tr valign="bottom" class="TextSmall" onMouseOver="this.style.backgroundColor='#C6D7EB'" onMouseOut="this.style.backgroundColor='#FFFFFF';">
                        <td width="70%"><a href="ViewRecord.aspx?Fld_id=<%# DataBinder.Eval(Container.DataItem,"FLD_id")%>"><%# DataBinder.Eval(Container.DataItem,"FLD_Title")%></a></td>
                        <td width="10%" align="center" valign="middle" nowrap>
<asp:Label id="File1" Runat ="server" Visible ='<%#DisplayVisible(Container.dataitem("Fld_File1"))%>'>
<img height="16" border="0" src="../images/attachment.gif" width="14">
</asp:Label>
                                    </td>
                        <td width="20%" align="right" valign="middle" nowrap><%# DataBinder.Eval(Container.DataItem, "FLD_TimeStamp", "{0:MM/dd/yyyy}")%></td>
                      </tr>
                              </table>
                      </itemtemplate>
                    </asp:DataList>                   
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Avatar of Nugs

ASKER

How about this.....
I write a function that takes the value of all 5 fields and places it inot a string... THEN i check the value of the string and if the string is Null THEN set a lable visible to FALSE...

I did something like this, i'm writing it to a lable just to test but i am having trouble pulling the value from the DB... What do i use instead of DataBinder.Eval(Container.DataItem,"FLD_File3") to get the value from athe DB?

-----------------------------------------------------------------------------------------------------------------------
Public Function SeekAnyFiles()
            Dim strFiles As String
                  strFiles = DataBinder.Eval(Container.DataItem,"FLD_File3")
                  strFiles = strFiles + DataBinder.Eval(Container.DataItem,"FLD_File3")
                  
                  lblTest.Text = strFiles
End Function                  
-----------------------------------------------------------------------------------------------------------------------

Nugs
I've trid this with C# and VB.net now and in C# I would be uding the ItemDataBound event instead of inline these calls into the html itself.

It's a better place because of a couple of things. One is that it allows for compiler checking at compile time (whereas the html is only checked at runtime) and it's much cleaner:

I came up with the following C# event handler:

            private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
            {
                  if (e.Item.ItemType == ListItemType.Item)
                  {
                        if (
                              DataBinder.Eval(e.Item.DataItem, "Fld_File1") == DBNull.Value
                              && DataBinder.Eval(e.Item.DataItem, "Fld_File2") == DBNull.Value
                              && DataBinder.Eval(e.Item.DataItem, "Fld_File3") == DBNull.Value
                              && DataBinder.Eval(e.Item.DataItem, "Fld_File4") == DBNull.Value
                              && DataBinder.Eval(e.Item.DataItem, "Fld_File5") == DBNull.Value)
                        {
                              e.Item.FindControl("imgAttachment").Visible = false;
                        }

                  }
                  
            }

I guess VB would result in:

Private Sub DataList1_ItemDataboud(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataList1.ItemDataboud
      if (e.Item.ItemType = ListItemType.Item) then
            if (
                  DataBinder.Eval(e.Item.DataItem, "Fld_File1") = DBNull.Value
                  And DataBinder.Eval(e.Item.DataItem, "Fld_File2") = DBNull.Value
                  And DataBinder.Eval(e.Item.DataItem, "Fld_File3") = DBNull.Value
                  And DataBinder.Eval(e.Item.DataItem, "Fld_File4") = DBNull.Value
                  And DataBinder.Eval(e.Item.DataItem, "Fld_File5") = DBNull.Value)
            then
                        e.Item.FindControl("imgAttachment").Visible = false
            end if
      end if
End Sub

In this case you can just add visible="true" to the image. The same technique can be applied to the labels if you want to.
Avatar of Nugs

ASKER

You said you did it in VB.Net.... Do you have that example?
That VB function I posted in my last post should do the job (it may contain some syntax errors though, I tried VB.net, but as I said before it clearly isn't language of choice).
Avatar of Nugs

ASKER

If you taling about:

<asp:Label id="File1" Runat ="server" Visible='<%#DisplayVisible(Container.dataitem("Fld_File1") Or Container.dataitem("Fld_File2") Or Container.dataitem("Fld_File3") Or Container.dataitem("Fld_File4") Or Container.dataitem("Fld_File5"))%>'>
<img height="16" border="0" src="../images/attachment.gif" width="14">
</asp:Label>

Public Function DisplayVisible(ByRef field As Object) As Boolean
        Return Not field Is DBNull.Value
End Function

Then no it doesn't work...

What about the idea of creating a string to check?
ASKER CERTIFIED SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial