[英]How to change value in listview of lable in asp.net
您好,我需要列表视图控件的帮助,这是下面的列表视图代码:
<asp:ListView runat="server" ID="ListView1"
GroupItemCount="3" OnItemCommand="ListView1_ItemCommand">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server"
ID="groupPlaceHolder" />
</div>
</LayoutTemplate>
<GroupTemplate>
<div style="clear: both;">
<asp:PlaceHolder runat="server"
ID="itemPlaceHolder" />
</div>
</GroupTemplate>
<ItemTemplate>
<div class="store-l">
<p class="exe-title">
<asp:Label ID="Label1" runat="server">
</asp:Label> </p>
<center>
<a href="View.aspx?Iv=<%# Eval("Id") %>"><asp:Image ID="Image2"
ImageUrl='<%# Eval("ImageUrl")%>' runat="server"
Height="175" Width="280"></asp:Image></a>
</center>
<p class="exe-title">
<%# Eval("Description")%>
<a href="ViewResult.aspx?Iv=<%# Eval("Id") %>">View Result</a>
</p>
</div>
</ItemTemplate>
<GroupSeparatorTemplate>
<div style="clear: both"/>
</GroupSeparatorTemplate>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:ListView>
我想通过代码更改Label1的值。 有12个固定值,我需要添加以下12个值:
1. day 1
2. day two
3. day three
4. day four
5. day fiv etc.
我想使用代码在lable1中添加此值。 编程语言是c#。 希望有人对此有解决方案,请让我知道我该怎么做,并与我分享您的代码
谢谢
您的基本方法应如下所示:
在ListView的ItemDataBound事件处理程序中,检查它是哪种模板。 如果它是ItemTemplate,则使用e.Item.FindControl(“ Label1”)找到控件Label1,并将其转换为Label类型。 这样您就可以随心所欲地使用它了。 因此,您可以将其分配给Label变量。 这样的事情应该起作用:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label Label1 = (Label)e.Item.FindControl("Label1");
Label1.Text = "day 1"; //do whatever you want with the Label here
}
另外,您必须在声明性标记或Page_Load或其他事件中使用此事件处理程序连接ListView。 如果在声明性标记中执行此操作,只需将以下属性添加到ListView标记中:
OnItemDataBound="ListView1_ItemDataBound"
希望能有所帮助。
查看此链接的示例:
http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.listview.itemdatabound.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.