C#如何在列表视图中设置标签文本?

问题描述:

在我的代码隐藏中,我希望设置标签的文本。下面是ASPX代码:C#如何在列表视图中设置标签文本?

<asp:ListView ID="lstRegistrations" runat="server"> 
    <LayoutTemplate> 
     <table cellspacing="0" cellpadding="0" border="0"> 
      <tr> 
       <th width="80" align="left"> 
        <asp:Label ID="lblDate" runat="server" Text="<%= GetTranslatedText(7726) %>" /> 
       </th> 
       <th width="150" align="left"> 
        <asp:Label ID="lblAuthor" runat="server" Text="<%= GetTranslatedText(7728) %>" /> 
       </th> 
       <th width="290" align="left"> 
        <asp:Label ID="lblRegistration" runat="server" Text="<%= GetTranslatedText(6671) %>" /> 
       </th> 
       <th width="60" align="left"> 
        <asp:Label ID="lblVersion" runat="server" Text="<%= GetTranslatedText(13) %>" /> 
       </th> 
      </tr> 
      <tr> 
       <td colspan="4" style="height: 3px;"></td> 
      </tr> 
      <tr runat="server" id="itemPlaceholder"></tr> 
     </table> 
    </LayoutTemplate> 

    <ItemTemplate> 
     <tr style="background-color:#FFFFD0;"> 
      <td style="padding-left: 3px"> 
       <%# ((DateTime)Eval("Date")).ToString("d-M-yyyy") %> 
      </td> 
      <td> 
       <%# GetStaffNameById((int)Eval("StaffID")) %> 
      </td> 
      <td> 
       <%# Server.HtmlEncode(Eval("Text").ToString())%> 
      </td> 
      <td> 
       <%# Eval("Version") %> 
      </td> 
     </tr> 
    </ItemTemplate> 
    <AlternatingItemTemplate> 
     <tr style="background-color: #C89292"> 
      <td style="padding-left: 3px"> 
       <%# ((DateTime)Eval("Date")).ToString("d-M-yyyy") %> 
      </td> 
      <td> 
       <%# GetStaffNameById((int)Eval("StaffID")) %> 
      </td> 
      <td> 
       <%# Server.HtmlEncode(Eval("Text").ToString())%> 
      </td> 
      <td> 
       <%# Eval("Version") %> 
      </td> 
     </tr> 
    </AlternatingItemTemplate> 
</asp:ListView> 

在顶部,在layoutTemplate中我有4个标签,我要改变的文本属性。我试图通过使用lstRegistrations.FindControl()方法来访问标签,但此方法找不到标签。我也尝试了Page.FindControl()方法,但是这种方法要么找不到标签。然后我想,我创建一个方法并在我的aspx页面中调用它(请参阅我的代码)。我没有得到任何错误,但我没有看到任何文字!

我在做什么错?

您可以:

  1. 试图获得一个参考控件本身,而不是依赖于ListView控件:Accessing Controls in ListView Templates
  2. 将一个虚拟对象绑定到ListView上,然后按照您原先想要的方式使用FindControl:Asp.net ListView - DataBindingHow to access web controls in from a function(看起来两个链接的底部都接近)。

问题是,在ListView上调用DataBind()之前,LayoutTemplate中的项目不可用。因此,FindControl在此之前返回null。

+0

我已经使用了第一种方式。我认为这是最好的方法。日Thnx – Martijn 2009-10-05 09:53:34

你想如何指定标签的值?当它被加载?当用户选择一些行动?

您可以实现ItemDataBound事件,并为每一行访问标签,设置它的文字...

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e) 
    { 
     if (e.Item.ItemType == ListViewItemType.DataItem) 
     { 
     Label someLabel = (Label)e.Item.FindControl("MyLabel"); 
     someLabel.Text = "Hurray!"; 
     } 
    } 

你的FindControl()将永远不会工作,因为你有一组每行的标签。形式哪一行应该FindControl获得标签?您需要先到达该行,然后获取所需的标签。

+0

我希望在页面加载时设置标签。但是我每行只有一组标签。布局模板描述了布局的权利?所以这不会重复,或者我错了吗? – Martijn 2009-10-02 18:18:32

+0

LayoutTemplate每行重复一次,您在那里添加一个占位符,ItemTemplate将放置在占位符内。不过,在ItemDataBound中,如果这是您所需要的,您应该能够访问标签以从代码背后设置一些值。 – Dante 2009-10-03 15:36:53

+0

我试过你的代码,但它不起作用。我找不到我的4个标签。我认为这是因为标签所在的行不是数据绑定的。我不在此行中使用。而且我不确定该行(标签所在的行)是否被视为DataItem,因为该行在“itemplaceholder”行之外。但我不确定。 – Martijn 2009-10-05 09:13:01