OnItemDataBound

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Mar 16, 2007.

  1. Guest

    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 -
    SL.NO., PRODUCT ID, PRODUCT NAME, DESCRIPTION, PRICE, QTY (each row
    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
    CType(ea.Item.FindControl("imgProductName"),
    HtmlImage).Visible = False
    CType(ea.Item.FindControl("imgDescription"),
    HtmlImage).Visible = False
    CType(ea.Item.FindControl("imgPrice"), HtmlImage).Visible =
    False
    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
    case)..

    Now shouldn't the ItemDataBound event fire when the user comes back to
    the 1st DataGrid from the 2nd DataGrid?
    , Mar 16, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio D'Ottavio
    Replies:
    1
    Views:
    2,704
    Antonio D'Ottavio
    Sep 19, 2005
  2. Steven
    Replies:
    1
    Views:
    1,636
    Simon
    Jul 9, 2003
  3. Maureen Black

    OnItemDataBound issue...

    Maureen Black, Apr 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    371
    Maureen Black
    Apr 4, 2004
  4. Replies:
    0
    Views:
    876
  5. Anders
    Replies:
    4
    Views:
    496
    Greg Burns
    Jul 19, 2004
Loading...

Share This Page