473,396 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Datagrid ItemCreated runs too many times.

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?
Nov 19 '05 #1
9 2311
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).

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 14 Dec 2004 14:47:36 -0500, "Manuel" <My@NoMailLand.com>
wrote:
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?


Nov 19 '05 #2
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.

Nov 19 '05 #3
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
Manuel wrote:
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?


Nov 19 '05 #4
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.
"ujjc001" <uj*****@charter.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Manuel wrote:
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?

Nov 19 '05 #5
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Wed, 15 Dec 2004 09:51:09 -0500, "Manuel" <My@NoMailLand.com>
wrote:
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.
"ujjc001" <uj*****@charter.net> wrote in message
news:11**********************@z14g2000cwz.googleg roups.com...
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
Manuel wrote:
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?


Nov 19 '05 #6
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

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:5q********************************@4ax.com...
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.

Nov 19 '05 #7
"Manuel" <My@NoMailLand.com> wrote in message
news:tN********************@giganews.com...
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
Nov 19 '05 #8
>> 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?


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 :(
Nov 19 '05 #9
"Manuel" <My@NoMailLand.com> wrote in message
news:A_********************@giganews.com...
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?


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


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
Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stevie_mac | last post by:
I'm probably missing something real easy here but... How do i set Tooltips to the header cells in a datagrid? Detail... Datagrid is databound, using non template named columns with 'Create...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
1
by: Jim Heavey | last post by:
Hello I have a datagrid that has a footer that I have place a dropdownbox on and loaded the values in the datalist in the ItemCreated Event. Everything is just as I expected when the grid is...
2
by: Jason | last post by:
I have a datagrid inside a user control. I use the itemcreated event to manipulate column values. I then add those values up and place the total in the footer of the datagrid. I have wrapped the...
2
by: Binny | last post by:
I have a datagrid with customized pager in header and footer. Is there a way to find during item created whether its for top or bottom of the Grid. thanks
2
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for...
3
by: Lars Netzel | last post by:
(applies to Windows Form .NET 2003) I'm filling a datagrid from a Datatable and applying a DataGridStyle. The Source Fields are "Name", "Value", "Locked" and the Style's Columns are "Name",...
2
by: DC | last post by:
Hi, I am doing something like this in the ItemCreated event (ASP.Net 1.1): DataGridItem pagerRow = e.Item; TableCell pagerCell = pagerRow.Cells; Control addedControl = new Control();...
2
by: archana | last post by:
Hi all, I am having one datagrid whose datasource i set as array. On itemcreated event i am having one counter which i have used to find out how many times item created event is fired. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.