Solved

Checking value before databinding

Posted on 2011-03-04
4
500 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
  • 2
  • 2
4 Comments
 

Author Comment

by:pratz09
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect Answer ! Thank you so much.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

14 Experts available now in Live!

Get 1:1 Help Now