Reputation: 231
When using the following Repeater
:
<asp:Repeater ID="rptFee" runat="server" Visible="false">
<HeaderTemplate>
<div class="CSSTableGenerator">
<table border="1">
<thead>
<th>Name</th>
<th>Course</th>
<th>Contact_No</th>
<th>Total_Fee</th>
<th>Paid_Amount</th>
<th>Due_Amount</th>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td><asp:Label id="lblname" runat="server" Text='<%# Eval("Name") %>'></asp:Label></td>
<td><asp:Label id="lblcourse" runat="server" Text='<%# Eval("Course") %>'></asp:Label></td>
<td><asp:Label id="lblcontact" runat="server" Text='<%# Eval("Contact_No") %>'></asp:Label></td>
<td><asp:Label id="lbltotalfee" runat="server" Text='<%# Eval("Total_Fee") %>'></asp:Label></td>
<td><asp:Label id="lblpaid" runat="server" Text='<%# Eval("Paid_Fee") %>'></asp:Label></td>
<td><asp:Label id="lbldue" runat="server" Text='<%# Eval("Due_Amount") %>'></asp:Label></td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>
The result I'm getting is this:
Now I want to fetch Abhishek Mishra from repeater. In the gridView
i was able to do that using gdFee.Rows[0].Cells[0]
, I am not able to do that in case of a repeater.
How would I retrieve the name of that element at index 0 of a Repeater
?
Upvotes: 0
Views: 3738
Reputation: 1898
You can try something below,
foreach (RepeaterItem itm in rptFee.Items) {
//You can loop through all repeater items here
Label lblname = (Label)itm.findControl("lblname");
}
Upvotes: 0
Reputation: 3949
Andy's answer is correct for finding controls while the Repeater is binding each RepeaterItem. If you want to get RepeaterItems outside of any data binding event, the repeater has a RepeaterItem collection simply called Items
.
Using this would be similar to a GridView, but you would still need to find the control like in Andy's example.
RepeaterItem item = rptFee.Items[0];
Label lblname = (Label)item.FindControl("lblname");
string name = lblname.text;
Upvotes: 1
Reputation: 83
Use the property OnItemDataBound of you repeater.
In your page:
<asp:Repeater ID="rptFee" runat="server" Visible="false" OnItemDataBound="rptFee_ItemDataBound">
In your code behind:
protected void rptFee_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label label = (Label)e.Item.FindControl("lblname");
//You have now access to each lblname in your repeater...
string temp = label.Text;
}
}
Here's a good link : https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx
Upvotes: 1