• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21381826.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:

-----------------------
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21372277.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?
0
Nugs
Asked:
Nugs
  • 12
  • 11
1 Solution
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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.
0
 
NugsAuthor Commented:
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
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
NugsAuthor Commented:
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...
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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
0
 
NugsAuthor Commented:
Only one lable though....
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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.
0
 
NugsAuthor Commented:
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
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
<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
0
 
NugsAuthor Commented:
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>
 
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
DisplayVisible2

shouldn't that be:

DisplayVisible
0
 
NugsAuthor Commented:
I changed the function name:

Public Function DisplayVisible2(ByRef field As Object) As Boolean
        Return Not field Is DBNull.Value
End Function
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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
0
 
NugsAuthor Commented:
Exception Details: System.FormatException: Input string was not in a correct format.

0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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?
0
 
NugsAuthor Commented:
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>                   
----------------------------------------------------------------------------------------------------------------------------------------------------------------
0
 
NugsAuthor Commented:
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
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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.
0
 
NugsAuthor Commented:
You said you did it in VB.Net.... Do you have that example?
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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).
0
 
NugsAuthor Commented:
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?
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
No I mean replacing all that with a ItemDataBound event:

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

Please check MSDN to see how these event work, as they are your best friend. If you can handle C# I can supply samples, but I'm stuck at VB.net.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now