如何从 Repeater 获取数据
How to fetch data from Repeater
使用以下 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>
我得到的结果是这样的:
现在我想从转发器获取 Abhishek Mishra。在 gridView
中,我能够使用 gdFee.Rows[0].Cells[0]
做到这一点,但在使用中继器的情况下我无法做到这一点。
我如何在 Repeater
的索引 0 处检索该元素的名称?
使用中继器的 属性 OnItemDataBound。
在您的页面中:
<asp:Repeater ID="rptFee" runat="server" Visible="false" OnItemDataBound="rptFee_ItemDataBound">
在你后面的代码中:
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;
}
}
这是一个很好的link:https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx
Andy 的答案对于在 Repeater 绑定每个 RepeaterItem 时查找控件是正确的。如果您想在任何数据绑定事件之外获取 RepeaterItems,转发器有一个名为 Items
.
的 RepeaterItem 集合
使用它类似于 GridView,但您仍然需要像 Andy 的示例中那样找到控件。
RepeaterItem item = rptFee.Items[0];
Label lblname = (Label)item.FindControl("lblname");
string name = lblname.text;
您可以尝试以下操作,
foreach (RepeaterItem itm in rptFee.Items) {
//You can loop through all repeater items here
Label lblname = (Label)itm.findControl("lblname");
}
使用以下 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>
我得到的结果是这样的:
现在我想从转发器获取 Abhishek Mishra。在 gridView
中,我能够使用 gdFee.Rows[0].Cells[0]
做到这一点,但在使用中继器的情况下我无法做到这一点。
我如何在 Repeater
的索引 0 处检索该元素的名称?
使用中继器的 属性 OnItemDataBound。
在您的页面中:
<asp:Repeater ID="rptFee" runat="server" Visible="false" OnItemDataBound="rptFee_ItemDataBound">
在你后面的代码中:
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;
}
}
这是一个很好的link:https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx
Andy 的答案对于在 Repeater 绑定每个 RepeaterItem 时查找控件是正确的。如果您想在任何数据绑定事件之外获取 RepeaterItems,转发器有一个名为 Items
.
使用它类似于 GridView,但您仍然需要像 Andy 的示例中那样找到控件。
RepeaterItem item = rptFee.Items[0];
Label lblname = (Label)item.FindControl("lblname");
string name = lblname.text;
您可以尝试以下操作,
foreach (RepeaterItem itm in rptFee.Items) {
//You can loop through all repeater items here
Label lblname = (Label)itm.findControl("lblname");
}