Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在itemCreated事件中更改ListView创建的行的样式_C#_Asp.net_Listviewitem - Fatal编程技术网

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

C# 在itemCreated事件中更改ListView创建的行的样式,c#,asp.net,listviewitem,C#,Asp.net,Listviewitem,渲染数据绑定列表视图时,我希望根据结果动态设置每行的背景颜色,在我的示例中为红色、橙色和绿色 protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e) { DataRow myRow; DataRowView myRowView; myRowView = (DataRowView)e.Item.DataItem;

渲染数据绑定列表视图时,我希望根据结果动态设置每行的背景颜色,在我的示例中为红色、橙色和绿色

        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标记以更改样式

非常感谢,,
Stefan

TR标签必须具有runat=“server”才能使用服务器端代码;但是,通过检查作为该项子项的控件,您可以在不使用该控件的情况下将其注入;HTML中可能有一个Literal或LiteralControl,您可以使用字符串操纵来注入…

TR标记必须有runat=“server”才能使用服务器端代码;但是,通过检查作为该项子项的控件,您可以在不使用该控件的情况下将其注入;HTML中可能有一个Literal或Literal控件,您可以使用字符串操作来注入…

在Brian的帮助下,我找到了解决问题的方法

我有一个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;";

    }

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

在Brian的帮助下,我找到了解决问题的方法

我有一个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;";

    }

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

Hi,您的意思是在ItemCreated事件中使用Sender还是ListViewItemEventArgs?不是100%确定你在这里的建议//感谢如果您使用服务器端TR,请使用e.Item.FindControl(“trid”);否则,我不是100%确定,但我认为e.Item有一个Controls集合,您可以循环查找带有要注入的标记的文本。您好,您的意思是在ItemCreated事件中使用Sender还是ListViewItemEventArgs?不是100%确定你在这里的建议//感谢如果您使用服务器端TR,请使用e.Item.FindControl(“trid”);否则,我不是100%确定,但我认为e.Item有一个Controls集合,您可以循环查找带有要注入的标记的文本。