OnItemDataBound problem

M

Mikeon

Hello!

I have a problem customizing the databound controls using the
OnItemDataBound event.
The code below is an example of what I'm trying to do. I have a
datasource and basing on the data I want to customize the cells in the
DataList for example by setting the CssClass property.
Everything works fine the first time, but on PostBack the
OnItemDataBound does not fire because the datasource is only bound when
!IsPostBack and the CssClass property is not set (class attribute in
html). If I bind the data every time - everything works fine but I
wonder if the viewstate should kick in at some point? Additionaly when
using datasource controls I have observed that on PostBack the data is
not bound.


Any advice on this issue?

-------------------------

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int[] table = new int[3];
table[0] = 10;
table[1] = 11;
table[2] = 12;
DataList1.DataSource = table;
DataList1.DataBind();
}
}

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType
== ListItemType.AlternatingItem))
{
e.Item.CssClass = e.Item.DataItem.ToString();
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
aaa
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
 
M

Mikeon

Hello!

OK! I have checked almost the same code but replaced the DataList with
the DataGrid control.
Guess what? It works! It also works with GridView.

Any hints on why DataList does not work with postbacks?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top