2013-07-01 34 views
0

我正在使用中繼器控件將數據從SqlDataSource填充到我自定義設計的顯示框中。基於SqlDataSource中的數據更改標籤控制屬性在中繼器內

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnDataBinding="Repeater_ItemDataBound"> 
<HeaderTemplate> 

</HeaderTemplate> 

<ItemTemplate> 

    <div class="bubble-content"> 
    <div style="float: left;"> 
     <h2 class="bubble-content-title"><%# Eval("CommentTitle") %></h2> 
    </div> 

    <div style="text-align: right;"> 
     <asp:Label ID="lbl_category" runat="server" Text=""><%# Eval("CommentType") %> 
     </asp:Label> 
    </div> 

    <div style="float: left;"> 
     <p><%# Eval("CommentContent") %></p> 
    </div> 
    </div> 
</ItemTemplate> 

<FooterTemplate> 
</FooterTemplate> 

</asp:Repeater> 

<asp:SqlDataSource ID="mySqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
    SelectCommand="SELECT [CommentTitle],[CommentType],[CommentContent] FROM [Comments] WHERE ([PostId] = @PostId)"> 
    <SelectParameters> 
     <asp:QueryStringParameter Name="PostId" QueryStringField="id" Type="String" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

現在,數據庫中可以有三種類型的「CommentTypes」。我想根據[CommentType]的值更改「lbl_category」的CssClass屬性。

我試着這樣做:

<asp:Label ID="lbl_category" runat="server" CssClass="<%# Eval("CommentType") %>" Text=""><%# Eval("CommentType") %></asp:Label> 

但是,這給出了一個錯誤:「服務器控件沒有很好地形成」 而一直沒能找到一種方法,在代碼中實現這一落後。有人可以幫忙嗎?

+1

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

嘗試從這個chaning代碼:

<asp:Label ID="lbl_category" runat="server" CssClass="<%# Eval("CommentType") %>" 

這樣:

<asp:Label ID="lbl_category" runat="server" CssClass='<%# Eval("CommentType") %>' /> 
+0

非常感謝,像一個魅力工作:) 它可以讓我現在去,但我想知道是否有任何方式在C#中相同。就像我看到什麼是價值,然後相應地更改控件的屬性。 –

+1

是的,看看OnItemBound事件。 –

相關問題