Datagrid ItemCreated runs too many times.

M

Manuel

I have a Datagrid with 9 elements. The problem is that when I sort the grid
by a column, it present more columns than there are.

This is my code for the ItemCreated event:

Private pNo As Integer = 0

Private Sub grd_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles grd.ItemCreated

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Response.Write(Me.pNo & ",")

Me.pNo += 1

End If

End Sub



The first time I run the page it displays the list: 0,1,2,3,4,5,6,7,8,

But when I click one of the column headers to sort the grid I get the
following list: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,



Does anyone have an idea of what's happening here?
 
S

Scott Allen

When you click to sort the grid the form will post back and the grid
will restore it's settings from ViewState (which fires ItemCreated).
Presumably in the sort event handler you'll call DataBind to get the
data into the grid in a different sort order, which recreates the grid
(and fires ItemCreated again).
 
U

ujjc001

Are you binding it on sort and in your postback? Follow the code from
your header click and see if it is getting bound two times. Also you
could put your Private pNo As Integer = 0 in your Private Sub
grd_ItemCreated command, that way it resets to 0 each time your make
the datagrid.
 
U

ujjc001

If you want your rows to count, regardless of their position after to
sort, put a "pNo = 0" line right before your databind. That way
whenever you bind you'll be sure to start at 0.
Jeff
 
M

Manuel

I reset the pNo = 0 at databind and now I get
0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,6,7,8,

I forgot to mention in the original post. The html table displayed has the
propper amount of rows (9) even though the ItemCreated event is run twice
for each set.

I have a function that binds the data and I tested how many times the grid
bind occurrs and it shows it's run once (as expected).

At this point, besides solving this problem, I would like to understand
what/why is happening.
 
S

Scott Allen

Each item is created twice.

The first time is when the grid restores settings from ViewState and
rebuilds itself.

The second time is when you call DataBind and the grid builds itself
again, this time with the updated (sorted) data.
 
M

Manuel

New problem now :(

If I create a control (for me it's a button) on the ItemDataBound event, the
control isn't created when doing a postback (the event isn't triggered). I
thought of putting the code on the ItemCreated event but the problem is that
I make use of another column text (e.Item.Cells(0).Text) and when I ask for
it's value it returns blank.

In short, what's happening to me is:
_ItemDataBound event:
Can retrieve column values using e.Item.Cells(0).Text
Doesn't occurr if it's a postback (other than a Column Sort or Paging)

_ItemCreated event:
Cannot retrieve column values using e.Item.Cells(0).Text
Occurrs always

My solution (if it can be called one :p) was to force a gridbind everytime.
It seems like brute force imo, is there a better way to do this? or is there
a way to grab a column value in the ItemCreated event?

Thanks
 
J

John Saunders

Manuel said:
New problem now :(

If I create a control (for me it's a button) on the ItemDataBound event,
the control isn't created when doing a postback (the event isn't
triggered). I thought of putting the code on the ItemCreated event but the
problem is that I make use of another column text (e.Item.Cells(0).Text)
and when I ask for it's value it returns blank.

Why is that text blank? Was that column databound, perhaps?

You can create the button on ItemCreated, and fill in its value during
ItemDataBound. On PostBack, the ItemCreated event will occur and you'll
create the button again, but this time, it will get its value from
ViewState.

John Saunders
 
M

Manuel

If I create a control (for me it's a button) on the ItemDataBound event,
Why is that text blank? Was that column databound, perhaps?

Yes, the column is databound.
You can create the button on ItemCreated, and fill in its value during
ItemDataBound.

Good solution it it was a single button, but I need the value of the column
to know how many buttons to create :(
 
J

John Saunders

Manuel said:
Yes, the column is databound.


Good solution it it was a single button, but I need the value of the
column to know how many buttons to create :(

Then either use a repeater/datalist/datagrid in the column, and databind to
the data that tells you the number of buttons, or else write your own
control to do what the Repeater would do. The Repeater would be created on
ItemCreated, and will bind to data during the first DataBind. After that,
the Repeater will hold the number of items to create in its own ViewState.
On postback, it will create the appropriate number of items, and leave it up
to the controls in each item to load their own ViewState.

Give it a try with a Repeater just to see that the basic concept works, then
you can decide whether a Repeater is good enough for your needs.

John Saunders
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top