Solved

Checking value before databinding

Posted on 2011-03-04
4
521 Views
Last Modified: 2012-05-11
Hi !

I have written this Databinding code to display location information.  

                    <%# DataBinder.Eval(Container.DataItem, "name") %><br />
                    <%# DataBinder.Eval(Container.DataItem, "Address") %><br>
                    <%# DataBinder.Eval(Container.DataItem, "City") %>,
                    <%# DataBinder.Eval(Container.DataItem, "State") %>
                    <%# DataBinder.Eval(Container.DataItem, "zip") %><br /><br />
                    Local: <%# DataBinder.Eval(Container.DataItem, "local") %><br />
                    Toll Free: <%# DataBinder.Eval(Container.DataItem, "tollfree") %><br />
                    Fax: <%# DataBinder.Eval(Container.DataItem, "fax") %><br /><br />
                    <%# DataBinder.Eval(Container.DataItem, "businesshours") %>

Now, before actually rendering the "Binded" value to the webpage (as html), I want to do a check if the Dynamic (or binded) value is null, if it is indeed null, I do not want to display it. What would be the most cost efficient way of doing it.

Thanks,
0
Comment
Question by:pratz09
[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
  • 2
  • 2
4 Comments
 

Author Comment

by:pratz09
ID: 35035150
I am sorry I missed mentioning one more thing. I am using repeater control to display the data items.

<asp:repeater id="rptHouses" runat="server">
            <ItemTemplate>
                <p>
                    <strong>
                    <%# DataBinder.Eval(Container.DataItem, "name") %></strong><br />
                    <%# DataBinder.Eval(Container.DataItem, "Address") %><br>
                    <%# DataBinder.Eval(Container.DataItem, "City") %>,
                    <%# DataBinder.Eval(Container.DataItem, "State") %>
                    <%# DataBinder.Eval(Container.DataItem, "zip") %><br /><br />
                    Local: <%# DataBinder.Eval(Container.DataItem, "local") %><br />
                    Toll Free: <%# DataBinder.Eval(Container.DataItem, "tollfree") %><br />
                    Fax: <%# DataBinder.Eval(Container.DataItem, "fax") %><br /><br />
                    <%# DataBinder.Eval(Container.DataItem, "businesshours") %>
                    <a href="/search-inventory.aspx">Search Inventory</a>
                </p>
            </ItemTemplate>
        </asp:repeater>

DataSource is SQL Server Table. I want to check if Local, Toll Free or Fax numbers are null, then DO NOT DISPLAY them on web page. I am confused since we usually just bind the data to datasource in code behind file, but I never actually checked the values of selective elements from datasource. Do we need to do it before binding (since it is repeater control) ? Or after binding but before rendering it as html ?

Thanks.
0
 
LVL 14

Accepted Solution

by:
Dhanasekaran Sengodan earned 500 total points
ID: 35035249
create a table inside repeater control and validate values in ItemDataBound event.

.cs (code behind)

=======================


 protected void rptHouses_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
        {
            if (string.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "local")))
            {
                HtmlTableRow Trrow = (HtmlTableRow)e.Item.FindControl("trlocal");
                Trrow.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "tollfree")))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trtollfree");
                Trrow1.Attributes.Add("style", "display:none");
            }
            if (string.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "fax")))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trfax");
                Trrow1.Attributes.Add("style", "display:none");
            }    
        }
    }

Open in new window

aspx
=========


 <asp:repeater id="rptHouses" runat="server" OnItemDataBound="rptHouses_OnItemDataBound">
            <HeaderTemplate>
            <table>
            </HeaderTemplate>
            <ItemTemplate>
                <p>
                    <strong>
                    
                    <div>
                    <table width="100%">
                    <tr><td><%# DataBinder.Eval(Container.DataItem, "name") %></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "Address")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "City")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "State")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "zip")%></strong><br /></td></tr>
                     <tr id="trlocal"><td>  Local:  <%# DataBinder.Eval(Container.DataItem, "local")%></strong><br /></td></tr>
                     <tr id="trtollfree" runat="server"><td>  Toll Free: <%# DataBinder.Eval(Container.DataItem, "tollfree")%></strong><br /></td></tr>
                     <tr id="trfax" runat="server"><td>  Fax: <%# DataBinder.Eval(Container.DataItem, "fax")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "businesshours")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "name") %></strong><br /></td></tr>
                     <tr><td><a href="/search-inventory.aspx">Search Inventory</a></td></tr>
                    </table>
                    
                    </div>
                </p>
            </ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
        </asp:repeater>

Open in new window

0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 35035281
Another solution
=============


code behind(.cs)
======================

 if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
        {
            if (string.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "local")))
            {
                HtmlTableRow Trrow = (HtmlTableRow)e.Item.FindControl("trlocal");
                Trrow.Visible = false;
            }
            if (string.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "tollfree")))
            {
                HtmlTableRow Trrow1 = (HtmlTableRow)e.Item.FindControl("trtollfree");
                Trrow1.Visible = false;
            }
            if (string.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "fax")))
            {
                HtmlTableRow Trrow2 = (HtmlTableRow)e.Item.FindControl("trfax");
                Trrow2.Visible = false;
            }    
        }


.aspx file
=========

<asp:repeater id="rptHouses" runat="server" OnItemDataBound="rptHouses_OnItemDataBound">
            <HeaderTemplate>
            <table>
            </HeaderTemplate>
            <ItemTemplate>
                <p>
                    <strong>
                    
                    <div>
                    <table width="100%">
                    <tr><td><%# DataBinder.Eval(Container.DataItem, "name") %></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "Address")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "City")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "State")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "zip")%></strong><br /></td></tr>
                     <tr id="trlocal" runat="server"><td>  Local:  <%# DataBinder.Eval(Container.DataItem, "local")%></strong><br /></td></tr>
                     <tr id="trtollfree" runat="server"><td>  Toll Free: <%# DataBinder.Eval(Container.DataItem, "tollfree")%></strong><br /></td></tr>
                     <tr id="trfax" runat="server"><td>  Fax: <%# DataBinder.Eval(Container.DataItem, "fax")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "businesshours")%></strong><br /></td></tr>
                     <tr><td><%# DataBinder.Eval(Container.DataItem, "name") %></strong><br /></td></tr>
                     <tr><td><a href="/search-inventory.aspx">Search Inventory</a></td></tr>
                    </table>
                    
                    </div>
                </p>
            </ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
        </asp:repeater>

Open in new window

0
 

Author Closing Comment

by:pratz09
ID: 35035396
Perfect Answer ! Thank you so much.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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