DataList's ItemCreated event

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

Hi all;
I have a DataList with ItemCreated event like below:
private void myList_ItemCreated(object sender , DataListItemEventArgs e) {
DataRowView myRowView;
DataRow myRow;
if (e.Item.DataItem != null) {
Trace.Write(e.Item.ItemType.ToString(), "---with data");
} else {
Trace.Write(e.Item.ItemType.ToString(), "---without data");
}

if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) && e.Item.DataItem != null) {
myRowView = (DataRowView) e.Item.DataItem;
myRow = myRowView.Row;
// do something with myRow["Column"]
} else if (e.Item.ItemType == ListItemType.EditItem && e.Item.DataItem !=
null) {
myRowView = (DataRowView) e.Item.DataItem;
myRow = myRowView.Row;
// do something with myRow["Column"]
}
}

I traced the event with Trace statement, and found that after page was
posted back, I got following values from trace.axd:
Item ---without data 0.090712 0.079591
AlternatingItem ---without data 0.091327 0.000614
Item ---without data 0.091959 0.000632
AlternatingItem ---without data 0.092603 0.000645
Which means e.Item.DataItem is always null.

Thus caused some section (to render the result within DataList for some
controls like Literal, Label and etc) won't get executed after posted back.
I enabled ViewState for all page, no single control is disabled to use
ViewState.

Anybody has some ideas?
 
Forgot to mentio my ASPX page is like:
<asp:DataList id="myList" width="95%" CellSpacing="10" CellPadding="5"
border="0" RepeatDirection="horizontal" RepeatColumns="3" runat="server"
EnableViewState="True" DataKeyField="IDNumber">
<ItemStyle VerticalAlign="top" />
<ItemTemplate>
<b>Size</b> <asp:Literal id="lSize" EnableViewState="True" runat="server"
/> KB<br/>
<asp:LinkButton id="btnEdit" CommandName="Edit" Text="Edit"
CausesValidation="false" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<b>Size</b> <asp:Literal id="lSize1" runat="server" /> KB<br/>
<table width=100% border=0>
<tr>
<td><asp:LinkButton CommandName="Update" id="btnUpdate" Text="Update"
CausesValidation="false" runat="server" /></td>
<td><asp:LinkButton CommandName="Cancel" id="btnCancel" Text="Calcen"
CausesValidation="false" runat="server" /></td>
<td><asp:LinkButton CommandName="Delete" id="btnDelete" Text="Delete"
CausesValidation="false" runat="server" /></td>
</tr>
</table>
</EditItemTemplate>
</asp:datalist>

The first time I run page, lSize in DataList will be populated with value,
but after posted back its value was lost...
 
The DataSource of the datalist is probably null on post back.

L.L.

Hardy Wang said:
Forgot to mentio my ASPX page is like:
<asp:DataList id="myList" width="95%" CellSpacing="10" CellPadding="5"
border="0" RepeatDirection="horizontal" RepeatColumns="3" runat="server"
EnableViewState="True" DataKeyField="IDNumber">
<ItemStyle VerticalAlign="top" />
<ItemTemplate>
<b>Size</b> <asp:Literal id="lSize" EnableViewState="True" runat="server"
/> KB<br/>
<asp:LinkButton id="btnEdit" CommandName="Edit" Text="Edit"
CausesValidation="false" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<b>Size</b> <asp:Literal id="lSize1" runat="server" /> KB<br/>
<table width=100% border=0>
<tr>
<td><asp:LinkButton CommandName="Update" id="btnUpdate" Text="Update"
CausesValidation="false" runat="server" /></td>
<td><asp:LinkButton CommandName="Cancel" id="btnCancel" Text="Calcen"
CausesValidation="false" runat="server" /></td>
<td><asp:LinkButton CommandName="Delete" id="btnDelete" Text="Delete"
CausesValidation="false" runat="server" /></td>
</tr>
</table>
</EditItemTemplate>
</asp:datalist>

