Solved

Checking value before databinding

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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