我有一个转发器,只有当它存在时才应该显示绑定的字段值。阅读了这篇文章后,我决定在我的转发器中使用文字并使用 OnItemDatabound 触发器填充我的文字,但我的文字似乎无法从后面的 c# 代码访问,我不明白为什么!
这是aspx页面
<asp:Repeater runat="server" ID="rpt_villaresults" OnItemDataBound="checkForChildren">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//.................MORE CODE HERE......................
<div class="sleeps"><h4>To Sleep</h4><h5><%#Eval("sleeps")%> <asp:Literal ID="sleepsChildrenLit" runat="server" /> </h5></div>
//.............MORE CODE HERE........................
以及背后的代码
public void checkForChildren(object sender, RepeaterItemEventArgs e)
{
Literal childLit = e.Item.FindControl("sleepsChildrenLit") as Literal;
//this is null at runtime
String str = e.Item.DataItem.ToString();
if (e.Item.DataItem != null)
{
if (Regex.IsMatch(str, "[^0-9]"))
{
if (Convert.ToInt32(str) > 0)
{
childLit.Text = " + " + str;
}
}
}
}