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.
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.
- protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
- {
- if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
- {
-
- Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");
-
- }
- }
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.
- protected void innerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
- {
- if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
- {
-
- Label lblEnglish = (Label)e.Item.FindControl("lblEnglish");
- Label lblHindi = (Label)e.Item.FindControl("lblHindi");
- Label lblMarathi = (Label)e.Item.FindControl("lblMarathi");
- Label lblMaths = (Label)e.Item.FindControl("lblMaths");
- Label lblScience = (Label)e.Item.FindControl("lblScience");
- Label lblSocialScience = (Label)e.Item.FindControl("lblSocialScience");
- float english = float.Parse(lblEnglish.Text);
- float hindi = float.Parse(lblHindi.Text);
- float marathi = float.Parse(lblMarathi.Text);
- float maths = float.Parse(lblMaths.Text);
- float science = float.Parse(lblScience.Text);
- float socialScience = float.Parse(lblSocialScience.Text);
- if (english < 35)
- lblEnglish.CssClass = "failedClass";
- if (hindi < 35)
- lblHindi.CssClass = "failedClass";
- if (marathi < 35)
- lblMarathi.CssClass = "failedClass";
- if (maths < 35)
- lblMaths.CssClass = "failedClass";
- if (science < 35)
- lblScience.CssClass = "failedClass";
- if (socialScience < 35)
- lblSocialScience.CssClass = "failedClass";
- ViewState["Total"] = english + hindi + marathi + maths + science + socialScience;
- ViewState["Fail"] = english < 35 || hindi < 35 || marathi < 35 || science < 35 || maths < 35 || socialScience < 35 ? "Fail" : "";
- }
- else if (e.Item.ItemType == ListItemType.Footer)
- {
- if (ViewState["Total"] != null && ViewState["Fail"] != null)
- {
- Label lblTotal = (Label)e.Item.FindControl("lblTotal");
- lblTotal.Text = ViewState["Total"].ToString();
- Label lblResult = (Label)e.Item.FindControl("lblResult");
- lblResult.Text = ViewState["Fail"].ToString();
- lblResult.CssClass = ViewState["Fail"].ToString() == "" ? "edClass" : "failedClass";
- }
- }
- }
The following are the SQL Queries that contains the tables used, procedures used etcetera:
- create database Practice
- use Practice
- drop table Students
- create table Students
- (
- StudId varchar(10),
- RollNo int identity(1,1)not null,
- StudName varchar(30),
- StudAdd varchar(50),
- StudCity varchar(50),
- StudTel varchar(20),
- constraint pkStudId primary key(StudId)
- )
- alter procedure prc_AddStudentDetails
- (
- @name varchar(30),
- @add varchar(50),
- @city varchar(50),
- @telno varchar(20)
- )
- as
- begin
- declare @rno int
- declare @studid varchar(10)
- if(select COUNT(*) from students)>0
- begin
- select @rno=MAX(rollno)
- from students
- end
- else
- set @rno=0
- set @studid='S00'+Convert(varchar,(@rno+1))
- insert into Students(StudId,StudName,StudAdd,StudCity,StudTel)
- values(@studid,@name,@add,@city,@telno)
- end
- prc_AddStudentDetails 'Vishal','Andheri','Mumbai','123456789'
- prc_AddStudentDetails 'Dheeraj','Bhayandar','Mumbai','987654321'
- prc_AddStudentDetails 'Shankar','Kalyan','Thane','456789123'
- prc_AddStudentDetails 'Kailash','Santacruz','Mumbai','789123456'
- prc_AddStudentDetails 'Jack','Dadar','Mumbai','852741963'
- select * from Students
- create table Marks
- (
- Rollno int,
- English float,
- Hindi float,
- Marathi float,
- Maths float,
- Science float,
- SocialScience float
- )
- select * from Marks
- insert into Marks values(1,85,75,95,99,95,89)
- insert into Marks values(2,65,85,88,83,98,80)
- insert into Marks values(3,55,85,85,85,65,79)
- insert into Marks values(4,95,90,90,88,75,99)
- insert into Marks values(5,80,88,89,90,92,88)
- create procedure prc_GetStudentMarksData
- as
- begin
- select StudId,a.RollNo,StudName,English,Marathi,Maths,Science,SocialScience,Hindi
- from Students a inner join Marks b
- on a.RollNo=b.Rollno
- end
- create procedure prc_GetStudentData
- as
- begin
- select * from Students
- end
- create procedure prc_GetStudentMarks
- (
- @rollno int
- )
- as
- begin
- select * from Marks
- where Rollno=@rollno
- end
- update Marks
- set SocialScience=23
- where Rollno=5
Let's check the design/mark up code for it.
Source Code for it
Finally it is time to check the output of it.
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.
Hope you liked the article.