?
Solved

Checking value before databinding

Posted on 2011-03-04
4
Medium Priority
?
532 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 2000 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

752 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