views:

1788

answers:

3

Hi all, 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

A: 

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
yes i can pass the id , but not the dataitemindex
Ali
I would use that then as you key to what you are saving.
DavidGouge
No but my situation requires the use of the index of the item rather than ID. How can I get that in an update panel?
Ali
any other ideas? anybody?
Ali
You could try Container.Container.DataItemIndex
DavidGouge
no. that didnt help. Same error :(
Ali
No, scratch that, that doesn't work either. Just running an example, I'll post back if anything comes up.
DavidGouge
Ok, give this a try:<%# ((ListViewDataItem)Container).DataItemIndex %>
DavidGouge
+1  A: 

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
TBH, I also tend to fall back to code behind once the binding markup starts to get too complicated.
DavidGouge
+1  A: 

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.

Ali
No problem, glad you got it working.
DavidGouge