Solved

Using a repeater - need to check for null Eval() values

Posted on 2010-11-15
5
1,129 Views
Last Modified: 2012-05-10
I have a repeater on my page, and I need to check in the code-behind to see if any of the Eval("fields") are null. Based on the presence or absence of data in the databound field, I need to turn the literal and it's associated label on the web page to visible = true (if there's data present) or visible=false (if there's no data in that field), so that only the fields with data  and their associated headers show on the page.

I don't know if I've just been staring at this for too long or what, but nothing seems to make sense.

I'm using C#, so an example in C# would be appreciated. I've searched the net and found a few VB examples, but they didn't really seem relevant. Some say to check it in the <%# Eval("field") %> area, some say to write a custom function. I'd rather keep it simple and just do this in the code behind if possible (see code example - I know the syntax is not correct, but it's for illustrative purposes).


if (Eval("description") == null)
            {
                ((Literal)Page.FindControl("litDescription")).Visible = false;
            }
            if (Eval("admission") == null)
            {
                ((Literal)Page.FindControl("litAdmission")).Visible = false;
                ((Label)Page.FindControl("lbladmissionHdr")).Visible = false;
            }

Open in new window

0
Comment
Question by:vcbertini
[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
  • 3
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
Saqib Khan earned 250 total points
ID: 34139436
you need to do this within an event

protected void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
          ListItemType.AlternatingItem)
        {
            if(DataBinder.Eval(e.Item.DataItem, "Id") == null) {
                 YOUR CODE
           }
        }
    }

where rptProducts is the name of the repeater Item
0
 

Author Comment

by:vcbertini
ID: 34139576
I am trying that, and getting this error when I call the event:

CS1061: 'ASP.layouts_catalogdegree_ascx' does not contain a definition for 'R1_ItemDataBound' and no extension method 'R1_ItemDataBound' accepting a first argument of type 'ASP.layouts_catalogdegree_ascx' could be found (are you missing a using directive or an assembly reference?)

Am I calling this wrong?
<asp:Repeater ID="degreeDetail" runat="server" DataSourceID="dsDegree" OnItemDataBound="R1_ItemDataBound">

Open in new window

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
// SOME LOGIC HERE
        }

Open in new window

0
 
LVL 23

Expert Comment

by:Saqib Khan
ID: 34139679
So your repeater is within a User Control and R1_ItemDataBound is within the Code Behind of User Control file?
0
 

Author Comment

by:vcbertini
ID: 34139745
Yes, the repeater is on my catalogDegree.ascx page and the R1_ItemDataBound is in the code behind at catalogDegree.ascx.cs  I've attached some more detailed code below.

Should I make this a public void.... or does that not matter?
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                //            if (((Evaluation)e.Item.DataItem).Rating == "Good") {
                //               ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
                //            }

                // Find the Inner DataSource control in this Row. 
                Repeater rpt = (Repeater)e.Item.FindControl("rptCourseInfo");
                SqlDataSource c = (SqlDataSource)rpt.FindControl("dsCourses");

                // Create the parameter for the child datasource
                c.SelectParameters.Add(new Parameter("ID", TypeCode.Int32));

                // Set the SelectParameter for this DataSource control by re-evaluating the field that is to be passed.
                c.SelectParameters["ID"].DefaultValue = DataBinder.Eval(e.Item.DataItem, "ID").ToString();
            }
        }

Open in new window

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CatalogDegree.ascx.cs" Inherits="xxx.layouts.catalogDegreeSublayout" %> 
 
    <asp:SqlDataSource ID="dsDegree" runat="server" 
        ConnectionString="" 
        SelectCommand="sp_degreeRecord" SelectCommandType="StoredProcedure">       
    </asp:SqlDataSource>
       
<div id="CenterColumn">
<form runat="server">
<asp:Repeater ID="degreeDetail" runat="server" DataSourceID="dsDegree" OnItemDataBound="R1_ItemDataBound"> 
<ItemTemplate>
    <asp:HiddenField ID="hdnID" runat="server" Value='<%# Eval("ID") %>' />
    <h1><asp:Label ID="lblDegreeName" runat="server" Text='<%# Eval("degreeName") %>'></asp:Label></h1>
    <p><asp:Literal ID="litDescription" runat="server" Text='<%# Eval("description") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblAdmissionHdr" runat="server" Text="Admission"></asp:Label></h2>
    <p><asp:Literal ID="litAdmission" runat="server" Text='<%# Eval("admission") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblStandardsHdr" runat="server" Text="Standards"></asp:Label></h2>
    <p><asp:Literal ID="litStandards" runat="server" Text='<%# Eval("standards") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblAdvisingHdr" runat="server" Text="Advising"></asp:Label></h2>
    <p><asp:Literal ID="litAdvising" runat="server" Text='<%# Eval("advising") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblPrerequisitesHdr" runat="server" Text="Prerequisites"></asp:Label></h2>
    <p><asp:Literal ID="litPrerequisites" runat="server" Text='<%# Eval("prerequisites") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblRequirementsHdr" runat="server" Text="Requirements"></asp:Label></h2>
    <p><asp:Literal ID="litRequirements" runat="server" Text='<%# Eval("requirements") %>'></asp:Literal></p>
    
        <asp:SqlDataSource ID="dsCourses" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ruUtility %>" 
        SelectCommand="sp_courseRecords" SelectCommandType="StoredProcedure">     
        </asp:SqlDataSource> 
    
        <asp:Repeater ID="rptCourseInfo" runat="server" DataSourceID="dsCourses">
        <HeaderTemplate><table width="95%" cellpadding="3" cellspacing="0" border="0" align="center"></HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td width="5%">&nbsp;</td>
        <td width="15%"><%# Eval("subjectID") %> <%# Eval("courseNumber") %></td>
        <td align="left" valign="top" width="70%"><%# Eval("courseName") %></td>
        <td align="left" valign="top" width="10%">.....<%# Eval("credits") %></td>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>
        
    <p><asp:Literal ID="litOther" runat="server" Text='<%# Eval("otherInfo") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblCertificationHdr" runat="server" Text="Certification"></asp:Label></h2>
    <p><asp:Literal ID="litCertification" runat="server" Text='<%# Eval("certification") %>'></asp:Literal></p>
    </ItemTemplate>
    </asp:Repeater>
</form>
    </div>

Open in new window

0
 

Author Closing Comment

by:vcbertini
ID: 34159066
I found the problem - apparently when my fields are empty, they're not returning a null, which is what I was checking for. They are returning "".  When I changed my IF statement to check for both "" and null, then it started working.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

738 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