更改 ListView 在 itemCreated 事件中创建的行的样式

发布于 2024-09-14 09:11:36 字数 477 浏览 7 评论 0原文

当渲染数据绑定 ListView 时,我想根据结果动态设置每行的背景颜色,在我的例子中为红色、橙色和绿色。

        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        DataRow myRow;
        DataRowView myRowView;
        myRowView = (DataRowView)e.Item.DataItem;
        myRow = myRowView.Row;

        if (myRow[2].ToString().CompareTo("") == 1)
        {
          // Colour coding here..    
        }

    }

是否可以到达每一行的TR标签来更改样式?

非常感谢, 斯特凡

When render a databound ListView I want to dynamically set the background colour of each row depending on the results, in my case Red, Orange and Green.

        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        DataRow myRow;
        DataRowView myRowView;
        myRowView = (DataRowView)e.Item.DataItem;
        myRow = myRowView.Row;

        if (myRow[2].ToString().CompareTo("") == 1)
        {
          // Colour coding here..    
        }

    }

Is it possible to reach the TR tag for each row to change the style?

Many thanks,
Stefan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港小镇 2024-09-21 09:11:36

TR 标签必须具有 runat="server" 才能使用服务器端代码;但是,您可以通过检查该项目的子项控件来注入它,而无需这样做; HTML 中可能有一个 Literal 或 LiteralControl,您可以使用字符串操作来注入...

The TR tag would have to have runat="server" to use server-side code; however, you may be able to inject it in without that by examining the controls that are the child of the item; there's probably a Literal or LiteralControl with the HTML, and you can use string manipulation to inject...

屋檐 2024-09-21 09:11:36

在布莱恩的大力帮助下,我找到了解决问题的方法。

我有一个 ListView,我添加了一个 id 标签(trRow)和标签 runat="server",如下所示:

<AlternatingItemTemplate>
            <tr id="trRow" runat="server" style="background-color:#FFF8DC;">

在它后面的代码中看起来像这样:

        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        DataRow myRow;
        DataRowView myRowView;
        myRowView = (DataRowView)e.Item.DataItem;
        myRow = myRowView.Row;

        System.Web.UI.HtmlControls.HtmlTableRow myTR = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");

        if (myRow[2].ToString().CompareTo("") == 1)
        {
            myTR.Style.Value = "background-color:#FF0000;color: #000000;";
        } else
            myTR.Style.Value = "background-color:#00FF00;color: #000000;";

    }

其中的一些逻辑仍然不正确等,只是为了展示我如何解决这个问题问题是动态更改每行的背景颜色。

I worked out a solution to my problem, with some good help from Brian.

I have a ListView and I added an id tag (trRow) and tag runat="server" like this:

<AlternatingItemTemplate>
            <tr id="trRow" runat="server" style="background-color:#FFF8DC;">

In code behind it looks like this:

        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        DataRow myRow;
        DataRowView myRowView;
        myRowView = (DataRowView)e.Item.DataItem;
        myRow = myRowView.Row;

        System.Web.UI.HtmlControls.HtmlTableRow myTR = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");

        if (myRow[2].ToString().CompareTo("") == 1)
        {
            myTR.Style.Value = "background-color:#FF0000;color: #000000;";
        } else
            myTR.Style.Value = "background-color:#00FF00;color: #000000;";

    }

Some of the logic in there is still not correct etc, just to show how I solved the issue to dynamically change the background color of each row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文