Solved

Can't get nested repeater to populate

Posted on 2010-11-30
12
636 Views
Last Modified: 2012-06-27
I have a page I'm programming that has a nested repeater like this:

Repeater 1 - Heading - Degree Name
some information about the degree.
some more information about the degree.

Nested Repeater2 - Required Courses
- COURSE A
- COURSE B
- COURSE C

Reapeater 1 - Heading 2 - Thesis Option
some information here
some more information here

Nested Repeater 2 - Required courses
- COURSE D
- COURSE E
- COURSE F

I need to do things to the courses in the back end, like make them visible, or hide them based on if there's a link or not.   I am populating the reader and the repeater in the code-behind, but I think I'm doing this in the wrong event handlers because the page is ignoring the event code completely.


<asp:Repeater ID="degreeDetail" runat="server" DataSourceID="dsDegree" OnItemDataBound="Repeater1_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:Repeater ID="courseDetail" runat="server" OnItemDatabound="Repeater2_ItemDataBound">
        <HeaderTemplate>
            <table width="95%" cellpadding="3" cellspacing="0" border="0" align="center">
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Literal ID="litCategory" runat="server" Text=""></asp:Literal>
            <asp:Literal ID="litSubcategory" runat="server" Text=""></asp:Literal>
            <tr>
            <td width="5%">&nbsp;</td>
            <td align="left" valign="top" width="15%">
            <asp:Hyperlink ID="hlCourse" runat="server"></asp:Hyperlink>
            </td>
            <td align="left" valign="top" width="70%">
                <asp:Label ID="lblCourseName" runat="server" Text="" Visible="false"></asp:Label>
                <asp:HyperLink ID="hlCourseName" runat="server"></asp:HyperLink></td>
            <td align="left" valign="top" width="10%" nowrap="nowrap">.....<asp:Label ID="lblCredits" runat="server" Text=""></asp:Label></td>
           
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
        </asp:Repeater> 
    <p></p>
    <p><asp:Literal ID="litOther" runat="server" Text='<%# Eval("otherInfo") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblCertificationHdr" runat="server" Text="Certification" Visible="false"></asp:Label></h2>
    <p><asp:Literal ID="litCertification" runat="server" Text='<%# Eval("certification") %>'></asp:Literal></p>
    </ItemTemplate>
    </asp:Repeater>

Open in new window

protected void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string adm = DataBinder.Eval(e.Item.DataItem, "admission").ToString();
                if ((adm == "") || (adm == null))
                {
                    ((Label)e.Item.FindControl("lblAdmissionHdr")).Visible = false;
                }
                string st = DataBinder.Eval(e.Item.DataItem, "standards").ToString();
                if ((st == "") || (st == null))
                {
                    ((Label)e.Item.FindControl("lblStandardsHdr")).Visible = false;
                }
                string advising = DataBinder.Eval(e.Item.DataItem, "advising").ToString();
                if ((advising == "") || (advising == null))
                {
                    ((Label)e.Item.FindControl("lblAdvisingHdr")).Visible = false;
                }
                string prereq = DataBinder.Eval(e.Item.DataItem, "prerequisites").ToString();
                if ((prereq == "") || (prereq == null))
                {
                    ((Label)e.Item.FindControl("lblPrerequisitesHdr")).Visible = false;
                }
                string req = DataBinder.Eval(e.Item.DataItem, "requirements").ToString();
                if ((req == "") || (req == null))
                {
                    ((Label)e.Item.FindControl("lblRequirementsHdr")).Visible = false;
                }
                string cert = DataBinder.Eval(e.Item.DataItem, "certification").ToString();
                if ((cert == "") || (cert == null))
                {
                    ((Label)e.Item.FindControl("lblCertificationHdr")).Visible = false;
                }
            }
        }
        protected void Repeater2_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            string degreeID = DataBinder.Eval(e.Item.DataItem, "ID").ToString();

            string connStr = ConfigurationManager.ConnectionStrings["ruUtility"].ToString();
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("sp_courseRecords", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ID", degreeID);

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            Repeater cd = ((Repeater)e.Item.FindControl("courseDetail"));
            cd.DataSource = reader;

            //cd.DataBind();

            /* Course data returned:
             * subjectID, courseNumber, courseName, credits, category, subcategory */

            while (reader.Read())
            {
                string category = reader["category"].ToString();
                string subcat = reader["subcategory"].ToString();
                //set up counter
                //if counter = 0 then move category/subcategory into session
                //check for equality between session & reader for cat & subcat
                //if unequal, move new value into session

                // CATEGORY HEADING
                if ((category == "") || (category == "0"))
                {
                    ((Literal)e.Item.FindControl("litCategory")).Visible = false;
                }
                else
                {
                    ((Literal)e.Item.FindControl("litCategory")).Visible = true;
                    ((Literal)e.Item.FindControl("litCategory")).Text = "<tr><td colspan='4' align='left'><h2>" +
                        category + "</h2></td></tr>";
                }
                // SUBCATEGORY HEADING
                if ((subcat == "") || (subcat == "0"))
                {
                    ((Literal)e.Item.FindControl("litSubcategory")).Visible = false;
                }
                else
                {
                    ((Literal)e.Item.FindControl("litSubcategory")).Visible = true;
                    ((Literal)e.Item.FindControl("litSubcategory")).Text = "<tr><td width='5%'>&nbsp;</td><td colspan='3' align='left'><h3><i>" +
                       subcat + "</i></h3></td></tr>";
                }
                // COURSE ROWS
                string subj = reader["subjectID"].ToString();
                string course = reader["courseNumber"].ToString();
                string cname = reader["courseName"].ToString();
                string credits = reader["credits"].ToString();

                if ((subj == "") || (subj == "0") || (course == "") || (course == "0"))
                {
                    ((HyperLink)e.Item.FindControl("hlCourse")).Visible = false;
                    ((HyperLink)e.Item.FindControl("hlCourseName")).Visible = false;
                    // No link, just course name
                    ((Label)e.Item.FindControl("lblCourseName")).Text = cname;
                    ((Label)e.Item.FindControl("lblCourseName")).Visible = true;
                }
                else
                {
                    // Show linked subject & course
                    ((HyperLink)e.Item.FindControl("hlCourse")).Text = subj + " " + course;
                    ((HyperLink)e.Item.FindControl("hlCourse")).NavigateUrl =
                        Session["path"] + "Course.aspx?subj=" + subj + "&num=" + course;
                    ((HyperLink)e.Item.FindControl("hlCourse")).Visible = true;
                    // Hide unlinked course name
                    ((Label)e.Item.FindControl("lblCourseName")).Visible = false;
                }
                if ((cname == "") || (cname == "0"))
                {
                    ((HyperLink)e.Item.FindControl("hlCourseName")).Visible = false;
                }
                else
                {
                    // Show linked course name
                    ((HyperLink)e.Item.FindControl("hlCourseName")).Text = cname;
                    ((HyperLink)e.Item.FindControl("hlCourseName")).Visible = true;
                    ((HyperLink)e.Item.FindControl("hlCourseName")).NavigateUrl =
                        Session["path"] + "Course.aspx?subj=" + subj + "&num=" + course;
                }

                if ((credits == "") || (credits == "0"))
                {
                    ((Label)e.Item.FindControl("lblCredits")).Visible = false;
                }
                else
                {
                    ((Label)e.Item.FindControl("lblCredits")).Visible = true;
                    ((Label)e.Item.FindControl("lblCredits")).Text = credits;
                }

            }
            reader.Close();
            conn.Close();
        }

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
  • 6
  • 6