The first time I run page, lSize in DataList will be populated with value,
but after posted back its value was lost...

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Hardy Wang said:
Hi all;
I have a DataList with ItemCreated event like below:
private void myList_ItemCreated(object sender , DataListItemEventArgs e) {
DataRowView myRowView;
DataRow myRow;
if (e.Item.DataItem != null) {
Trace.Write(e.Item.ItemType.ToString(), "---with data");
} else {
Trace.Write(e.Item.ItemType.ToString(), "---without data");
}

if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) && e.Item.DataItem != null) {
myRowView = (DataRowView) e.Item.DataItem;
myRow = myRowView.Row;
// do something with myRow["Column"]
} else if (e.Item.ItemType == ListItemType.EditItem && e.Item.DataItem !=
null) {
myRowView = (DataRowView) e.Item.DataItem;
myRow = myRowView.Row;
// do something with myRow["Column"]
}
}

I traced the event with Trace statement, and found that after page was
posted back, I got following values from trace.axd:
Item ---without data 0.090712 0.079591
AlternatingItem ---without data 0.091327 0.000614
Item ---without data 0.091959 0.000632
AlternatingItem ---without data 0.092603 0.000645
Which means e.Item.DataItem is always null.

Thus caused some section (to render the result within DataList for some
controls like Literal, Label and etc) won't get executed after posted back.
I enabled ViewState for all page, no single control is disabled to use
ViewState.

Anybody has some ideas?
 
Yes, I agree. But it also seems that ViewState did not include the value for
child control of DataList, if I don't use
DataBinder.Eval(Container.DataItem, "FieldName")%> to populate values.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
L. L. said:
The DataSource of the datalist is probably null on post back.

L.L.

Hardy Wang said:
Forgot to mentio my ASPX page is like:
<asp:DataList id="myList" width="95%" CellSpacing="10" CellPadding="5"
border="0" RepeatDirection="horizontal" RepeatColumns="3" runat="server"
EnableViewState="True" DataKeyField="IDNumber">
<ItemStyle VerticalAlign="top" />
<ItemTemplate>
<b>Size</b> <asp:Literal id="lSize" EnableViewState="True" runat="server"
/> KB<br/>
<asp:LinkButton id="btnEdit" CommandName="Edit" Text="Edit"
CausesValidation="false" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<b>Size</b> <asp:Literal id="lSize1" runat="server" /> KB<br/>
<table width=100% border=0>
<tr>
<td><asp:LinkButton CommandName="Update" id="btnUpdate" Text="Update"
CausesValidation="false" runat="server" /></td>
<td><asp:LinkButton CommandName="Cancel" id="btnCancel" Text="Calcen"
CausesValidation="false" runat="server" /></td>
<td><asp:LinkButton CommandName="Delete" id="btnDelete" Text="Delete"
CausesValidation="false" runat="server" /></td>
</tr>
</table>
</EditItemTemplate>
</asp:datalist>

The first time I run page, lSize in DataList will be populated with value,
but after posted back its value was lost...
e)
{
DataRowView myRowView;
DataRow myRow;
if (e.Item.DataItem != null) {
Trace.Write(e.Item.ItemType.ToString(), "---with data");
} else {
Trace.Write(e.Item.ItemType.ToString(), "---without data");
}

if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem) && e.Item.DataItem != null) {
myRowView = (DataRowView) e.Item.DataItem;
myRow = myRowView.Row;
// do something with myRow["Column"]
} else if (e.Item.ItemType == ListItemType.EditItem &&
e.Item.DataItem
!=
null) {
myRowView = (DataRowView) e.Item.DataItem;
myRow = myRowView.Row;
// do something with myRow["Column"]
}
}

I traced the event with Trace statement, and found that after page was
posted back, I got following values from trace.axd:
Item ---without data 0.090712 0.079591
AlternatingItem ---without data 0.091327 0.000614
Item ---without data 0.091959 0.000632
AlternatingItem ---without data 0.092603 0.000645
Which means e.Item.DataItem is always null.

Thus caused some section (to render the result within DataList for some
controls like Literal, Label and etc) won't get executed after posted back.
I enabled ViewState for all page, no single control is disabled to use
ViewState.

Anybody has some ideas?
 
Back
Top