【发布时间】:2013-03-28 17:47:11
【问题描述】:
如何在转发器 _DataBound 子中获取我想要的每个单元格的值? - 这样我就可以稍微更改值并将更改应用于文字
【问题讨论】:
标签: asp.net repeater itemdatabound
如何在转发器 _DataBound 子中获取我想要的每个单元格的值? - 这样我就可以稍微更改值并将更改应用于文字
【问题讨论】:
标签: asp.net repeater itemdatabound
假设这是您的中继器(因为您没有包含任何代码):
<asp:Repeater ID="_r" runat="server">
<ItemTemplate>
<asp:Literal ID="_lit" Text='<%# Eval("yourItemName")%>' runat="server"></asp:Literal>
<br /><br />
</ItemTemplate>
</asp:Repeater>
并且您创建了一个 ItemDataBound 事件,因为您想在每个文字的末尾添加“meow”(否则只会显示来自数据源的 yourItemName 的值):
protected void Page_Load(object sender, EventArgs e)
{
_r.DataSource = table;
_r.ItemDataBound += new RepeaterItemEventHandler(RItemDataBound);
_r.DataBind();
}
protected void RItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Literal lit = (Literal)e.Item.FindControl("_lit");
lit.Text += "meow";
}
}
【讨论】:
您可以在该行中获取文字,但将事件转换为 arg:
Protected Sub repeater_dataBind(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
If (e.Item.ItemType <> ListItemType.Item And e.Item.ItemType <> ListItemType.AlternatingItem) Then
Return
Else
Dim myLiteral As New Literal
myLiteral = CType(e.Item.FindControl("IDofLiteral"), Literal)
End If
end Sub
【讨论】:
if 语句中执行return,则不需要else 语句。
e.Item.Itemtype 是Item 或AlternatingItem 会更简洁,然后你就不需要“else”语句了。
您可以使用FindControl 获取repeater 中单元格的值。
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Literal lbl = (Literal)e.Item.FindControl("ltrl");
lbl.Text = // will give you the value for each `ItemIndex`
}
}
【讨论】: