473,699 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.Data GridItemEventAr gs) Handles grd.ItemCreated

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem 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,1 3,14,15,16,17,

Does anyone have an idea of what's happening here?
Nov 19 '05 #1
9 2329
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.Dat aGridItemEventA rgs) Handles grd.ItemCreated

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.A lternatingItem 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,1 3,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.W ebControls.Data GridItemEventAr gs) Handles grd.ItemCreated
If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem 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,1 3,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*****@charte r.net> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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.W ebControls.Data GridItemEventAr gs) Handles

grd.ItemCreated

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem 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,1 3,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*****@charte r.net> wrote in message
news:11******* *************** @z14g2000cwz.go oglegroups.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.W ebControls.Data GridItemEventAr gs) Handles

grd.ItemCreated

If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem 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,1 3,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.c om...
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******** ************@gi ganews.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_******** ************@gi ganews.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
4741
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 columns at Run Time' disabled. PS, i know i can set Tooltips to the cell/rows of the datagrid in the ItemDataBound event - but can't seem to get to the header cells!
3
4920
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 are Web Control & not HTML control. One of these buttons whose title is Delete is added on the aspx page in design view & also I double clicked on this button in design view to get the onclick code for this button in the code behind page. & for...
1
1220
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 presented. I select some value from the DropDownList and then press a command button, also on the footer. By the time I get to the event which handles the button click event (CommandName Property in the datagrid), the
2
1185
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 function used to obtain the data for this datagrid in a page.ispostback=false condition. Everything displays accurately when an event(in the user control) posts the page back to the server, but if a control outside the user control posts the page...
2
1187
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
3998
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 ListItemType.Pager. The problem I'm having is subscribing to the selectedindex changed event. The datagrid doesn't even seem fire an itemcommand, but that's ok, just the selectedindex changed event would do... Any advice is greatly appreciated!
3
3318
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", "Value" Depending on the "Locked" field in the source... I want the "Value" cell to be readonly or not for each row
2
6753
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(); addedControl.ID = "addedControl"; pagerCell.Controls.Add(addedControl);
2
2280
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 i observerd is itemcreated event is fired twice than total items present in array.
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8885
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.