views:

697

answers:

3

I have an ASP.Net page, which displays a list of options to the user. When they select from the list, it does a post back and queries a sql server. The results are displayed in a listview below the options in an update panel. Below is a snippet of the ItemTemplate:

<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />

The DataItemIndex does not appear, so my commandargument is empty. However, the object sender is the button, which shows the item.

Why is the index item not appearing in the CommandArgument?

Could it be the post back? If so, why would it be the post back? Is there a way around it?

Edit: Sorry, from my attempts to solve it before, I posted bad code, but it still isn't appearing.

Resolution: I found another work around in that the sender of the OnCommand is the link button, which has the CommandArgument. I will chalk this issue up to be an issue with multiple postbacks and javascript.

A: 

You're not setting it

You possibly want

<%# Container.DataItemIndex %>

or

<%= Container.DataItemIndex %>

:)

Noon Silk
A: 

Try

<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />

You were missing the "#" sign.

David Stratton
Sorry, I posted bad code. It still isn't appearing.
daub815
+5  A: 

You can't use the <%= %> syntax inside properties on a tag with a runat="server" attribute. I'm surprised the code will even run. :)

UPDATE:

You probably want to use the ItemDataBound event on the repeater, find the linkbutton and set the CommandArgument property.

Not very elegant, but here's a VB.NET sample.

Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Select Case e.Item.ItemType
      Case ListItemType.Item, ListItemType.AlternatingItem
        Dim b As LinkButton = e.Item.FindControl("btn")
        b.CommandArgument = e.Item.ItemIndex
    End Select
  End Sub
Jakob Gade
I changed it to #, but it still won't display it in the CommandArgument. It doesn't solve the original problem.
daub815
I found another workaround, but your solution should work, too.
daub815