简体   繁体   English

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

[英]C# How to set label text in a listview?

In my codebehind I want the set the text of a label.在我的代码隐藏中,我想要设置标签的文本。 Here is the aspx code:下面是aspx代码:

<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>

In the top, in the layoutTemplate I have 4 labels which text property I want to change.在顶部的 layoutTemplate 中,我有 4 个标签,我想更改其中的文本属性。 I've tried to access the labels by using the lstRegistrations.FindControl() method, but this method doesn't find the labels.我尝试使用 lstRegistrations.FindControl() 方法访问标签,但此方法找不到标签。 I've also tried the Page.FindControl() method, but this method either can't find the labels.我也试过 Page.FindControl() 方法,但是这个方法要么找不到标签。 Then I thought, I create a method and invoke this in my aspx page (see my code).然后我想,我创建了一个方法并在我的 aspx 页面中调用它(见我的代码)。 I don't get any erros, but I don't see any text!我没有得到任何错误,但我没有看到任何文字!

What am I doing wrong?我究竟做错了什么?

How do you want to specify the value for the label?您想如何指定标签的值? When it is being loaded?什么时候加载? When the user selects some action?当用户选择某个动作时?

You can implement the ItemDataBound event and for each row access the label to set its text...您可以实现 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!";
    }
}

Your FindControl() will never work because you have a set of labels per row.您的FindControl()永远不会工作,因为您每行都有一组标签。 Form which row should the FindControl get the label? FindControl 应从哪一行获取标签? You need to reach to the row first, and then get the Label needed.您需要先到达该行,然后才能获取所需的 Label。

You can:你可以:

  1. Attempt to gain a reference to the controls themselves, rather than rely on the ListView: Accessing Controls in ListView Templates .尝试获得对控件本身的引用,而不是依赖 ListView: Accessing Controls in ListView Templates
  2. Bind a dummy object to the ListView then use FindControl in the manner you originally intended: Asp.net ListView - DataBinding and also How to access web controls in from a function (look near the bottom for both links).将一个虚拟对象绑定到 ListView,然后以您最初打算的方式使用 FindControl: Asp.net ListView - DataBinding以及如何从函数中访问 Web 控件(查看靠近底部的两个链接)。

The problem is that the items in the LayoutTemplate are not available until DataBind() is called on the ListView.问题是 LayoutTemplate 中的项目在 ListView 上调用 DataBind() 之前不可用。 Thus, FindControl returns null before that.因此,FindControl 在此之前返回 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM