【问题标题】:Highlight (bold font) row in asp:Repeater with timer在 asp 中突出显示(粗体)行:Repeater with timer
【发布时间】:2012-11-16 01:43:32
【问题描述】:

我有一个用中继器显示的活动列表。我已经使用定时器来显示这些,所以定时器不是问题。这是我必须放入Timer_tick 方法的代码。

“highlight-Timer”应该一次突出显示项目/行,然后我想显示一些与突出显示的行相关的信息。

如果使用另一个控件更容易,那也没问题。我不必成为中继器。我只是因为样式可能性而使用它(它不仅仅显示在一行中,而是有几个换行符等)

根据要求:

(中继器)

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div id="divActivity" runat="server">
            <span style="font-size:30px;">
                <%# DataBinder.Eval(Container.DataItem, "act_headline") %>
            </span>
            <br />
            <img alt="mapIcon" src="../img/mapIcon.gif" height="15px" width="15px" style="position:absolute;" />
            <span style="font-size:12px; font-style:italic; margin-left:23px;">    
                <%# DataBinder.Eval(Container.DataItem, "act_place") %>
            </span>
            <img alt="watchIcon" src="../img/watchIcon.png" height="15px" width="15px" style="position:absolute;" /> 
            <span style="font-size:12px; font-style:italic; margin-left:23px;">
                <%# DataBinder.Eval(Container.DataItem, "act_start") %> - <%# DataBinder.Eval(Container.DataItem, "act_end") %>
            </span>
            <br />
            <div style="word-wrap: break-word; width:1000px; margin-top:20px; padding:0;">
                <%# DataBinder.Eval(Container.DataItem, "act_text") %>
            </div>
            <br />
                <img alt="infoIcon" src="../img/infoIcon.png" height="15px" width="15px" style="position:absolute;" />
            <span style="font-size:12px; margin-left:23px;"><a target="_blank" href="http://<%# DataBinder.Eval(Container.DataItem, "act_info") %>"><%# DataBinder.Eval(Container.DataItem, "act_info") %></a>
            </span>
        </div>
    </ItemTemplate>
</asp:Repeater>

我在 Timer_tick 事件中没有任何内容,因为我尝试了几件事并在失败后将其删除。希望够了。

【问题讨论】:

  • 你能告诉我们一些你迄今为止尝试过的代码吗?
  • 我已将中继器的代码添加到主帖中。
  • 如果我理解你的意思,你想一次突出一个项目?
  • 没错!第 1 行,第 2 行等。

标签: c# asp.net timer repeater highlight


【解决方案1】:

我会通过循环Repeater.Items 集合在滴答事件处理程序中使用类似这样的东西来为突出显示的项目设置一个特殊的 CSS 类。

类似这样的东西(未经测试):

int currentHighlight = -1;
int nextHighlight = -1;
for (int i = 0; i < Repeater1.Items.Count; i++)
{
    HtmlControl htmlDiv = (HtmlControl)Repeater1.Controls[i].Controls[1];

    if (htmlDiv.Attributes["class"] != null)
    {
        if (htmlDiv.Attributes["class"].Contains("highlighted")) // this is the currently highlighted item
        {
            // record currently highlighted item index
            currentHighlight = i;

            // remove highlight
            htmlDiv.Attributes["class"].Replace("highlighted", "");
            htmlDiv.Attributes["class"].Trim();
        }
    }
}

if ((currentHighlight == -1) || (currentHighlight == Repeater1.Items.Count))
{
    // handle first Item highlighting
    nextHighlight = 1;
}
else
{
    // handle standard case
    nextHighlight = currentHighlight + 1;
}

// highlight next item
((HtmlControl)Repeater1.Controls[nextHighlight].Controls[1]).Attributes["class"] += " highlighted";

然后您可以使用highlighted CSS 类来处理样式(您提到的粗体字体)以及 JavaScript 或 jQuery 中的一些特殊行为。

顺便说一句,您可以在RepeaterItem 对象上执行任何其他操作,一旦您得到当前突出显示的对象。

【讨论】:

  • 我现在正在测试它,但我收到RepeaterItem 不包含CssClass 的定义的错误?
  • 抱歉,您必须使用 HtmlControl 类。我已经编辑了上面的代码...
  • 我试过了,但问题是,每行的 div (divActivity) 的 id 都会发生变化,因此它们是唯一的。当我使用“自动生成”的 ID 时,它不起作用。不过,当我在“静态” div-id 上尝试它时,我做到了。您将如何获得“自动生成”的 ID?也许你知道让它工作的方法..
  • Repeater1_divActivity_# WHERE # 第一行为 0,第二行为 1,依此类推(第一行:Repeater1_divActivity_0)。我得到的值是:Repeater1.Controls[].Controls[1].ClientID;
  • 它在这一行给了我一个错误(对象引用未设置为对象的实例)(if (htmlDiv.Attributes["class"].Contains("highlighted")))。我检查了它是否真的返回了 div 并且它确实返回了 (Repeater1.Controls[i].Controls[1].ClientID;) 所以这不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 2019-01-30
相关资源
最近更新 更多