Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2010-11-15
Medium Priority
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

Question by:vcbertini
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
LVL 23

Accepted Solution

Saqib Khan earned 750 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 ==
            if(DataBinder.Eval(e.Item.DataItem, "Id") == null) {
                 YOUR CODE

where rptProducts is the name of the repeater Item

Author Comment

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)

Open in new window

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?

Author Comment

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" 
        SelectCommand="sp_degreeRecord" SelectCommandType="StoredProcedure">       
<div id="CenterColumn">
<form runat="server">
<asp:Repeater ID="degreeDetail" runat="server" DataSourceID="dsDegree" OnItemDataBound="R1_ItemDataBound"> 
    <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:Repeater ID="rptCourseInfo" runat="server" DataSourceID="dsCourses">
        <HeaderTemplate><table width="95%" cellpadding="3" cellspacing="0" border="0" align="center"></HeaderTemplate>
        <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>
    <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>

Open in new window


Author Closing Comment

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.

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

610 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