Nested Repeater Control in ASP.Net 4.0

This article is basically an extension of my previous article. For those of you that have not seen my previous article, kindly have a look at the following link. It'll provide a basic idea of what a repeater control is all about.

http://www.c-sharpcorner.com/UploadFile/17e8f6/repeater-control-in-Asp-Net/

Toady in this article we are basically going to learn how to nest repeater controls within one another. The preceding link will help you with the basic concepts of what a repeater control is and which scenario is best for using a repeater control. Ok now without wasting my time and your time we will proceed to what and how to make use of a nested repeater control.
 
For instance let us say we are asked to create a report of a particular school student's Marksheet. Where the report will have a list of student data (such as their StudId, Rollno, Name etcetera) and below their Mark in the respective subject along with the total and their result, in other words whether they are int or not. In such cases, we could use a repeater control for displaying the report in a user specificed format.

NestedRepeater.jpg

From the preceding figure, you can see that we have a parent repeater control that is used for displaying the Student Releated Data (StudId, Name and RollNo) and an inner repeater control that is used for displaying the respective student Mark Details. In case of nested repeaters with a repeater control, you will encounter the problem that your nested control, in other words the inner repeater control, will not be accessbile to you directly in code behind. That is because it is placed inside the parent's repeater ItemTemplate. For accessing the inner repeater control you'll need to make use a RepeaterItem class that will help you to access the inner repeater control or any control that is placed inside the ItemTemplate or FooterTemplate or AlternatingItemTemplate class, by using the FindControl method of the RepeaterItem class.

The other thing that is interesting is that since the inner repeater control is also a Data Control, that means we will need to set its DataSource for displaying data within the inner repeater control. If you only set the Outer/Parent's DataSource property and you don't set the inner control's repeater control DataSource property then in only the parent repeater control data will be displayed and no data will be displayed in the child/inner control. As we discussed earlier, we can't directly access the inner repeater control directly in code behind. Then how to set its DataSource property? Well the answer is quite simply and logically since we know that we placed our inner repeater control inside the outer one's within the <ItemTemplate>. So we could always make use of the ItemDataBound event of the outer repeater control whereby we can find the inner Repetaer Control using the RepeaterItem class. The following snapshot shows that.

  1. protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  2. {  
  3.     if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)  
  4.     {  
  5.         //Accessing the inner repeater control on ItemBound event of Parent Repeater Control  
  6.         Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");  
  7.         //here e.Item basically refers to your RepeaterItem Class of the respective ItemTemplate  
  8.     }  
  9. }  

For accessing the elements within the inner Repeater Control we will be making use of an ItemDataBound event of the inner Repeater Control like the following.

  1. protected void innerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  2. {  
  3.     if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)  
  4.     {  
  5.         //Accessing the Labels control placed within the Child/Inner Repeater Control  
  6.         Label lblEnglish = (Label)e.Item.FindControl("lblEnglish");  
  7.         Label lblHindi = (Label)e.Item.FindControl("lblHindi");  
  8.         Label lblMarathi = (Label)e.Item.FindControl("lblMarathi");  
  9.         Label lblMaths = (Label)e.Item.FindControl("lblMaths");  
  10.         Label lblScience = (Label)e.Item.FindControl("lblScience");  
  11.         Label lblSocialScience = (Label)e.Item.FindControl("lblSocialScience");  
  12.         float english = float.Parse(lblEnglish.Text);  
  13.         float hindi = float.Parse(lblHindi.Text);  
  14.         float marathi = float.Parse(lblMarathi.Text);  
  15.         float maths = float.Parse(lblMaths.Text);  
  16.         float science = float.Parse(lblScience.Text);  
  17.         float socialScience = float.Parse(lblSocialScience.Text);  
  18.         if (english < 35)  
  19.             lblEnglish.CssClass = "failedClass";  
  20.         if (hindi < 35)  
  21.             lblHindi.CssClass = "failedClass";  
  22.         if (marathi < 35)  
  23.             lblMarathi.CssClass = "failedClass";  
  24.         if (maths < 35)  
  25.             lblMaths.CssClass = "failedClass";  
  26.         if (science < 35)  
  27.             lblScience.CssClass = "failedClass";  
  28.         if (socialScience < 35)  
  29.             lblSocialScience.CssClass = "failedClass";  
  30.         ViewState["Total"] = english + hindi + marathi + maths + science + socialScience;  
  31.         ViewState["Fail"] = english < 35 || hindi < 35 || marathi < 35 || science < 35 || maths < 35 || socialScience < 35 ? "Fail" : "";  
  32.     }  
  33.     else if (e.Item.ItemType == ListItemType.Footer)  
  34.     {  
  35.         if (ViewState["Total"] != null && ViewState["Fail"] != null)  
  36.         {  
  37.             Label lblTotal = (Label)e.Item.FindControl("lblTotal");  
  38.             lblTotal.Text = ViewState["Total"].ToString();  
  39.             Label lblResult = (Label)e.Item.FindControl("lblResult");  
  40.             lblResult.Text = ViewState["Fail"].ToString();  
  41.             lblResult.CssClass = ViewState["Fail"].ToString() == "" ? "edClass" : "failedClass";  
  42.         }  
  43.     }  
  44. } 

