Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hiding controls conditionally in formview

Status
Not open for further replies.

FarleySoftware

Programmer
Jan 14, 2008
18
US
I'm working with a client who has a FormView on an ASP.NET (VB) page, displaying a single record pulled from SQL using a SQLDataSource on the page.

Some of the data are address or phone fields that may be blank. If they're blank, there should be no white space/blank lines, and any accompanying labels must be hidden.

Example:
123 Any Town

Somewhere, NY 10101
Phone: 212-555-1212
Toll-Free:

Should look like:
123 Any Town
Somewhere, NY 10101
Phone: 212-555-1212

("Phone:" appears in a label)

Two questions:

1. Is a databound FormView the best choice of control in this situation?

2. What's the best way to hide controls, without leaving white space? (thinking use of a panel would do the trick if that's not overkill, but wasn't sure ofhow exactly to refer to the controls within ItemTemplate of a formview)

Thanks in advance.

Farley Software Solutions
 
A table within the FormView control ... or just plain ol' table instead of FormView? The latter is easier to me, but I wasn't sure if it was most efficient of my choices.

Farley Software Solutions
 
Ok, here's how it ended up working out ...

For the address portion, I created a function in the codebehind to format the address:

Code:
    Function BuildAddress(ByVal swAddress1 As String, ByVal swAddress2 As String, _
        ByVal swAddress3 As String, ByVal swCity As String, ByVal swState As String, _
        ByVal swPostal As String, ByVal swCountry As String) As String

        Dim stb As New StringBuilder

        With stb
            If Not String.IsNullOrEmpty(swAddress1) Then
                .Append(swAddress1)
                .Append("<BR>")
            End If
            If Not String.IsNullOrEmpty(swAddress2) Then
                .Append(swAddress2)
                .Append("<BR>")
            End If
            If Not String.IsNullOrEmpty(swAddress3) Then
                .Append(swAddress3)
                .Append("<BR>")
            End If
            If Not String.IsNullOrEmpty(swCity) Then
                .Append(swCity)
                .Append("&nbsp;&nbsp;&nbsp;")
            End If
            If Not String.IsNullOrEmpty(swState) Then
                .Append(swState)
                .Append("&nbsp;")
            End If
            If Not String.IsNullOrEmpty(swPostal) Then
                .Append(swPostal)
                .Append("<BR>")
            End If
            If Not String.IsNullOrEmpty(swCountry) Then
                .Append(swCountry)
                .Append("<BR>")
            End If
        End With

        Return stb.ToString
    End Function

I then inserted an ASP.NET Literal control and set its value to the function above:

Code:
    <asp:Literal 
        ID="litAddress" 
        runat="server" 
        Text='<%# BuildAddress(Eval("swAddress1"), Eval("swAddress2"), Eval("swAddress3"), Eval("swCity"), Eval("swState"), Eval("swPostal"),Eval("swCountry") ) %>' />

The section with phone numbers was already set up in a series of table rows within the FormView control. So after thinking of three or four ways to address it the hard way, I finally thought of the simplest way:

Code:
    <tr id="rowswPhone" runat="server" visible='<%# Not String.IsNullOrEmpty(Eval("swPhone")) %>'>



Farley Software Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top