Solved

Checking value before databinding

Posted on 2011-03-04
4
506 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
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 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

10 Experts available now in Live!

Get 1:1 Help Now