Repeater Control In ASP.NET - Part 2

I will suggest to read my previous article on Repeater control,

The following two events are more important.

  1. ItemDataBound: Execute just before it is rendered on the page.
  2. ItemCommand: Execute when button kind control of repeater clicked.

Explained through a practical kind of scenario

Requirement:

One educational institute named “SAI Education Institute”. We are going create a SEARCH PAGE. In page we have displayed button dynamically as per student table data setting.

Conditions:

  1. Display Balance Fees button if there is a balance fee.
  2. Display Exam Result button if there is test series taken by student.

Student Table Structure

Table Name : tblStudents

  1. USE[VidyaDB]  
  2. GO  
  3. /****** Object: Table [dbo].[tblStudents] Script Date: 01/01/2016 15:15:21 ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. CREATE TABLE[dbo].[tblStudents]  
  9. (  
  10.     [StudentID][int] IDENTITY(1, 1) NOT NULL, [StudentName][nvarchar](25) NULL, [ResidenceCity][nvarchar](50) NULL, [ResidencePin][intNULL, [FatherMobileNos][nvarchar](15) NULL, [MotherMobileNos][nvarchar](15) NULL, [TotalFees][intNULL, [ReceivedFees][intNULL, [BalanceFees][intNULL, [IsTestSeriesTaken][bitNOT NULL  
  11. ON[PRIMARY]  
Dummy Data

dummy

In above dummy data image you see, total Six (6) students, only two students have not taken TEST SERIES and two student PAID FULL FEES.

Our Task is todisplay Balance Fees button if there is a balance fee and display Exam Result button if there is test series taken by student.
  1. As usual we have created a project ASP.NET Empty Web Site project.
    File, Web Site, then ASP.NET Empty Web Site,

    new

  2. Create/Add a new web form.
    Right click on project, select ADD - Add New Item, then Web Form
    Give Name : Default.aspx

    form

  3. Double click on Default.aspx page.
  4. Drag and drop Repeater control from Data toolbox.

    toolbox

  5. The following control used Inside Repeater Control,

    CONTROL TYPE CONTROL ID DESCRIPTION,

    CONTROL TYPE CONTROL ID DESCRIPTION
    HiddenField hdnTestSeriesTaken To store the test series taken or not – True / False.
    Label lblStudentID To display Student ID
    Label lblStudentName To display Studnet Name.
    Label lblResidenceCity To display City.
    Label lblFatherMobileNos To display Father mobile number.
    Label lblMotherMobileNos To display Mother mobile number.
    Label lblTotalFees To display Total Fees.
    Label lblReceivedFees To display Received Fees.
    Label lblBalanceFees To display Balance Fees.
      Following Fields will Display Dynamically  
    Label lblRemarks This field text display dynamically as per balance fees value.
    For this you can check CODE BEHIND code.
    1. if (BalanceFees > 0)   
    2. {  
    3.     lblRemarks.Text = "Please, contact for Balance Fees";  
    4. }  
    5.   
    6. if (BalanceFees == 0)   
    7. {  
    8.     lblRemarks.Text = "Full Fees Received";  
    9. }  
    Button btnTestSeries This button will display or hide. if TestSeriesTaken then button will display otherwise button get hidden.


  6. DEFAULT.ASPX page code
    1. <asp:Repeater ID="rptStudentDetail" runat="server" OnItemDataBound="rptStudentDetail_ItemDataBound">  
    2.     <ItemTemplate>  
    3.         <table>  
    4.             <tr>  
    5.   
    6.                 <td>  
    7.                     <asp:Button ID="btnForm" runat="server" Text="View Student Form" CommandName="Form" />  
    8.                     <br />  
    9.                     <asp:HiddenField ID="hdnTestSeriesTaken" runat="server" Value='<%# Eval("IsTestSeriesTaken") %>' />  
    10.                     <b>Student ID:</b>  
    11.                     <asp:Label ID="lblStudentID" runat="server" Text='<%# Eval("StudentID") %>'></asp:Label>  
    12.                     <br />  
    13.                     <b>Student Name:</b>  
    14.                     <asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>  
    15.                     <br />  
    16.                     <b>City:</b>  
    17.                     <asp:Label ID="lblResidenceCity" runat="server" Text='<%# Eval("ResidenceCity") %>'></asp:Label>  
    18.                     <br />  
    19.                     <b>Father Mobile Number:</b>  
    20.                     <asp:Label ID="lblFatherMobileNos" runat="server" Text='<%# Eval("FatherMobileNos") %>'></asp:Label>  
    21.                     <br />  
    22.                     <b>Mother Mobile Number:</b>  
    23.                     <asp:Label ID="lblMotherMobileNos" runat="server" Text='<%# Eval("MotherMobileNos") %>'></asp:Label>  
    24.                     <br />  
    25.                     <b>Total Fees:</b>  
    26.                     <asp:Label ID="lblTotalFees" runat="server" Text='<%# Eval("TotalFees") %>'></asp:Label>  
    27.                     <br />  
    28.                     <b>Received Fees:</b>  
    29.                     <asp:Label ID="lblReceivedFees" runat="server" Text='<%# Eval("ReceivedFees") %>'></asp:Label>  
    30.                     <br />  
    31.                     <b>Balance Fees:</b>  
    32.                     <asp:Label ID="lblBalanceFees" runat="server" Text='<%# Eval("BalanceFees") %>'></asp:Label>  
    33.                     <br />  
    34.                     <asp:Label ID="lblRemarks" runat="server" Visible="true" Font-Bold="true" Font-Size="X-Large" BackColor="Yellow"></asp:Label>  
    35.                     <br />  
    36.                     <asp:Button ID="btnTestSeries" runat="server" Visible="false" CommandName="Test"></asp:Button>  
    37.                 </td>  
    38.             </tr>  
    39.   
    40.         </table>  
    41.   
    42.     </ItemTemplate>  
    43.     <SeparatorTemplate>  
    44.         <br />  
    45.         <hr/>  
    46.         <br />  
    47.     </SeparatorTemplate>  
    48. </asp:Repeater>  
  7. Right click on project, Add Reference, AssembliesFramework
    Select System.Configuration.dll

    refrence

  8. Add the following namespace in Default.aspx.cs,
    1. using System.Data;   
    2. using System.Data.SqlClient;  
    3. using System.Configuration;  
    Using System.Data: This namespace to access ADO.NET full functionalities.
    Using System.Data.SqlClient: This namespace to access SQL Server database.
    Using System.Configuration: To fetch connection string from web.config.

  9. Default.aspx.cs code
    1. string ConStr = ConfigurationManager.ConnectionStrings["VidyaCDACConnectionString"].ConnectionString;  
    2.   
    3. protected void Page_Load(object sender, EventArgs e)   
    4. {  
    5.     if (!IsPostBack)   
    6.     {  
    7.         BindRepeater();  
    8.     }  
    9. }  
    10.   
    11. /// <summary>  
    12. /// To load data into Repeater control  
    13. /// </summary>  
    14. public void BindRepeater()  
    15. {  
    16.   
    17.     SqlConnection con = new SqlConnection(ConStr);  
    18.     SqlDataAdapter da = new SqlDataAdapter("Select * From tblStudents", ConStr);  
    19.     DataSet ds = new DataSet();  
    20.     da.Fill(ds, "StudentsTable");  
    21.     rptStudentDetail.DataSource = ds;  
    22.     rptStudentDetail.DataBind();  
    23. }  
    24.   
    25. // This event is raised for the header, the footer, separators, and items.  
    26. protected void rptStudentDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)   
    27. {  
    28.   
    29.     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
    30.     {  
    31.         int BalanceFees = Convert.ToInt16(((Label) e.Item.FindControl("lblBalanceFees")).Text);  
    32.         Label lblRemarks = ((Label) e.Item.FindControl("lblRemarks"));  
    33.         Button btnTestSeries = ((Button) e.Item.FindControl("btnTestSeries"));  
    34.         HiddenField hdnTestSeriesTaken = ((HiddenField) e.Item.FindControl("hdnTestSeriesTaken"));  
    35.         btnTestSeries.Text = "Test Series Result Entry";  
    36.         if (BalanceFees > 0)   
    37.         {  
    38.             lblRemarks.Text = "Please, contact for Balance Fees";  
    39.         }  
    40.   
    41.         if (BalanceFees == 0)   
    42.         {  
    43.             lblRemarks.Text = "Full Fees Received";  
    44.         }  
    45.   
    46.         if (hdnTestSeriesTaken.Value == "True")   
    47.         {  
    48.             btnTestSeries.Visible = true;  
    49.         } else   
    50.         {  
    51.             btnTestSeries.Visible = false;  
    52.         }  
    53.     }  
    54. }  
    55. protected void rptStudentDetail_ItemCommand(object source, RepeaterCommandEventArgs e)   
    56. {  
    57.     if (e.CommandName == "Test")  
    58.     {  
    59.         //Code for btnTestSeries button clicked  
    60.         Response.Write("<script>alert('Test Series Button Clicked')</script>");  
    61.     }  
    62.     if (e.CommandName == "Form")  
    63.     {  
    64.         //Code for btnForm button clicked  
    65.         Response.Write("<script>alert('View Student Form Button Clicked')</script>");  
    66.     }  
    67. }  
  10. ItemDataBound method:

    As you know now we had checked Item.ItemType. There are the followings types of ItemType:

    a. ListItemType.Item: check item template.
    b. ListItemType.AlternatingItem: check alternating item template.
    c. ListItemType.Footer: check footer item template.

    template

    Please, feel free to ask any question related to this article.

Up Next
    Ebook Download
    View all
    Learn
    View all