Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to habdle Eval("Address")  blanks

Posted on 2010-09-06
10
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33612908
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
ID: 33612921
this looks more a vbscript , can I do it on code behind?
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33612963
I think that you must do it at the time that the control is binding but you can try at the code behind
0
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
LVL 9

Author Comment

by:TonyReba
ID: 33613025
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 2000 total points
ID: 33613102
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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33613126
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
ID: 33613171
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
ID: 33613540
Put the , in the format of Eval like this
Eval(yourbinder,",{0}")
0
 
LVL 9

Author Comment

by:TonyReba
ID: 33613781
like this?

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

Open in new window

0
 
LVL 13

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
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.

618 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