tags:

views:

23

answers:

1

Hi,

I'm a bit lost here with how the ListView works.

When my ListView is created I do some coloring depending of the data in the result set.

protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
    DataRow myRow;
    DataRowView myRowView;
    myRowView = (DataRowView)e.Item.DataItem;
    myRow = myRowView.Row;
    HtmlTableRow myTR = (HtmlTableRow)e.Item.FindControl("trRow");
    HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell");

    Label myCB = (Label)e.Item.FindControl("ResultLabel1");
    if (myRow["Result"].ToString().Equals("true"))
        myTC.Style.Value = "background-color:#00FF00;color: #000000;";
    else
        myTC.Style.Value = "background-color:#FF0000;color: #000000;";
}

The ListView has a pager and that is causing a postback to ItemCreated and I cant reach the dataRow again.

Any suggestion how to deal with this so I can handle postbacks?

UPDATE: I changed the code to read the actual value of the Label called ResultLabel1 which is True/False but it returns back an empty string.. Thought this was better than reading the the result from the result from the DataSet. Can anyone see whats wrong?

Code:

        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        HtmlTableRow myTR = (HtmlTableRow)e.Item.FindControl("trRow");
        HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell");

        // Retrieve the current item.
        ListViewItem item = e.Item;

        // Verify if the item is a data item.
        if (item.ItemType == ListViewItemType.DataItem)
        {
            // Get the Label ResultLabel1 control in the item.
            Label myCB = (Label)item.FindControl("ResultLabel1");


            if (myCB.Text.Equals("True"))
                myTC.Style.Value = "background-color:#00FF00;color: #000000;";
            else
                myTC.Style.Value = "background-color:#FF0000;color: #000000;";

        }
    }

ASP.NET:

            <ItemTemplate>
            <tr id="trRow" runat="server" style="">
                <td>
                    <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                </td>
                <td>
                    <asp:Label ID="DateTime_StartLabel" runat="server" 
                        Text='<%# Eval("DateTime_Start") %>' />
                </td>
                <td>
                    <asp:Label ID="DateTime_EndLabel" runat="server" 
                        Text='<%# Eval("DateTime_End") %>' />
                </td>
                <td>
                    <asp:Label ID="TestCaseNameLabel" runat="server" 
                        Text='<%# Eval("TestCaseName") %>' />
                </td>
                <td>
                    <asp:Label ID="ModelNameLabel" runat="server" Text='<%# Eval("ModelName") %>' />
                </td>
                <td id="tdCell" runat="server">
                    <asp:Label ID="ResultLabel1" runat="server" 
                        Text='<%# Eval("Result") %>' />
                </td>
            </tr>
        </ItemTemplate>
+2  A: 

If you're doing this colouring as a result of data binding, you might be better handling the ItemDataBound event instead of ItemCreated.

Graham Clark
Hi, How can I reach the current rows from the ItemDataBound event? In ItemCreated I do:HtmlTableCell myTC = (HtmlTableCell)e.Item.FindControl("tdCell");
StefanE
Worked all better when put the code in ItemDataBound regardless my solution. Thanks!
StefanE