Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my CommandArgument Empty?

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.

like image 909
kevindaub Avatar asked Sep 05 '09 00:09

kevindaub


1 Answers

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
        b.DataBind()
    End Select
  End Sub
like image 94
Jakob Gade Avatar answered Sep 22 '22 12:09

Jakob Gade