Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 30
tableview is not updating 1 21
Unable to connect C# program to an SQL database - Exception occurs. 4 48
Problem to Office 1 15
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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