views:

59

answers:

1

I am facing a strange behaviour for an .aspx page.

I have DataList called MyDataList. I need to conditionally highlight the rows of the DataList, depending on an arbitrary value in the data, such as if a Date field is less then SomeDate.

I am using an UpdatePanel, ScriptManager and a timer (all AJAX) to refresh MyDataList.

protected void Timer1_Tick(object sender, EventArgs e)
{                
    MyDataList.DataBind();
}

protected void MyDataList_ItemCreated(object sender, DataListItemEventArgs e)
{
}

The Problem:

If I add an empty event handler for the ItemCreated event (EG, MyDataList_ItemCreated), it works fine (as shown above).

If I provide code to highlight the value in the ItemCreated event handler (as shown below), the Timer stops ticking, and the event Timer1_Tick does not fire any more.

protected void DataListBgArticles_ItemCreated(object sender, 
    DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Product product = (Product)e.Item.DataItem;
        if (product.SaleDate > DateTime.Now.AddDays(-2))
        {
            e.Item.BackColor = Color.Pink;
        }
    }
}

How can I fix this so that the Timer continues to update?

A: 

Forgive the question, but I think you should specify exactly why you are performing a data binding operation in a timer callback in an ASP.NET page.

There aren't many (ok, any) situations I can think of which would require such a thing.

The timer callback basically means you place the action outside of the page processing pipeline, and because of this, it's completely possible that any objects that the code implicitly or explicitly depends on are no longer available.

Because you are performing data binding, it's completely possible that your timer event is firing and trying to update a grid which has already been pushed out to the user.

It appears that you are using the timer to fire the event to update the data grid. The problem with this is that you are updating the structure on the server side, but it doesn't equate an update on the client. The client needs to refresh itself in order to get this data (the server has no way of actually connecting to the client and refreshing).

casperOne
Timer event is not firing at all in the second case as I have put BreakPoints inside Timer1_Tick and see no action.
Asad Butt
@Asad Butt - See the last sentence of my answer (I've revised it). It appears you have the wrong idea of how to perform updates to the client side.
casperOne