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>
<aspataList ID="DataList1" runat="server"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
aaa
</ItemTemplate>
</aspataList>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
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>
<aspataList ID="DataList1" runat="server"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
aaa
</ItemTemplate>
</aspataList>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>