views:

1029

answers:

2

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?

+1  A: 

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

    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. Form which row should the FindControl get the label? You need to reach to the row first, and then get the Label needed.

Dante
I want the labels be set while the page is loading. But I have only one set of labels per row. The layout template describes the lay out right? So this ain't repeated or am I wrong?
Martijn
The LayoutTemplate is repeated per row, you add a placeholder in there and the ItemTemplate will be placed inside the Placeholder. Still, in the ItemDataBound you should be able to reach the labels to set some values from code behind, if that's what you need.
Dante
I've tried your code, but it doesn't work. I can't find my 4 labels. And I think this is because the row where the labels are located are not data bound. I don't use <%# %> in this row. And I am not sure that the row (where the labels are placed in) is seen as a DataItem because the row is outside the 'itemplaceholder'-row. But I'm not sure.
Martijn
You're correct, it doesn't work. Like you answered to Ahmad, the best way would be to do a FindControl("lblName") in the ListView.
Dante
Okay :) Thnx for the effort though :)
Martijn
A: 

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.

Ahmad Mageed
I've used the the first manner. I think this is the neatest method. Thnx
Martijn