This tutorial will show you how to use the OnItemDataBound event of a Repeater control to query data when displaying from a data source. C#.
In this tutorial you will learn how to use the On Item Data Bound of a Repeater control. This event is useful when we want to display information from a data source, and query the data at the same time. For example, we have a table of customers, and for each customer with a negative balance, we want to display their name in red. We can do this using the OnItemDataBound event of the Repeater.
In this example, we will create a datatable (with columns Name, Age, and City) on page load and then populate a Repeater. When displaying the data, we will query the Age column, and if it is empty, then we will display a note to the user. So to begin, let us first add our repeater to the page:
| <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> </form> <HeaderTemplate> </asp:Repeater></HeaderTemplate> <ItemTemplate> </ItemTemplate> <FooterTemplate> </FooterTemplate> |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
This is the shell of our repeater. We will create a header template and a footer template to display our data in a table. We can also add Literal controls to display our data:
| <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> </form> <HeaderTemplate> </asp:Repeater><table><tr><th>Name</th><th>Age</th><th>City</th><th>Notes</th></tr> </HeaderTemplate><ItemTemplate> <tr> </ItemTemplate><td><asp:Literal ID="lit_Name" runat="server" /></td> </tr><td><asp:Literal ID="lit_Age" runat="server" /></td> <td><asp:Literal ID="lit_City" runat="server" /></td> <td><asp:Literal ID="lit_Notes" runat="server" /></td> <FooterTemplate> </table> </FooterTemplate> |
We can now begin on our code-behind. We will create a DataTable on page load, for demonstration purposes, to bind data to the repeater. In a real-world situation, this would likely be populated from a database.
On Page_Load we will programmatically create a datatable, populate it, and then bind it to the repeater:
| DataTable dt = new DataTable(); DataColumn dc; dc = new DataColumn(); dc.ColumnName = "Name"; dt.Columns.Add(dc); dc = new DataColumn(); dc.ColumnName = "Age"; dt.Columns.Add(dc); dc = new DataColumn(); dc.ColumnName = "City"; dt.Columns.Add(dc); DataRow dr; dr = dt.NewRow(); dr["Name"] = "Fred"; dr["Age"] = "54"; dr["City"] = "Rockingham"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Name"] = "Bill"; dr["Age"] = ""; dr["City"] = "Rio"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Name"] = "Rhona"; dr["Age"] = "32"; dr["City"] = "LA"; dt.Rows.Add(dr); Repeater1.DataSource = dt; Repeater1.DataBind(); |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Here, we have added three rows to the table, under the columns Name, Age, and City. Notice that we have left the second Age column blank. In this example, we will check if the Age column is blank and then display a message to the user.
Next, we will create a method that will execute on the Repeater's ItemDataBound event. To do this, add the following method in the codebehind:
| protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e) { RepeaterItem item = e.Item; if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item) } { } |
This method will be executed when the Repeater is bound (when we call the DataBing method), and will occur for each data item that is bound to the repeater. But before that happens, we need to add the following to the Repeater:
| <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound"> |
We tell the Repeater which method to call on its ItemDataBound event.
Going back to the method, we use a simple if statement to check if the current item is either an item or alternating item. This is because the header and footer is also looped through on each data bind.
We now need to add the logic to the method that will check our Age value:
| protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e) { RepeaterItem item = e.Item; } if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item) { Literal lit_Age = (Literal)item.FindControl("lit_Age"); }if (String.IsNullOrEmpty(lit_Age.Text)) { Literal lit_Name = (Literal)item.FindControl("lit_Name"); }Literal lit_Notes = (Literal)item.FindControl("lit_Notes"); lit_Age.Text = "-"; lit_Notes.Text = lit_Name.Text + "'s age is unknown."; |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Here we are checking to see if the Age value is blank, and if it is, we will display the age as '-' and notify the user that the person's age is unknown. This check will occur for each data item in the repeater.
Finally, we can add the text value of our Literal controls to display the data from the datatable:
| <td><asp:Literal ID="lit_Name" runat="server" Text='<%# Eval("Name") %>' /></td> <td><asp:Literal ID="lit_Age" runat="server" Text='<%# Eval("Age") %>' /></td> <td><asp:Literal ID="lit_City" runat="server" Text='<%# Eval("City") %>' /></td> <td><asp:Literal ID="lit_Notes" runat="server" /></td> |
No comments:
Post a Comment