12 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
ID: 34240269
Inside Repeater1_ItemDataBound, set the datasource for courseDetail repeater:
   
     ... code to get data for repeater ...
     ...............................................
     Repeater cd = ((Repeater)e.Item.FindControl("courseDetail"));
     cd.DataSource = reader;
     cd.DataBind();
 
Inside Repeater2_ItemDataBound(), remove the code that moved to Repeater1_ItemDataBound()
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34240289
The bottom line is that if there is no datasouce bound, the event ItemDataBound is not fired. Therefore, you cannot set a repeater's datasouce inside its own ItemDataBound event handler.
0
 

Author Comment

by:vcbertini
ID: 34240302
If I move the reader into Repeater1_ItemDataBound, then it is not recognized in Repeater2_ItemDataBound
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 11

Expert Comment

by:jasonduan
ID: 34240326
do not use reader, use datatable instead.
0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34240342
the reason being datareader is an "active" connection to database, the datatable or dataset is in disconnected mode.
0
 

Author Comment

by:vcbertini
ID: 34240536
Ok. I can try that - not sure how to create a data table (still new at C#, but will look that up). How do you bind the repeater to the data table?
0
 

Author Comment

by:vcbertini
ID: 34240670
Oops, not the repeater, I mean, how do you the data table with the data from the database? Do you have to use a reader for that?
0
 

Author Comment

by:vcbertini
ID: 34240681
Man, I gotta lay off the cold medicine. "how do you FILL the data table with the data from the database? Do you have to use a reader for that? "
0
 
LVL 11

Assisted Solution

by:jasonduan
jasonduan earned 500 total points
ID: 34240795
0
 

Author Comment

by:vcbertini
ID: 34241347
I think I have just programmed myself into a corner. I can't figure out how to get the data out of my table.  I populated the table with the reader (couldn't figure out how to use the adapter), and now I can't figure out, no matter how much I read, how to pull the data out.  Everyone in every article, website, forum, etc. seems to have their own method of doing things, binding to grids, etc. and it doesn't seem to apply to what I'm trying to do.  Whatever I try is not finding the table name outside of the function it was created in.
protected void Repeater2_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            /* Course data returned:
             * subjectID, courseNumber, courseName, credits, category, subcategory */

            for (DataRow row.Read())
            {
//no idea what to put here
}

Open in new window

string connStr = ConfigurationManager.ConnectionStrings["ruUtility"].ToString();
                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand("sp_courseRecords", conn);
                cmd.Parameters.AddWithValue("@ID", degreeID);

                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader()

                DataTable myTable = new DataTable();
                while(reader.Read())
                {
                    DataRow row;
                    row = myTable.NewRow();

                    row["subjectID"] = reader["subjectID"].ToString();
                    row["courseNumber"] = reader["courseNumber"].ToString();
                    row["courseName"] = reader["courseName"];
                    row["credits"] = reader["credits"];
                    row["category"] = reader["category"];
                    row["subcategory"] = reader["subcategory"];

                    myTable.Rows.Add(row);    
                }

                Repeater cd = ((Repeater)e.Item.FindControl("courseDetail"));
                cd.DataSource = myTable;
                cd.DataBind();

                reader.Close();
                conn.Close();

Open in new window

0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34242226
I suggest you isolate the issues and resolve them one by one.
It would be easier for you to simply create a "fake" data table with a few sample rows to make the repeater work. Once the repeater is working, you can focus on how to populate the data table.
It is very.
Keep it cool and stay focus, you'll get it working.
0
 

Author Closing Comment

by:vcbertini
ID: 34242331
Still trying to figure it out, but closer.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

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