Accessing DataGridItem in ItemCreated Event.

G

Guest

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
 
K

Karl

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
 
S

Souri Challa

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
 
K

Karl

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 said:
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 said:
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

state
on
case
 
S

Souri Challa

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 said:
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 said:
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

message news:[email protected]...
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

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
 

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,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top