2009-10-02 112 views
0

在我的代碼隱藏中,我希望設置標籤的文本。下面是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頁面中調用它(請參閱我的代碼)。我沒有得到任何錯誤,但我沒有看到任何文字!

我在做什麼錯?

回答

0

您可以:

  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

3

你想如何指定標籤的值?當它被加載?當用戶選擇一些行動?

您可以實現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

相關問題