CommandArgument does not contain index when under Templatefield??

J

jobs

CommandArgument does not contain row index when command under
Templatefield of- Gridview

It appears CommandArgument passed to the rowcommand event does not
contain row index when the command is called from a control under a
Templatefield. Why is this not the case when the command is from a
Buttonfield for example. And, if this so, how can I get the index of
the row executing the command. This code works for Buttonfield under
the rowcommand event, but fails for Templatefield buttons under the
same gridview.




Dim grid As GridView = CType(sender, GridView)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = grid.Rows(index)
 
T

Teemu Keiski

ButtonField does this internally, e.g it contains literally a Button which
has CommandArgument set to the index of the row, and when clicked, the
Command event is bubbled up to the GridViewRow and finally to the GridView,
which raises RowCommand as response to this action.

With TemplateField, you take the customization route and framework cannot do
everything automatically. Since you provide the content on the template, you
are responsible for passing what you need to pass. Framework is responsible
for instantisting the correct template (and setting controls in it as child
control of the relevant GridViewRow). Besides, RowCommand couldn't be raised
from TemplateField, unless there is a command-capable controls (any of the
Buttons), so it's something that isn't done automatically.

To get the index, get it from current Row's RowIndex property, for example
in RowCreated event, or you can also access it in databinding syntax when
you refer to Container (which references current GridViewRow).
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top