Link to home
Start Free TrialLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

asked on

how to habdle Eval("Address") blanks

Hi folks, I have a page which displays a list of physicians in a listview control asp.net 3.5
I was chwecking the table on database and there is many of them that is not phone, address, address line2, fax available , So I am looking a way to programatically tell the list that when there is not any of those fileds to pull just go to next one...  What I am doing since I dont have that much experience is n "spagettti code" in each Lable of the Item Template, as the code posted below,

Is there any other better approach, the issue here is that for the way I build the list report, is showing lots of blanks ore even

Name
,   ,
,   ,
City Zip

THANKS!!!
<ItemTemplate>
            <br />
            <table id="TableItemTemplate" runat="server" class="itemTemplateClass">
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Last_Name") %>' />
                        ,
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("First_Name") %>' />
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("Prof_Designation") %>' />
                        </b>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("Specialty") %>' />
                        <asp:Label ID="Label5" runat="server" Text='<%# Eval("[Specialty2]") %>' />
                        <br />
                        <%  If Eval("Address") Is <> "" Then %>

                            
                        <asp:Label ID="Label6" runat="server" Text='<%# Eval("Address") %>' />
                        <br />
                        <%End If%>

                        <%  If Eval("Address2") Is <> "" Then %>
                        <asp:Label ID="Label7" runat="server" Text='<%# Eval("Address2") %>' />
                        <br />
                        <%End If%>

Open in new window

Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

You can do this in a Javascript on the back of your page like this

Remove all this code
 <asp:Label ID="Label6" runat="server" Text='<%# Eval("Address") %>' />
                        <br />
                        <%End If%>

                        <%  If Eval("Address2") Is <> "" Then %>
                        <asp:Label ID="Label7" runat="server" Text='<%# Eval("Address2") %>' />
                        <br />
                        <%End If%>


and then insert this script on your page between the <head></head>

<script type="text/vb" language="vbscript">
 Function CheckAddress(byVal Address,ByVal Address2)
   Dim FinalAddress
   address2=iff(Address2 is dbnull.value,"",vbcrlf & Address2)
   FinalAddress=iff(Address is dbnull.value,"",Address & address2)
   return FinalAddress
 End Function
</script>

and in the form where you remove your code do this

<asp:Label ID="Label6" runat="server" Text='<%# CheckAddress(Eval("Address"),Eval("Address2")) %>' />

 and you will have in the Label6 the address. if has both will add the first one and the secondone on the other line below
Avatar of TonyReba

ASKER

this looks more a vbscript , can I do it on code behind?
I think that you must do it at the time that the control is binding but you can try at the code behind
it shows multiple errors..
this is what I have

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
    CodeFile="ProviderSearch.aspx.vb" Inherits="ProviderSearch" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script src="Scripts/Script.js" type="text/javascript"></script>
    <script type="text/javascript">

        // Let's use a lowercase function name to keep with JavaScript conventions

        function selectAll(invoker) {

            // Since ASP.NET checkboxes are really HTML input elements
            //  let's get all the inputs 
            var inputElements = document.getElementsByTagName('input');
            for (var i = 0; i < inputElements.length; i++) {

                var myElement = inputElements[i];
                // Filter through the input types looking for checkboxes
                if (myElement.type === "checkbox") {

                    // Use the invoker (our calling element) as the reference 
                    //  for our checkbox status
                    myElement.checked = invoker.checked;
                }

            }

        }  

    </script>

    <script type="text/vb" language="vbscript">
 Function CheckAddress(byVal Address,ByVal Address2)
   Dim FinalAddress
   address2=iff(Address2 is dbnull.value,"",vbcrlf & Address2)
   FinalAddress=iff(Address is dbnull.value,"",Address & address2)
   return FinalAddress
 End Function
</script>

.
..
.

 <ItemTemplate>
            <br />
            <table id="TableItemTemplate" runat="server" class="itemTemplateClass">
                <tr>
                    <td>
                        <b>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Last_Name") %>' />
                            ,
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("First_Name") %>' />
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("Prof_Designation") %>' />
                        </b>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("Specialty") %>' />
                        <asp:Label ID="Label5" runat="server" Text='<%# Eval("[Specialty2]") %>' />
                        <br />
                        
                       <asp:Label ID="Label6" runat="server" Text='<%# CheckAddress(Eval("Address"),Eval("Address2")) %>' />
                        <br />
                        <asp:Label ID="Label7" runat="server" Text='<%# Eval("Address2") %>' />
                        <br />
                        <asp:Label ID="Label8" runat="server" Text='<%# Eval("City") %>' />
                        ,
                        <asp:Label ID="Label9" runat="server" Text='<%# Eval("State") %>' />
                        ,
                        <asp:Label ID="Label10" runat="server" Text='<%# Eval("Zip") %>' />
                        <br />
                        <asp:Label ID="Label11" runat="server" Text='<%# Eval("Phone") %>' />
                        <br />
                        <asp:Label ID="Label12" runat="server" Text='<%# Eval("Fax") %>' />
                        <br />
                        <asp:Label ID="lblId1" runat="server" Visible="false" Text='<%# Eval("Id") %>' />
                        <asp:CheckBox ID="CheckBoxPrintProvider1" runat="server" />
                        <asp:HyperLink ID="HyperLink1" runat="server" Text="Click to See a Map" NavigateUrl='<%# "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=" + Server.UrlEncode(Eval("Address").ToString) + "," + Server.UrlEncode(Eval("City").ToString) + "," + Server.UrlEncode(Eval("State").ToString) %>' />
                    </td>
                </tr>
            </table>
            <br />
        </ItemTemplate>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America 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
Also on this line make this correction
before
         Address2 = IIf(Address2 Is DBNull.Value, "", vbCrLf & Address2)
         FinalAddress = IIf(Address Is DBNull.Value, "", Address & Address2)
After
        Address2 = IIf(Address2 Is DBNull.Value, "",  Address2)
        FinalAddress = IIf(Address Is DBNull.Value, Address2, Address & VbCrLf & Address2)

Because if address1 is blank and address 2 have a value then must show the address2 then

Actually I tried another approach by writting line code , but now I need to handle the (,) comma betwwen Last_name and First_Name ..  do you happen to know any idea,

  <b>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Last_Name") %>' Visible='<%# IIF(CONVERT.ToString(DataBinder.Eval(Container.DataItem, "Last_Name"))="",False,True)%>'/>
                           <%#If  %> ,
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("First_Name") %>' Visible='<%# IIF(CONVERT.ToString(DataBinder.Eval(Container.DataItem, "First_Name"))="",False,True)%>'/>
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("Prof_Designation") %>' Visible='<%# IIF(CONVERT.ToString(DataBinder.Eval(Container.DataItem, "Prof_Designation"))="",False,True)%>'/>
                        </b>

Open in new window

Put the , in the format of Eval like this
Eval(yourbinder,",{0}")
like this?

<asp:Label ID="Label6" runat="server" Text=     '<%# Eval(Address,",{0}")%>' />

Open in new window

Yes, this will add a , and behind the comma will be your value