• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

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

0
TonyReba
Asked:
TonyReba
  • 6
  • 4
1 Solution
 
Jesus RodriguezIT ManagerCommented:
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
 
TonyRebaAuthor Commented:
this looks more a vbscript , can I do it on code behind?
0
 
Jesus RodriguezIT ManagerCommented:
I think that you must do it at the time that the control is binding but you can try at the code behind
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TonyRebaAuthor Commented:
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
 
Jesus RodriguezIT ManagerCommented:
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
 
Jesus RodriguezIT ManagerCommented:
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
 
TonyRebaAuthor Commented:
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
 
Jesus RodriguezIT ManagerCommented:
Put the , in the format of Eval like this
Eval(yourbinder,",{0}")
0
 
TonyRebaAuthor Commented:
like this?

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

Open in new window

0
 
Jesus RodriguezIT ManagerCommented:
Yes, this will add a , and behind the comma will be your value
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now