Today we are going to see how to work with Repeater Control.
Here is an example how are we going to get a repeater after we finished coding.
Here I am using AdventureWorksDW2008R2 in SQL SERVER which can be downloaded from http://www.codeplex.com.
The table I am using is DimCustomer and fields are :
1)FirstName
2)MiddleName
3)LastName
4)BirthDate
5)CustomerKey
Step 1 : Upload the control in the page in .aspx side let it be default.aspx
<form id=”frmExample” runat=”server”>
<asp:Repeater ID=”rptList” runat=”server” OnItemDataBound=”rptList_ItemDataBound”>
<HeaderTemplate>
<table border=”1″ cellpadding=”0″ cellspacing=”0″>
12px; color:White;”>
<th>
<asp:Label ID=”Label14″ Text=”Login” runat=”server”></asp:Label>
</th>
<th>
<asp:Label ID=”Label1″ Text=”First Name” runat=”server”></asp:Label>
</th>
<th>
<asp:Label ID=”Label2″ Text=”Last Name” runat=”server”></asp:Label>
</th>
<th>
<asp:Label ID=”Label3″ Text=”Date Added” runat=”server”></asp:Label>
</th>
<th>
<asp:Label ID=”Label6″ Text=”Delete” runat=”server”></asp:Label>
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID=”lbnLogin” Text='<%# Eval(“FirstName”) %>’ runat=”server”</asp:
OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>
LinkButton>
</td>
<td>
<asp:LinkButton ID=”lbnFirstName” Text='<%# Eval(“MiddleName”).ToString() %>'</asp:
runat=”server”
OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>
LinkButton>
</td>
<td>
<asp:LinkButton ID=”LinkButton2″ Text='<%# Eval(“LastName”).ToString() %>'</asp:
runat=”server”
OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>
</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID=”LinkButton1″ Text='<%# string.Format(“{0:d}”, Eval(“BirthDate”)) %>’ runat=”server”
OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>
</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID=”lbnDelete” Text=”delete” runat=”server”
OnClick=”lbnDelete_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>
</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
NOTE: Dont forget to make the form tag as runat = “server “
otherwise an error will occur because you are trying to insert the data from .cs
side but u can only access it when u make runat = “server “
STEP 2: Now insert the data from the code behing
int Page_Load event
SqlConnection con = new SqlConnection(“Server=localhost;UID=xx;PWD=xx;
Database=AdventureWorksDW2008R2″);
string sSQL = “Select top 5 * from dbo.DimCustomer”;
SqlCommand cmd = new SqlCommand(sSQL, con);
con.Open();
SqlDataReader dtrClient = cmd.ExecuteReader();
rptList.DataSource = dtrClient;
rptList.DataBind();
con.Close();
/*along with it*/ write these dummy function later you can implement them as you like .
protected void lnkClient_Click(object sender, EventArgs e)
{ }
protected void lbnDelete_Click(object sender, EventArgs e)
{ }
protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{ }
The last 2 lines binding the data to the repeater and starting remaining lines is to establish connection to database.
the output will come as shown above .
Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )