Why is my CommandArgument empty?

I have an ASP.Net page that displays a list of options for the user. When they select from the list, it returns a message and queries the sql server. The results are displayed in the list below the options in the update panel. The following is an ItemTemplate snippet:

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

DataItemIndex does not appear, so my command value is empty. However, the sender of the object is the button in which the item is displayed.

Why is the index element not showing in CommandArgument?

Maybe this message? If so, why will it be a post? Is there any way around this?

Edit: Due to my attempts to resolve this earlier, I posted the code, but it still does not appear.

Resolution: I found another job in that the OnCommand sender is a link button that has a CommandArgument. I will flag this issue to be a problem with multiple postbacks and javascript.

+6
listview commandargument
source share
5 answers

You cannot use the syntax <%= %> inside tag properties with the runat="server" attribute. I am surprised that the code will even run. :)

UPDATE:

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

Not very elegant, but here is an example of VB.NET.

 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 
+7
source share

You do not install it

You might want

 <%# Container.DataItemIndex %> 

or

 <%= Container.DataItemIndex %> 

:)

0
source share

Try

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

You missed the "#" sign.

0
source share

This site really helped me with this problem: http://forums.asp.net/t/1671316.aspx

The problem I ran into was that they passed me null arguments to the commandargument when I pressed the button a second time. As explained above, this is because the commandargument is set only in the data binding event. So, to fix this, enable the data binding event in the page_load substrate

Ex. (Vb)

 Private Sub BindSelectButtons() 'Purpose: bind the data to the select buttons for commandargument to be used Dim i As Integer For i = 0 To gridview1.Rows.Count - 1 gridview1.Rows(i).Cells(8).FindControl("btnID").DataBind() Next End Sub Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 'Rebind select buttons so that the commandargument refreshes BindSelectButtons() End Sub 
0
source share

Make sure the view mode is on. e.Row.EnableViewState = true;

0
source share

All Articles