473,748 Members | 4,951 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 2331
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
4746
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
4927
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
1223
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
1189
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
1189
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
4000
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
3322
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
6758
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
2284
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
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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
9367
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...
0
9243
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
8241
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
6795
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...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2213
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.