hide fields and lables with empty data, in Formview or Details

Last post 02-09-2008 3:30 PM by riprod. 7 replies.

Sort Posts:

  • hide fields and lables with empty data, in Formview or Details

    02-09-2008, 1:25 PM
    • Member
      12 point Member
    • riprod
    • Member since 02-08-2008, 7:04 PM
    • Posts 102

    I have something like this :

    Name
    address1
    address2
    City
    State
    Zip

     

    If address2 is empty, I want it to hide that whole row. Even if I remove the lables, it leaves a line return there.

    I'm using Formview but can also use details in Visual Studio 2005

    Thanks

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 2:18 PM
    • Participant
      1,984 point Participant
    • s10n
    • Member since 01-12-2007, 5:16 PM
    • El Salvador
    • Posts 425

    if you are using a table do it like htis:

    <tr runat="server" visible='<%# Eval("Name")!=null %>'>your label goes here</tr>

     

    if you are not using a table consider using one so you dont have problems with the line return that is left after you remove the label.

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 2:24 PM
    • Participant
      832 point Participant
    • ChibiNopo
    • Member since 06-09-2007, 12:53 AM
    • Los Angeles
    • Posts 174

    Use a PlaceHolder around your control and set the PlaceHolder's visibility based on the content of the control it holds.  If the control's data is Null the PlaceHolder will be hidden and won't even take up space. 

    <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Eval("YourItem")) %>'><asp:Label ID="YourItem" runat="server" Text="YourItem" Visible='<%# (Eval("YourItem")) %>' ></asp:Label></asp:PlaceHolder> 

    You might need a <br /> just before the closing tag to make sure that the data above and below don't end up next to each other.  If you put it outside, it will always be there and you could potentially end up with 2 or 3 breaks between items if there were a few Null items being hidden.

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 2:41 PM
    • Member
      12 point Member
    • riprod
    • Member since 02-08-2008, 7:04 PM
    • Posts 102

    I tried this but if gives an error : Conversion from string "" to type 'Boolean' is not valid.

    here's the code :

    <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Eval("address2")) %>'>address2:
    <asp:Label ID="address2Label" runat="server" Text='<%# Bind("address2") %>'></asp:Label><br /></asp:PlaceHolder>

     

    any ideas ?

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 2:53 PM
    • Member
      12 point Member
    • riprod
    • Member since 02-08-2008, 7:04 PM
    • Posts 102

    Hi, SOrry. I tried this too :

     <tr runat="server" visible='<%# Eval("Address2")!=null %>'>your label goes here</tr> but converted it to VB like so...

    <td runat="server" visible='<%# Eval("address2") IsNot Nothing %>'>
    address2:
    <asp:Label ID="address2Label" runat="server" Text='<%# Bind("address2") %>'></asp:Label>
    </td>

    Makes no difference. The row still remains there with the lable and an empty field next to it.

    Maybe I need to elaborate a bit. I'm using VB in Visual Studio 2005.
    I've tried both "DetailsView" and "FormView"

    Details View doesn't seam to allow me to do anything to the code.

    Any other ideas ? I would think this is a very common thing but I'm totally new to ASP.NET and only really know classic ASP.

    Thanks

     

     

     

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 3:05 PM
    Answer
    • Participant
      832 point Participant
    • ChibiNopo
    • Member since 06-09-2007, 12:53 AM
    • Los Angeles
    • Posts 174

    Maybe like this? <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Eval("address2")).ToString().Length > 0 ? true : false%>'>

    This will not hide the row/cell from a table but see if it gets rid of the data.

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 3:22 PM
    • Member
      12 point Member
    • riprod
    • Member since 02-08-2008, 7:04 PM
    • Posts 102

    I can't tell because there is not data there to start with, that's why I'm trying to remove the row.

    If I try it on a row with data like this :

    <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Eval("address2")).ToString().Length > 0 ? true : false %>'><asp:Label ID="address1Label" runat="server" Text='<%# Bind("address1") %>'></asp:Label></asp:PlaceHolder>

    It errors out with Character Not Valid / Syntax error

  • Re: hide fields and lables with empty data, in Formview or Details

    02-09-2008, 3:30 PM
    Answer
    • Member
      12 point Member
    • riprod
    • Member since 02-08-2008, 7:04 PM
    • Posts 102

    Got it !

    <asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# IIf((Eval("address2")).ToString().Length > 0, "true", "false") %>'>
    <
    tr>
    <td>
    address2:
    <asp:Label ID="address2Label" runat="server" Text='<%# Bind("address2") %>'></asp:Label>
    </td>
    </tr>
    </
    asp:PlaceHolder>

     This works! Thanks for the help

Page 1 of 1 (8 items)