Change Label Control Property Based on Data from SqlDataSource Inside a Repeater

Posted by Furqan Muhammad Khan on Stack Overflow See other posts from Stack Overflow or by Furqan Muhammad Khan
Published on 2013-07-01T16:14:03Z Indexed on 2013/07/01 16:21 UTC
Read the original article Hit count: 198

I am using a repeater control to populate data from SqlDataSource into my custom designed display-box.

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

Now, there can be three types of "CommentTypes" in the database. I want to change the CssClass property of "lbl_category" based on the value of [CommentType].

I tried doing this:

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

But this gives an error: "The server control is not well formed" and haven't been able to find a way to achieve this in the code behind. Can someone please help?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET