2

我正在使用asp:Repeater,我需要一些帮助。在我的表格中,我有一个<td>,我想更改颜色,当且仅当<td>它的右侧包含某个文本。

代码如下所示:

 <asp:Repeater ID="myRepeater" runat="server">
        <HeaderTemplate>
            <div id="myDiv">
                <table id="table">
                    <thead>
                        <tr>
                            <th class="class">
                                Row 1
                            </th>
                            <th class="class">
                                Row 2
                            </th>
                            <th class="class">
                                Row 3                               
                            </th>
                        </tr>
                    </thead>
                    <tbody>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td class="myClass">
                </td>
                <td class="changeMyColor!">
                </td>
        <td class="lookAtMe">
         certainText
        </td>
    </ItemTemplate>

在这种情况下,如果包含“certainText”,我想给背景颜色。这对我来说也具有挑战性,中继器中的每个项目都必须这样做。

我在 jsfiddle 上找到了这个,但是在我的解决方案中使用它来实现它没有任何运气。

谢谢你的帮助!

4

4 回答 4

1

试试这个例子:

http://www.c-sharpcorner.com/blogs/7592/change-the-forecolor-of-an-item-in-a-repeater-based-on-the-s.aspx

您可以在您使用的 sql 适配器类型上使用foreach,并验证调用IF的方法

dt.NewRow();

,在示例中,因此验证每个字段并为其分配您想要的颜色

于 2012-07-16T20:20:52.363 回答
1

尝试这个 :

$(document).ready(function()
{
  $('td').each(function(index) 
  {
    if($(this).text().indexOf("certainText") >= 0 )
    {
      $(this).css('background-color','red');
    }
  });
});
于 2012-07-16T20:14:26.090 回答
1

我不知道你DataSource是什么,但你可以改变下面的代码来做它的 ASP.NET 方式:

标记:

 <asp:Repeater ID="myRepeater" runat="server"                 
           OnItemDataBound="myRepeater_ItemDataBound">
  ................
 <ItemTemplate>
        <tr>
            <td class="myClass" id="td1" runat="server">
            </td>
            <td class="changeMyColor!" id="td2" runat="server">
            </td>
    <td class="lookAtMe">
     <asp:label id="lookAtMe" Text="<%#Eval("LookAtMe")%>" />
    </td>
 </ItemTemplate>

代码隐藏:

  protected void myRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      {
         YourDataObject data = (YourDataObject)e.Item.DataItem;
         HtmlGenericControl td1 = e.Item.FindControl("td1") as HtmlGenericControl;
         HtmlGenericControl td1 = e.Item.FindControl("td2") as HtmlGenericControl;           

         if (data.YourProperty == "CertainText") {                                 
            td1.Attributes.Add("class","whateverClass");
            td2.Attributes.Add("class","whateverClass2");
         }

      }
   }    
于 2012-07-16T20:24:24.873 回答
1
jQuery(document).ready(function($) {

  //Find <td>'s in your table id="table" which contains text 'certainText'
  $('#table tbody td.lookAtMe:contains(certainText)').each(function() {

    //You said the test was for the <td> to the right of it
    //which means .prev() will return the <td> to the left
    //select that <td> and change its color
    $(this).prev().css({'background-color':'#a90000'});
  });
});
于 2012-07-16T22:40:36.677 回答