在Repeater的HeaderTemplate中设置MyLabel.Text
-
23-08-2019 - |
题
我发现这样做的每一个样本包括为了做到这一点写一个函数我的网页的OnLoad之外的,但我很好奇,如果有一个更简洁的方式去了解它。我有一个标签一个HeaderTemplate中的里面,我只是想在标签的文本设置为一个字符串。我可以执行以下操作:如果标签是所述中继器之外的:
Month.Text = Enum.GetName(typeof(Month), Convert.ToInt16(MonthList.SelectedValue));
有没有一种简洁的方式来做到这一点?
解决方案
请尝试以下的在的header模版:
<asp:Label ID="Month" runat="server" Text='<%# (Month)Convert.ToInt16(MonthList.SelectedValue) %>' />
其他提示
这将是更好的,如果你没有使用数据绑定事件。
ASPX标记:
<asp:Repeater ID="repTest" runat="server">
<HeaderTemplate>
<asp:Label ID="lblHeader" runat="server" />
</HeaderTemplate>
</asp:Repeater>
代码后面:
protected void Page_Load(object sender, EventArgs e)
{
repTest.ItemDataBound += new RepeaterItemEventHandler(repTest_ItemDataBound);
int[] testData = { 1, 2, 3, 4, 5, 6, 7, 8 };
repTest.DataSource = testData;
repTest.DataBind();
}
void repTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label lblHeader = e.Item.FindControl("lblHeader") as Label;
if (lblHeader != null)
{
lblHeader.Text = "Something";
}
}
}
有你去:)
我不是100%确定你是否需要等待中继器具有绑定到它有数据,但是这是在它的头,你将如何访问控制:
var myLabel = MyRepeater.Controls[0].Controls[0].FindControl("MyLabel") as Label;
myLabel.Text = "Hello World";
您应该可能打破这种成多行,并检查,以确保有在控制对象[0]。
不隶属于 StackOverflow