Search code examples
c#asp.netlistviewlabel

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:

<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. I've tried to access the labels by using the lstRegistrations.FindControl() method, but this method doesn't find the labels. I've also tried the Page.FindControl() method, but this method either can't find the labels. Then I thought, I create a method and invoke this in my aspx page (see my code). I don't get any erros, but I don't see any text!

What am I doing wrong?


Solution

  • You can:

    1. Attempt to gain a reference to the controls themselves, rather than rely on the 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).

    The problem is that the items in the LayoutTemplate are not available until DataBind() is called on the ListView. Thus, FindControl returns null before that.