vcbertini
asked on
Using a repeater - need to check for null Eval() values
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).
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So your repeater is within a User Control and R1_ItemDataBound is within the Code Behind of User Control file?
ASKER
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?
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();
}
}
<%@ 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%"> </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>
ASKER
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.
ASKER
CS1061: 'ASP.layouts_catalogdegree
Am I calling this wrong?
Open in new window
Open in new window