Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > OnItemDataBound

Thread Tools

Posts: n/a
A Web Form has 2 DataGrids. At any given time, only one of the
DataGrids will be visible to a user. Both the DataGrids also have
images (<img src../>) in the headers. The 1st DataGrid has 7 columns -
under this column has a TextBox) & a ADD TO CART link (which is
actually a EditCommandColumn). The first 6 columns are
TemplateColumns. The 2nd DataGrid has the same columns as the 1st one
but there are 2 additional columns - a Template Column TOTAL & a
ButtonColumn.DELETE. The Text of EditCommandColumn changes from ADD TO
CART to EDIT when the 2nd DataGrid is visible.

Note that in the 1st DataGrid, the headers of only the PRODUCT ID,
PRODUCT NAME, DESCRIPTION & PRICE columns are accompanied by images.
In the 2nd DataGrid, apart from the columns mentioned above, the
headers of the QTY & TOTAL columns are accompanied by images.

As you all must have guessed by now, this is a shopping cart app. When
the page loads for the first time, the 1st DataGrid displays all the
products from a database. When a product is added to the cart, the 1st
DataGrid gets hidden & the 2nd DataGrid becomes visible. The 2nd
DataGrid shows the details of the products the user has in his
shopping cart. When the 2nd DataGrid is visible, a LinkButton also
gets visible clicking which the 1st DataGrid becomes visible (so that
users can see all the items & add more items to his cart) & the 2nd
DataGrid & the just clicked LinkButton get hidden.

I am populating & binding the 1st DataGrid only when a user comes to
this page for the very first time. On other occasions, whenever the
1st DataGrid is visible, I am relying on the ViewState to display it.

The images accompanying the headers (which are actually LinkButtons)
change dpending upon how a column gets sorted. When a header is
clicked, I am using the ItemDataBound event of the DataGrid to change
the images. Both the DataGrids are sortable & execute the same
ItemDataBound event function.

If a user sorts the PRICE column ascendingly in the 2nd DataGrid &
then goes back to the 1st DataGrid, then the 1st DataGrid
automatically gets sorted by the PRICE column ascendlngly & vice-versa
(since it maintains the ViewState). I am saving the sort column & the
sort order in a Session variable.

Assume that a user is currently viewing the 1st DataGrid. He sorts the
PRODUCT NAME column descendlngly. Next he adds a product to his cart.
Now the 2nd DataGrid is visible.which is also sorted by the PRODUCT
NAME column descendingly. Next the user sorts the 2nd DataGrid by the
TOTAL column ascendingly. Then he goes back to the 1st DataGrid. Under
such circumstances, what I find is since there is no column named
TOTAL in the 1st DataGrid (which was sorted ascendingly in the 2nd
DataGrid), the 1st DataGrid remains sorted by the column which the
user had sorted just before coming to the 2nd DataGrid (which is
PRODUCT NAME descendingly in this case).

But I want that under such circumstances, the 1st DataGrid should be
sorted in the default manner (how it was sorted when the user visited
the page for the very first time). To do this, I added the following
code in the ItemDataBound event of the DataGrid:

If (DataGrid1.Visible = True) Then
If (InStr(Session("Sort"), "Total") > 0) Then
CType(ea.Item.FindControl("imgProductID"), HtmlImage).Visible
= False
HtmlImage).Visible = False
HtmlImage).Visible = False
CType(ea.Item.FindControl("imgPrice"), HtmlImage).Visible =
End If
End If

But what I find is when I click the LinkButton (when the 2nd DataGrid
is visible) to come back to the 1st DataGrid, the ItemDataBound event
doesn't fire! Hence the above code snippet in the ItemDataBound event
doesn't get executed due to which the 1st DataGrid remains sorted by
the same column in the same order as how it was sorted before the user
came to the 2nd DataGrid (which is PRODUCT NAME descendingly in this

Now shouldn't the ItemDataBound event fire when the user comes back to
the 1st DataGrid from the 2nd DataGrid?

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem assigning selectedItem.Value to a dropdownlist filled with OnItemDataBound Antonio D'Ottavio ASP .Net 1 09-19-2005 09:41 AM
What is most efficient? OnItemDataBound or using a function Anders ASP .Net 4 07-19-2004 11:29 AM
Re: How to access DataSource rows onItemDatabound? ASP .Net 0 07-08-2004 04:27 PM
OnItemDataBound issue... Maureen Black ASP .Net 0 04-04-2004 05:36 AM
DataGrid OnItemDataBound problem in APS.NET C# Steven ASP .Net 1 07-09-2003 05:17 PM