Timer behaving strangely!
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?
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).
精彩评论