The following are the SQL Queries that contains the tables used, procedures used etcetera:

  1. create database Practice  
  2. use Practice  
  3. drop table Students  
  4. create table Students  
  5. (  
  6. StudId varchar(10),  
  7. RollNo int identity(1,1)not null,  
  8. StudName varchar(30),  
  9. StudAdd varchar(50),  
  10. StudCity varchar(50),  
  11. StudTel varchar(20),  
  12. constraint pkStudId primary key(StudId)  
  13. )  
  14. alter procedure prc_AddStudentDetails  
  15. (  
  16. @name varchar(30),  
  17. @add varchar(50),  
  18. @city varchar(50),  
  19. @telno varchar(20)  
  20. )  
  21. as  
  22. begin  
  23. declare @rno int  
  24. declare @studid varchar(10)  
  25. if(select COUNT(*) from students)>0  
  26. begin  
  27. select @rno=MAX(rollno)  
  28. from students  
  29. end  
  30. else  
  31. set @rno=0  
  32. set @studid='S00'+Convert(varchar,(@rno+1))  
  33. insert into Students(StudId,StudName,StudAdd,StudCity,StudTel)  
  34. values(@studid,@name,@add,@city,@telno)  
  35. end  
  36. prc_AddStudentDetails 'Vishal','Andheri','Mumbai','123456789'  
  37. prc_AddStudentDetails 'Dheeraj','Bhayandar','Mumbai','987654321'  
  38. prc_AddStudentDetails 'Shankar','Kalyan','Thane','456789123'  
  39. prc_AddStudentDetails 'Kailash','Santacruz','Mumbai','789123456'  
  40. prc_AddStudentDetails 'Jack','Dadar','Mumbai','852741963'  
  41. select * from Students  
  42. create table Marks  
  43. (  
  44. Rollno int,  
  45. English float,  
  46. Hindi float,  
  47. Marathi float,  
  48. Maths float,  
  49. Science float,  
  50. SocialScience float  
  51. )  
  52. select * from Marks  
  53. insert into Marks values(1,85,75,95,99,95,89)  
  54. insert into Marks values(2,65,85,88,83,98,80)  
  55. insert into Marks values(3,55,85,85,85,65,79)  
  56. insert into Marks values(4,95,90,90,88,75,99)  
  57. insert into Marks values(5,80,88,89,90,92,88)  
  58. create procedure prc_GetStudentMarksData  
  59. as  
  60. begin  
  61. select StudId,a.RollNo,StudName,English,Marathi,Maths,Science,SocialScience,Hindi  
  62. from Students a inner join Marks b  
  63. on a.RollNo=b.Rollno  
  64. end  
  65. create procedure prc_GetStudentData  
  66. as  
  67. begin  
  68. select * from Students  
  69. end  
  70. create procedure prc_GetStudentMarks  
  71. (  
  72. @rollno int  
  73. )  
  74. as  
  75. begin  
  76. select * from Marks  
  77. where Rollno=@rollno  
  78. end  
  79. update Marks  
  80. set SocialScience=23  
  81. where Rollno=5 

