Solved

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

Posted on 2010-11-15
5
1,121 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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ Select all records by MAX data syntax help 1 27
asp.net mvc 2 33
How to escape alias names for Oracle query 1 25
Build a string of emails from a gridview 2 15
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

756 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