Asp.net listview Container.DataItemIndex

I am trying to pass commandarguments as

<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 the extension method 'DataItemIndex' cannot be found that accepts the first argument of the type 'System.Web.UI.Control' (you do not use the directive or assembly reference ?)

What is the correct method for communicating team goals? This button is located inside and is updated on the list item panel.

Thanks Ali

+4
source share
4 answers

Many thanks to DavidGouge and Jason Birkan. I did it using

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

However, I think both suggestions should also work.

+8
source

If you use Repeater, you can use "Container.ItemIndex" to retrieve the index of the item.

+6
source

This is because the button is located inside the update panel, which is the "Container", and you are trying to get the DataItemIndex of this UpdatePanel, which obviously does not exist.

Could you pass the "Id" of the element that you will save to the CommandArgument directly with Eval ("WhateverId")?

EDIT: if you really need a DataItemIndex, this will get it for you:

 <%# ((ListViewDataItem)Container).DataItemIndex %> 
+3
source

In this situation, I found that the easiest way to set CommandArgument in code. In the ItemCreated event:

 Dim btnSave As Button = e.Item.FindControl("btnSave") btnSave.CommandArgument = e.Item.DataItemIndex 
+2
source

All Articles