I will suggest to read my previous article on Repeater control,
The following two events are more important.
- ItemDataBound: Execute just before it is rendered on the page.
- 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:
- Display Balance Fees button if there is a balance fee.
- Display Exam Result button if there is test series taken by student.
Student Table Structure
Table Name : tblStudents
- USE[VidyaDB]
- GO
- /****** Object: Table [dbo].[tblStudents] Script Date: 01/01/2016 15:15:21 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE[dbo].[tblStudents]
- (
- [StudentID][int] IDENTITY(1, 1) NOT NULL, [StudentName][nvarchar](25) NULL, [ResidenceCity][nvarchar](50) NULL, [ResidencePin][int] NULL, [FatherMobileNos][nvarchar](15) NULL, [MotherMobileNos][nvarchar](15) NULL, [TotalFees][int] NULL, [ReceivedFees][int] NULL, [BalanceFees][int] NULL, [IsTestSeriesTaken][bit] NOT NULL
- ) ON[PRIMARY]
Dummy Data
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.
- As usual we have created a project ASP.NET Empty Web Site project.
File, Web Site, then ASP.NET Empty Web Site,
- Create/Add a new web form.
Right click on project, select ADD - Add New Item, then Web Form
Give Name : Default.aspx
- Double click on Default.aspx page.
- Drag and drop Repeater control from Data toolbox.
- 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. - if (BalanceFees > 0)
- {
- lblRemarks.Text = "Please, contact for Balance Fees";
- }
-
- if (BalanceFees == 0)
- {
- lblRemarks.Text = "Full Fees Received";
- }
|
Button | btnTestSeries | This button will display or hide. if TestSeriesTaken then button will display otherwise button get hidden. |
- DEFAULT.ASPX page code
- <asp:Repeater ID="rptStudentDetail" runat="server" OnItemDataBound="rptStudentDetail_ItemDataBound">
- <ItemTemplate>
- <table>
- <tr>
-
- <td>
- <asp:Button ID="btnForm" runat="server" Text="View Student Form" CommandName="Form" />
- <br />
- <asp:HiddenField ID="hdnTestSeriesTaken" runat="server" Value='<%# Eval("IsTestSeriesTaken") %>' />
- <b>Student ID:</b>
- <asp:Label ID="lblStudentID" runat="server" Text='<%# Eval("StudentID") %>'></asp:Label>
- <br />
- <b>Student Name:</b>
- <asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
- <br />
- <b>City:</b>
- <asp:Label ID="lblResidenceCity" runat="server" Text='<%# Eval("ResidenceCity") %>'></asp:Label>
- <br />
- <b>Father Mobile Number:</b>
- <asp:Label ID="lblFatherMobileNos" runat="server" Text='<%# Eval("FatherMobileNos") %>'></asp:Label>
- <br />
- <b>Mother Mobile Number:</b>
- <asp:Label ID="lblMotherMobileNos" runat="server" Text='<%# Eval("MotherMobileNos") %>'></asp:Label>
- <br />
- <b>Total Fees:</b>
- <asp:Label ID="lblTotalFees" runat="server" Text='<%# Eval("TotalFees") %>'></asp:Label>
- <br />
- <b>Received Fees:</b>
- <asp:Label ID="lblReceivedFees" runat="server" Text='<%# Eval("ReceivedFees") %>'></asp:Label>
- <br />
- <b>Balance Fees:</b>
- <asp:Label ID="lblBalanceFees" runat="server" Text='<%# Eval("BalanceFees") %>'></asp:Label>
- <br />
- <asp:Label ID="lblRemarks" runat="server" Visible="true" Font-Bold="true" Font-Size="X-Large" BackColor="Yellow"></asp:Label>
- <br />
- <asp:Button ID="btnTestSeries" runat="server" Visible="false" CommandName="Test"></asp:Button>
- </td>
- </tr>
-
- </table>
-
- </ItemTemplate>
- <SeparatorTemplate>
- <br />
- <hr/>
- <br />
- </SeparatorTemplate>
- </asp:Repeater>
- Right click on project, Add Reference, AssembliesFramework
Select System.Configuration.dll
- Add the following namespace in Default.aspx.cs,
- using System.Data;
- using System.Data.SqlClient;
- 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.
- Default.aspx.cs code
- string ConStr = ConfigurationManager.ConnectionStrings["VidyaCDACConnectionString"].ConnectionString;
-
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindRepeater();
- }
- }
-
-
-
-
- public void BindRepeater()
- {
-
- SqlConnection con = new SqlConnection(ConStr);
- SqlDataAdapter da = new SqlDataAdapter("Select * From tblStudents", ConStr);
- DataSet ds = new DataSet();
- da.Fill(ds, "StudentsTable");
- rptStudentDetail.DataSource = ds;
- rptStudentDetail.DataBind();
- }
-
-
- protected void rptStudentDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)
- {
-
- if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
- {
- int BalanceFees = Convert.ToInt16(((Label) e.Item.FindControl("lblBalanceFees")).Text);
- Label lblRemarks = ((Label) e.Item.FindControl("lblRemarks"));
- Button btnTestSeries = ((Button) e.Item.FindControl("btnTestSeries"));
- HiddenField hdnTestSeriesTaken = ((HiddenField) e.Item.FindControl("hdnTestSeriesTaken"));
- btnTestSeries.Text = "Test Series Result Entry";
- if (BalanceFees > 0)
- {
- lblRemarks.Text = "Please, contact for Balance Fees";
- }
-
- if (BalanceFees == 0)
- {
- lblRemarks.Text = "Full Fees Received";
- }
-
- if (hdnTestSeriesTaken.Value == "True")
- {
- btnTestSeries.Visible = true;
- } else
- {
- btnTestSeries.Visible = false;
- }
- }
- }
- protected void rptStudentDetail_ItemCommand(object source, RepeaterCommandEventArgs e)
- {
- if (e.CommandName == "Test")
- {
-
- Response.Write("<script>alert('Test Series Button Clicked')</script>");
- }
- if (e.CommandName == "Form")
- {
-
- Response.Write("<script>alert('View Student Form Button Clicked')</script>");
- }
- }
- 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.
Please, feel free to ask any question related to this article.