ItemIndex is -1 in ItemCommand after selecting page in DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Alan Eilers, May 12, 2004.

  1. Alan Eilers

    Alan Eilers Guest

    I have set up a DataGrid with default paging. The DG ItemCommand
    works fine when user selects row by clicking on ButtonColumn (which
    fills in text boxes at bottom of page). ItemIndex properly shows 0
    when first row selected, 1 for second, etc. But when user clicks on
    hyperlink for new page at bottom of DG, ItemIndex shows as -1 in
    ItemCommand handler! If I don't trap it first I get an "index out of
    range" error there when trying to access the values from the selected
    line.

    Questions: (1) Why does the selection of a page hyperlink fire
    ItemCommand event before it fires PageIndexChanged? (2) Why doesn't
    the ItemIndex default to 0 in ItemCommand instead of -1 (which I
    specified in the SelectedIndex property of the DG)? (3) Why is it
    important to establish any kind of row position in ItemCommand anyway
    first, since my PageIndexChanged event happens next and goes to a new
    page just fine?

    Just some idiosyncracies of the DataGrid that make it a challenge for
    newbies like me to learn. Any insights would be appreciated.

    Alan Eilers
     
    Alan Eilers, May 12, 2004
    #1
    1. Advertising

  2. Alan Eilers wrote:
    > Questions: (1) Why does the selection of a page hyperlink fire
    > ItemCommand event before it fires PageIndexChanged?


    Alan, ANY command button in the DataGrid will ALWAYS cause the
    DataGrid's ItemCommand event to fire. Just how the code's written.

    > (2) Why doesn't
    > the ItemIndex default to 0 in ItemCommand instead of -1


    Again, just how the code's written. The DataGrid's Items collection
    represents the set of *databound* rows (not
    headers/footers/pagers/etc.). So the ItemIndex property is really only
    applicable for those databound rows, and not for the header, footer,
    pager, etc.

    > (3) Why is it
    > important to establish any kind of row position in ItemCommand anyway
    > first, since my PageIndexChanged event happens next and goes to a new
    > page just fine?


    The ItemCommand event is not important for paging. In any event, your
    ItemCommand event should look something like:

    Sub ItemCommandEventHandler(...)
    If e.CommandName = "..." then
    ...
    ElseIf e.CommandName = "...something else..." then
    ...
    ElseIf ...
    ...
    End If
    End Sub


    That is, you should only run code in ItemCommand for a specific button
    with a specific CommandName property set. This way you don't "step on
    the toes" of your pager, or Edit/Update/Delete buttons.

    I discuss this in more detail in my book on DataGrids, ASP.NET Data Web
    Controls Kick Start
    [http://www.amazon.com/exec/obidos/ASIN/0672325012/4guysfromrollaco].

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], May 13, 2004
    #2
    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. DataGrid change ItemIndex

    , Jan 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    492
    S. Justin Gengo
    Jan 6, 2006
  2. Rod
    Replies:
    2
    Views:
    861
  3. susie
    Replies:
    0
    Views:
    150
    susie
    Sep 29, 2003
  4. Jose Fernandez

    DATASET DATAGRID ITEMINDEX DATASETINDEX

    Jose Fernandez, May 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    281
    Jose Fernandez
    Jun 1, 2005
  5. janetb

    datagrid itemIndex cell reference

    janetb, May 10, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    172
    janetb
    May 10, 2006
Loading...

Share This Page