Solved

how to habdle Eval("Address")  blanks

Posted on 2010-09-06
10
400 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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 500 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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