Solved

how to habdle Eval("Address")  blanks

Posted on 2010-09-06
10
396 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:TonyReba
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
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
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
this looks more a vbscript , can I do it on code behind?
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
I think that you must do it at the time that the control is binding but you can try at the code behind
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
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

0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
Comment Utility
Soory I forgot to put runat="server" on the script. here is the correct script an I do it with an example that I create right now

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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<script language="vbscript" type="text/VB" runat="server">

 Function CheckAddress(byVal Address,ByVal Address2)

   Dim FinalAddress

        Address2 = IIf(Address2 Is DBNull.Value, "", vbCrLf & Address2)

        FinalAddress = IIf(Address Is DBNull.Value, "", Address & Address2)

   return FinalAddress

 End Function

</script> 

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

        AutoGenerateColumns="False" DataSourceID="SqlDataSource1">

      <Columns>

            <asp:CommandField ShowSelectButton="True" />

            <asp:TemplateField HeaderText="ADDRESS" SortExpression="ADDRESS3">

                <EditItemTemplate>

                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ADDRESS3") %>'></asp:TextBox>

                </EditItemTemplate>

                <ItemTemplate>

                    <asp:Label ID="Label1" runat="server" Text='<%# CheckAddress(Eval("ADDRESS1"),Eval("ADDRESS2")) %>'></asp:Label>

                </ItemTemplate>

            </asp:TemplateField>

            <asp:BoundField DataField="CITY" HeaderText="CITY" SortExpression="CITY" />

            <asp:BoundField DataField="STATE" HeaderText="STATE" SortExpression="STATE" />

            <asp:BoundField DataField="ZIP" HeaderText="ZIP" SortExpression="ZIP" />

            <asp:BoundField DataField="Job #" HeaderText="Job #" SortExpression="Job #" />

        </Columns>

    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FlatLinerConnectionString %>"

        SelectCommand="SELECT TOP 100 ADDRESS1, ADDRESS2, ADDRESS3, CITY, STATE, ZIP, [Job #] FROM Prospect">

    </asp:SqlDataSource>

</asp:Content>

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
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

0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
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

0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
Put the , in the format of Eval like this
Eval(yourbinder,",{0}")
0
 
LVL 9

Author Comment

by:TonyReba
Comment Utility
like this?

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

Open in new window

0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
Comment Utility
Yes, this will add a , and behind the comma will be your value
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now