【问题标题】:Change Label Control Property Based on Data from SqlDataSource Inside a Repeater根据中继器内 SqlDataSource 中的数据更改标签控件属性
【发布时间】:2013-07-01 16:14:03
【问题描述】:

我正在使用中继器控件将 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>

但这给出了一个错误:“服务器控件格式不正确” 并且无法在后面的代码中找到实现这一目标的方法。有人可以帮忙吗?

【问题讨论】:

标签: c# asp.net sql-server repeater sqldatasource


【解决方案1】:

尝试从这里更改您的代码:

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

到这里:

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

【讨论】:

  • 非常感谢,工作就像一个魅力 :) 它现在可以让我继续前进,但我想知道 C# 中是否有任何相同的方法。就像我看到值是什么,然后相应地更改控件的属性。
  • 是的,看看 OnItemBound 事件。
猜你喜欢
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
相关资源
最近更新 更多