ItemIndex is -1 in ItemCommand after selecting page in DataGrid

A

Alan Eilers

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
(e-mail address removed)
 
S

Scott Mitchell [MVP]

Alan said:
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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top