4

I am trying to pass commandarguments like

<asp:Button ID="btnSave" runat="server" Text="Save" CommandName='<%# Eval("Section_Name")%>' CommandArgument='<%# Container.DataItemIndex %>' />

but I get this error:

'System.Web.UI.Control' does not contain a definition for 'DataItemIndex' and no extension method 'DataItemIndex' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)

What is the correct method to pass the commandarguments? This button is inside and updatepanel in the itemtemplate of a listview.

Thanks, Ali

4 Answers4

8

Thanks a lot DavidGouge and Jason Berkan. I made it working using

CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>'

However, I think both of the suggestions should also work.

6

If you're using Repeater, you can use "Container.ItemIndex" to retrieve the item index.

Mayur Bhavsar
  • 61
  • 1
  • 2
3

It's because the button is inside an update panel which is the "Container" and you're trying to get the DataItemIndex of that UpdatePanel which obviously doesn't exist.

Could you pass the "Id" of the item you'll be saving to the CommandArgument directly with Eval("WhateverId") ?

EDIT: If you really do need the DataItemIndex, this will get it for you:

<%# ((ListViewDataItem)Container).DataItemIndex %>
DavidGouge
  • 4,583
  • 6
  • 34
  • 46
2

In this situation, I've found it easiest to set the CommandArgument in the code behind. In the ItemCreated event:

Dim btnSave As Button = e.Item.FindControl("btnSave")
btnSave.CommandArgument = e.Item.DataItemIndex
Jason Berkan
  • 8,734
  • 7
  • 29
  • 39