By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,398 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Accessing DataGridItem in ItemCreated Event.

P: n/a
Hello All,

When a datagrid in a web form is getting re populated from view state on
post back, it is firing the datagrid Item Created event but the
DataGridItem(e.Item.DataItem is null) in the event arguments has no values.
Is there any way to access and manipulate the row contents in this case ?

Thanks in advance for any insigts in to this.

-Souri
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Souri:
Your datasource isn't stored in the viewstate - which is why e.Item.DataItem
is null. Thankfully the viewstate only stores what it needs to recreate
the control as-is...otherwise it would grow to be too big.

Karl

"Souri Challa" <So*********@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Hello All,

When a datagrid in a web form is getting re populated from view state on post back, it is firing the datagrid Item Created event but the
DataGridItem(e.Item.DataItem is null) in the event arguments has no values. Is there any way to access and manipulate the row contents in this case ?

Thanks in advance for any insigts in to this.

-Souri

Nov 18 '05 #2

P: n/a
Does this mean, it is not possible to inspect and change values of a
datagrid row
in Item created always ? When the datagrid is re-rendering its content from
viewstate
and firing the Item Created event, shouldn't there be some way to access the
values ?

I've a requirement to remove a button column based on a value in the row.
and replace the cell with text

I do some thing like..

if (e.item.cells[0].text == "Last Level"){
LinkButton lnkBtn = ((LinkButton)e.Item.Cells[2].Controls[0]);
e.Item.Cells[2].Text = lnkBtn.Text;
e.Item.Cells[2].Controls.Remove(lnkBtn);
}

( Disabling the button colum is not option here for me.)

I tried doing this in Item Data Bound, It works but it seems this change
is not saved to view state. Upon post back, I notice that Grid has empty
values
in cell[2].

Any Ideas ?

Thanks,
-Souri

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eG*************@TK2MSFTNGP12.phx.gbl...
Souri:
Your datasource isn't stored in the viewstate - which is why e.Item.DataItem is null. Thankfully the viewstate only stores what it needs to recreate
the control as-is...otherwise it would grow to be too big.

Karl

"Souri Challa" <So*********@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Hello All,

When a datagrid in a web form is getting re populated from view state

on
post back, it is firing the datagrid Item Created event but the
DataGridItem(e.Item.DataItem is null) in the event arguments has no

values.
Is there any way to access and manipulate the row contents in this case ?
Thanks in advance for any insigts in to this.

-Souri


Nov 18 '05 #3

P: n/a
ItemCreated isn't the place to do this. ItemDataBound is. Instead of
removing things, try to change their visibility. For example, try this in
your aspx file:

<asp:Literal ID="lit" Runat="server" />
<asp:LinkButton ID="lnk" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "FIELD")%>' />

And in your codebehind, do something like this:

Literal lit = (Literal)e.Item.FindControl("lit");
linkButton lnk = (LinkButton)e.Item.FindControl("lnk")
lit.Text = lnk.Text;
lnk.Visible = False;
lit.Visible = True;

Hope this helps,
Karl
"Souri Challa" <so*********@hotmail.com> wrote in message
news:mO0Yc.84014$mD.82428@attbi_s02...
Does this mean, it is not possible to inspect and change values of a
datagrid row
in Item created always ? When the datagrid is re-rendering its content from viewstate
and firing the Item Created event, shouldn't there be some way to access the values ?

I've a requirement to remove a button column based on a value in the row.
and replace the cell with text

I do some thing like..

if (e.item.cells[0].text == "Last Level"){
LinkButton lnkBtn = ((LinkButton)e.Item.Cells[2].Controls[0]);
e.Item.Cells[2].Text = lnkBtn.Text;
e.Item.Cells[2].Controls.Remove(lnkBtn);
}

( Disabling the button colum is not option here for me.)

I tried doing this in Item Data Bound, It works but it seems this change
is not saved to view state. Upon post back, I notice that Grid has empty
values
in cell[2].

Any Ideas ?

Thanks,
-Souri

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:eG*************@TK2MSFTNGP12.phx.gbl...
Souri:
Your datasource isn't stored in the viewstate - which is why e.Item.DataItem
is null. Thankfully the viewstate only stores what it needs to recreate
the control as-is...otherwise it would grow to be too big.

Karl

"Souri Challa" <So*********@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Hello All,

When a datagrid in a web form is getting re populated from view
state on
post back, it is firing the datagrid Item Created event but the
DataGridItem(e.Item.DataItem is null) in the event arguments has no

values.
Is there any way to access and manipulate the row contents in this

case ?
Thanks in advance for any insigts in to this.

-Souri



Nov 18 '05 #4

P: n/a
Thanks a lot Karl. I'm still unclear why it is too late in the event cycle
to remove/add
controls in the Item Data Bound and what happens when DataGrid loads from
the view state.

Right now I'm using the datagrid button column in several pages with a
custom Grid class I wrote.
( which overrides the Item databound etc.)
This is the reason, why I'm a little reluctant to change the button column
to a template column
with two controls in it, in all pages.

-Regards,
Souri
"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:ex**************@tk2msftngp13.phx.gbl...
ItemCreated isn't the place to do this. ItemDataBound is. Instead of
removing things, try to change their visibility. For example, try this in
your aspx file:

<asp:Literal ID="lit" Runat="server" />
<asp:LinkButton ID="lnk" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "FIELD")%>' />

And in your codebehind, do something like this:

Literal lit = (Literal)e.Item.FindControl("lit");
linkButton lnk = (LinkButton)e.Item.FindControl("lnk")
lit.Text = lnk.Text;
lnk.Visible = False;
lit.Visible = True;

Hope this helps,
Karl
"Souri Challa" <so*********@hotmail.com> wrote in message
news:mO0Yc.84014$mD.82428@attbi_s02...
Does this mean, it is not possible to inspect and change values of a
datagrid row
in Item created always ? When the datagrid is re-rendering its content

from
viewstate
and firing the Item Created event, shouldn't there be some way to access

the
values ?

I've a requirement to remove a button column based on a value in the row.
and replace the cell with text

I do some thing like..

if (e.item.cells[0].text == "Last Level"){
LinkButton lnkBtn = ((LinkButton)e.Item.Cells[2].Controls[0]);
e.Item.Cells[2].Text = lnkBtn.Text;
e.Item.Cells[2].Controls.Remove(lnkBtn);
}

( Disabling the button colum is not option here for me.)

I tried doing this in Item Data Bound, It works but it seems this change is not saved to view state. Upon post back, I notice that Grid has empty
values
in cell[2].

Any Ideas ?

Thanks,
-Souri

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:eG*************@TK2MSFTNGP12.phx.gbl...
Souri:
Your datasource isn't stored in the viewstate - which is why

e.Item.DataItem
is null. Thankfully the viewstate only stores what it needs to

recreate the control as-is...otherwise it would grow to be too big.

Karl

"Souri Challa" <So*********@discussions.microsoft.com> wrote in message news:6B**********************************@microsof t.com...
> Hello All,
>
> When a datagrid in a web form is getting re populated from view state on
> post back, it is firing the datagrid Item Created event but the
> DataGridItem(e.Item.DataItem is null) in the event arguments has no
values.
> Is there any way to access and manipulate the row contents in this

case
?
>
> Thanks in advance for any insigts in to this.
>
> -Souri



Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.