Change Label Control Property Based on Data from SqlDataSource Inside a Repeater
- by Furqan Muhammad Khan
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?