Let's check the design/mark up code for it.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6.     <link rel="Stylesheet" type="text/css" href="myStyle.css" />  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:Label ID="lblNoRecords" runat="server" Text="No Records Found" Visible="false"></asp:Label>  
  12.         <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">  
  13.             <HeaderTemplate>  
  14.                 <table class="w70 center">  
  15.                     <tr>  
  16.                         <td colspan="2" align="center" class="title">  
  17.                             Student's Report  
  18.                         </td>  
  19.                     </tr>  
  20.             </HeaderTemplate>  
  21.             <ItemTemplate>  
  22.                 <tr class="even">  
  23.                     <td>  
  24.                         Stud ID:  
  25.                         <asp:TextBox ID="txtStudID" Enabled="false" Text='<%#Eval("StudID") %>' runat="server"  
  26.                             CssClass="txtSmallEntry"></asp:TextBox>  
  27.                     </td>  
  28.                     <td>  
  29.                         Roll No:  
  30.                         <asp:TextBox ID="txtRollno" Enabled="false" Text='<%#Eval("Rollno") %>' runat="server"  
  31.                             CssClass="txtSmallEntry"></asp:TextBox>  
  32.                     </td>  
  33.                 </tr>  
  34.                 <tr class="even">  
  35.                     <td colspan="2">  
  36.                         Name:  
  37.                         <asp:TextBox ID="txtName" Enabled="false" Text='<%#Eval("StudName") %>' runat="server"  
  38.                             CssClass="txtEntry"></asp:TextBox>  
  39.                     </td>  
  40.                 </tr>  
  41.                 <tr class="even">  
  42.                     <td colspan="2">  
  43.                         <asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerRepeater_ItemDataBound">  
  44.                             <HeaderTemplate>  
  45.                                 <table class="w50 left10">  
  46.                             </HeaderTemplate>  
  47.                             <ItemTemplate>  
  48.                                 <tr>  
  49.                                     <td class="center">  
  50.                                         English:  
  51.                                     </td>  
  52.                                     <td>  
  53.                                         <asp:Label ID="lblEnglish" CssClass="edClass" runat="server" Text='<%#Eval("English") %>'></asp:Label>  
  54.                                     </td>  
  55.                                 </tr>  
  56.                                 <tr>  
  57.                                     <td>  
  58.                                         Hindi:  
  59.                                     </td>  
  60.                                     <td>  
  61.                                         <asp:Label ID="lblHindi" CssClass="edClass" runat="server" Text='<%#Eval("Hindi") %>'></asp:Label>  
  62.                                     </td>  
  63.                                 </tr>  
  64.                                 <tr>  
  65.                                     <td>  
  66.                                         Marathi:  
  67.                                     </td>  
  68.                                     <td>  
  69.                                         <asp:Label ID="lblMarathi" CssClass="edClass" runat="server" Text='<%#Eval("Marathi") %>'></asp:Label>  
  70.                                     </td>  
  71.                                 </tr>  
  72.                                 <tr>  
  73.                                     <td>  
  74.                                         Maths:  
  75.                                     </td>  
  76.                                     <td>  
  77.                                         <asp:Label ID="lblMaths" CssClass="edClass" runat="server" Text='<%#Eval("Maths") %>'></asp:Label>  
  78.                                     </td>  
  79.                                 </tr>  
  80.                                 <tr>  
  81.                                     <td>  
  82.                                         Science:  
  83.                                     </td>  
  84.                                     <td>  
  85.                                         <asp:Label ID="lblScience" CssClass="edClass" runat="server" Text='<%#Eval("Science") %>'></asp:Label>  
  86.                                     </td>  
  87.                                 </tr>  
  88.                                 <tr>  
  89.                                     <td>  
  90.                                         Social Science:  
  91.                                     </td>  
  92.                                     <td>  
  93.                                         <asp:Label ID="lblSocialScience" CssClass="edClass" runat="server" Text='<%#Eval("SocialScience") %>'></asp:Label>  
  94.                                     </td>  
  95.                                 </tr>  
  96.                             </ItemTemplate>  
  97.                             <FooterTemplate>  
  98.                                 <tr>  
  99.                                     <td align="right">  
  100.                                         Total:  
  101.                                         <asp:Label ID="lblTotal" CssClass="totalClass" runat="server"></asp:Label>  
  102.                                     </td>  
  103.                                     <td class="right">  
  104.                                         <asp:Label ID="lblResult" runat="server"></asp:Label>  
  105.                                     </td>  
  106.                                 </tr>  
  107.                                 </table>  
  108.                             </FooterTemplate>  
  109.                         </asp:Repeater>  
  110.                     </td>  
  111.                 </tr>  
  112.             </ItemTemplate>  
  113.             <AlternatingItemTemplate>  
  114.                 <tr class="odd">  
  115.                     <td>  
  116.                         Stud ID:  
  117.                         <asp:TextBox ID="txtStudID" Enabled="false" Text='<%#Eval("StudID") %>' runat="server"  
  118.                             CssClass="txtSmallEntry"></asp:TextBox>  
  119.                     </td>  
  120.                     <td>  
  121.                         Roll No:  
  122.                         <asp:TextBox ID="txtRollno" Enabled="false" Text='<%#Eval("Rollno") %>' runat="server"  
  123.                             CssClass="txtSmallEntry"></asp:TextBox>  
  124.                     </td>  
  125.                 </tr>  
  126.                 <tr class="odd">  
  127.                     <td colspan="2">  
  128.                         Name:  
  129.                         <asp:TextBox ID="txtName" Enabled="false" Text='<%#Eval("StudName") %>' runat="server"  
  130.                             CssClass="txtEntry"></asp:TextBox>  
  131.                     </td>  
  132.                 </tr>  
  133.                 <tr class="odd">  
  134.                     <td colspan="2">  
  135.                         <asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerRepeater_ItemDataBound">  
  136.                             <HeaderTemplate>  
  137.                                 <table class="w50 left10">  
  138.                             </HeaderTemplate>  
  139.                             <ItemTemplate>  
  140.                                 <tr>  
  141.                                     <td class="center">  
  142.                                         English:  
  143.                                     </td>  
  144.                                     <td>  
  145.                                         <asp:Label ID="lblEnglish" CssClass="edClass" runat="server" Text='<%#Eval("English") %>'></asp:Label>  
  146.                                     </td>  
  147.                                 </tr>  
  148.                                 <tr>  
  149.                                     <td>  
  150.                                         Hindi:  
  151.                                     </td>  
  152.                                     <td>  
  153.                                         <asp:Label ID="lblHindi" CssClass="edClass" runat="server" Text='<%#Eval("Hindi") %>'></asp:Label>  
  154.                                     </td>  
  155.                                 </tr>  
  156.                                 <tr>  
  157.                                     <td>  
  158.                                         Marathi:  
  159.                                     </td>  
  160.                                     <td>  
  161.                                         <asp:Label ID="lblMarathi" CssClass="edClass" runat="server" Text='<%#Eval("Marathi") %>'></asp:Label>  
  162.                                     </td>  
  163.                                 </tr>  
  164.                                 <tr>  
  165.                                     <td>  
  166.                                         Maths:  
  167.                                     </td>  
  168.                                     <td>  
  169.                                         <asp:Label ID="lblMaths" CssClass="edClass" runat="server" Text='<%#Eval("Maths") %>'></asp:Label>  
  170.                                     </td>  
  171.                                 </tr>  
  172.                                 <tr>  
  173.                                     <td>  
  174.                                         Science:  
  175.                                     </td>  
  176.                                     <td>  
  177.                                         <asp:Label ID="lblScience" CssClass="edClass" runat="server" Text='<%#Eval("Science") %>'></asp:Label>  
  178.                                     </td>  
  179.                                 </tr>  
  180.                                 <tr>  
  181.                                     <td>  
  182.                                         Social Science:  
  183.                                     </td>  
  184.                                     <td>  
  185.                                         <asp:Label ID="lblSocialScience" CssClass="edClass" runat="server" Text='<%#Eval("SocialScience") %>'></asp:Label>  
  186.                                     </td>  
  187.                                 </tr>  
  188.                             </ItemTemplate>  
  189.                             <FooterTemplate>  
  190.                                 <tr>  
  191.                                     <td align="right">  
  192.                                         Total:  
  193.                                         <asp:Label ID="lblTotal" CssClass="totalClass" runat="server"></asp:Label>  
  194.                                     </td>  
  195.                                     <td class="right">  
  196.                                         <asp:Label ID="lblResult" runat="server"></asp:Label>  
  197.                                     </td>  
  198.                                 </tr>  
  199.                                 </table>  
  200.                             </FooterTemplate>  
  201.                         </asp:Repeater>  
  202.                     </td>  
  203.                 </tr>  
  204.             </AlternatingItemTemplate>  
  205.             <FooterTemplate>  
  206.                 <tr>  
  207.                     <td colspan="2" align="center" style="margin-left: 3px;">  
  208.                         <asp:Button ID="btnFirst" OnClick="CommonButton_Click" runat="server" Text="<<" />  
  209.                         <asp:Button ID="btnPrevious" OnClick="CommonButton_Click" runat="server" Text="<" />  
  210.                         <asp:Button ID="btnNext" OnClick="CommonButton_Click" runat="server" Text=">" />  
  211.                         <asp:Button ID="btnLast" OnClick="CommonButton_Click" runat="server" Text=">>" />  
  212.                     </td>  
  213.                 </tr>  
  214.                 </table>  
  215.             </FooterTemplate>  
  216.         </asp:Repeater>  
  217.     </div>  
  218.     </form>  
  219. </body>  
  220. </html> 

Source Code for it

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Configuration;  
  8. using System.Data;  
  9. using System.Data.SqlClient;  
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     SqlConnection con;  
  13.     DataSet ds;  
  14.     SqlDataAdapter da;  
  15.     SqlCommand cmd;  
  16.     private string _dbCon = ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;  
  17.     //for paging within the repeater control  
  18.     PagedDataSource pageDataSource = new PagedDataSource();  
  19.     public int CurrentPage  
  20.     {  
  21.         get  
  22.         {  
  23.             if (ViewState["currentPage"] == null)  
  24.                 return 0;  
  25.             else  
  26.                 return Convert.ToInt32(ViewState["currentPage"].ToString());  
  27.         }  
  28.         set  
  29.         {  
  30.             ViewState["currentPage"] = value;  
  31.         }  
  32.     }  
  33.     protected void Page_Load(object sender, EventArgs e)  
  34.     {  
  35.         if (!IsPostBack)  
  36.             GetStudentData();  
  37.     }  
  38.     /// <summary>  
  39.     /// Get the Student Personal Data such as their ID,RollNo,  
  40.     /// Name for displaying in the parent control  
  41.     /// </summary>  
  42.     private void GetStudentData()  
  43.     {  
  44.         con = new SqlConnection(_dbCon);  
  45.         da = new SqlDataAdapter("prc_GetStudentData", con);  
  46.         da.SelectCommand.CommandType = CommandType.StoredProcedure;  
  47.         ds = new DataSet("myDs");  
  48.         da.Fill(ds);  
  49.         if (ds.Tables[0].Rows.Count > 0)  
  50.         {  
  51.             pageDataSource.DataSource = ds.Tables[0].DefaultView;  
  52.             pageDataSource.CurrentPageIndex = CurrentPage;  
  53.             pageDataSource.PageSize = 2;  
  54.             pageDataSource.AllowPaging = true;  
  55.             ViewState["totalPages"] = pageDataSource.PageCount;  
  56.             Repeater1.DataSource = pageDataSource;  
  57.             //on DataBind parent ItemDataBound event will be called  
  58.             Repeater1.DataBind();  
  59.             //For accessing the FooterTemplate controls.  
  60.             RepeaterItem repeaterItem = (RepeaterItem)Repeater1.Controls[Repeater1.Controls.Count - 1];  
  61.             Button btnFirst = (Button)repeaterItem.FindControl("btnFirst");  
  62.             Button btnPrevious = (Button)repeaterItem.FindControl("btnPrevious");  
  63.             Button btnNext = (Button)repeaterItem.FindControl("btnNext");  
  64.             Button btnLast = (Button)repeaterItem.FindControl("btnLast");  
  65.             btnPrevious.Enabled = btnFirst.Enabled = !pageDataSource.IsFirstPage;  
  66.             btnLast.Enabled = btnNext.Enabled = !pageDataSource.IsLastPage;  
  67.         }  
  68.         else  
  69.             lblNoRecords.Visible = true;  
  70.     }  
  71.     /// <summary>  
  72.     /// Getting respective Student Marks Details of the Student by using their rollno  
  73.     /// </summary>  
  74.     /// <param name="rollNo">RollNo</param>  
  75.     /// <returns>returns DataTable</returns>  
  76.     private DataTable GetStudentMarks(string rollNo)  
  77.     {  
  78.         con = new SqlConnection(_dbCon);  
  79.         da = new SqlDataAdapter("prc_GetStudentMarks", con);  
  80.         da.SelectCommand.CommandType = CommandType.StoredProcedure;  
  81.         da.SelectCommand.Parameters.AddWithValue("@rollno", rollNo);  
  82.         DataTable dt = new DataTable();  
  83.         da.Fill(dt);  
  84.         return dt;  
  85.     }  
  86.     /// <summary>  
  87.     /// Within this event we are trying to access the child or inner element of the Repeater Control  
  88.     /// by using FindControl method  
  89.     /// </summary>  
  90.     /// <param name="sender"></param>  
  91.     /// <param name="e"></param>  
  92.     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  93.     {  
  94.         if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)  
  95.         {  
  96.             TextBox lblRollNo = (TextBox)e.Item.FindControl("txtRollno");  
  97.             Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");  
  98.             DataTable dt = GetStudentMarks(lblRollNo.Text);  
  99.             innerRepeater.DataSource = dt;  
  100.             innerRepeater.DataBind();  
  101.         }  
  102.     }  
  103.     /// <summary>  
  104.     /// Childs ItemDataBound event for accessing the controls of the child/inner Repeater Control for performing some  
  105.     /// logic such as if the student fails then his/her data should be displayed in red color.  
  106.     /// </summary>  
  107.     /// <param name="sender"></param>  
  108.     /// <param name="e"></param>  
  109.     protected void innerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  110.     {  
  111.         if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)  
  112.         {  
  113.             Label lblEnglish = (Label)e.Item.FindControl("lblEnglish");  
  114.             Label lblHindi = (Label)e.Item.FindControl("lblHindi");  
  115.             Label lblMarathi = (Label)e.Item.FindControl("lblMarathi");  
  116.             Label lblMaths = (Label)e.Item.FindControl("lblMaths");  
  117.             Label lblScience = (Label)e.Item.FindControl("lblScience");  
  118.             Label lblSocialScience = (Label)e.Item.FindControl("lblSocialScience");  
  119.             float english = float.Parse(lblEnglish.Text);  
  120.             float hindi = float.Parse(lblHindi.Text);  
  121.             float marathi = float.Parse(lblMarathi.Text);  
  122.             float maths = float.Parse(lblMaths.Text);  
  123.             float science = float.Parse(lblScience.Text);  
  124.             float socialScience = float.Parse(lblSocialScience.Text);  
  125.             if (english < 35)  
  126.                 lblEnglish.CssClass = "failedClass";  
  127.             if (hindi < 35)  
  128.                 lblHindi.CssClass = "failedClass";  
  129.             if (marathi < 35)  
  130.                 lblMarathi.CssClass = "failedClass";  
  131.             if (maths < 35)  
  132.                 lblMaths.CssClass = "failedClass";  
  133.             if (science < 35)  
  134.                 lblScience.CssClass = "failedClass";  
  135.             if (socialScience < 35)  
  136.                 lblSocialScience.CssClass = "failedClass";  
  137.             ViewState["Total"] = english + hindi + marathi + maths + science + socialScience;  
  138.             ViewState["Fail"] = english < 35 || hindi < 35 || marathi < 35 || science < 35 || maths < 35 || socialScience < 35 ? "Fail" : "";  
  139.         }  
  140.         else if (e.Item.ItemType == ListItemType.Footer)  
  141.         {  
  142.             if (ViewState["Total"] != null && ViewState["Fail"] != null)  
  143.             {  
  144.                 Label lblTotal = (Label)e.Item.FindControl("lblTotal");  
  145.                 lblTotal.Text = ViewState["Total"].ToString();  
  146.                 Label lblResult = (Label)e.Item.FindControl("lblResult");  
  147.                 lblResult.Text = ViewState["Fail"].ToString();  
  148.                 lblResult.CssClass = ViewState["Fail"].ToString() == "" ? "edClass" : "failedClass";  
  149.             }  
  150.         }  
  151.     }  
  152.     /// <summary>  
  153.     /// Common Handler for all the button placed within the parent's footer row  
  154.     /// </summary>  
  155.     /// <param name="sender"></param>  
  156.     /// <param name="e"></param>  
  157.     protected void CommonButton_Click(object sender, EventArgs e)  
  158.     {  
  159.         Button b = (Button)sender;  
  160.         switch (b.ID)  
  161.         {  
  162.             case "btnFirst":  
  163.                 CurrentPage = 0;  
  164.                 GetStudentData();  
  165.                 break;  
  166.             case "btnPrevious":  
  167.                 CurrentPage -= 1;  
  168.                 GetStudentData();  
  169.                 break;  
  170.             case "btnNext":  
  171.                 CurrentPage += 1;  
  172.                 GetStudentData();  
  173.                 break;  
  174.             case "btnLast":  
  175.                 if (ViewState["totalPages"] != null)  
  176.                 {  
  177.                     CurrentPage = Convert.ToInt32(ViewState["totalPages"].ToString()) - 1;  
  178.                     GetStudentData();  
  179.                 }  
  180.                 break;  
  181.         }  
  182.     }  
  183. } 

Finally it is time to check the output of it.

NestedRepeater3.jpg

You could make use of Pagination also by simply clicking the next or last button on the first page or else the previous and first page if on the middle pages.

Where the Student Fails his/her subject then marks are displayed in the red color and also the /fail flag is displayed in the red color.

NestedRepeater4.jpg


NestedRepeater5.jpg


Hope you liked the article.


Similar Articles