tags:

views:

356

answers:

5

I have a commandargument inside an aspx page set to an object variable inside a for loop as below:

<% foreach (PromotionImage p in imageList)
    { 
 %>
     <asp:LinkButton runat="server" OnCommand="deleteButton_Click" ID="deleteButton" CommandArgument="<%# p.ImageId.ToString(); %>" ForeColor="Red"
                            OnClientClick="javascript:return confirm('Are you sure you want to delete this item?');">X</asp:LinkButton>
<%
     }
%>

Then in my c# code behind I have the following to try to get this value:

protected void deleteButton_Click(object sender, CommandEventArgs e)
        {
            int imageId = System.Convert.ToInt32(e.CommandArgument.ToString());
        }

However the c# code keeps returning "System.FormatException: Input string was not in a correct format."

When debugging the e.CommandArgument contains the string "<%# p.ImageId.ToString(); %>" rather than the actual ImageId, why is it not evaluating? Though all my other variables evaluate fine?

+1  A: 

Using <%# %> is used for binding.

Use <%= p.propertyblahblah %> instead.

Furthermore, don't do: commandArgument="..." but use single quotes instead; also the ';' can be dropped at the end of your statement.

-edit: One last thing: just use a repeater for this.

-edit2: Ok. this is truely not going to work unless you're using a repeater or some other kind of databound control. Or you'll have to do this in real code like:

    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            LinkButton l = new LinkButton
            {
                CommandArgument = i.ToString(),
                Text = i.ToString(),
            };
            l.Click += test_onclick;

            holder.Controls.Add(l);
        }
    }
    protected void test_onclick(object sender, EventArgs e)
    {
        var x = ((LinkButton)sender).CommandArgument;   
    }
Jan Jongboom
gives me the same error :( now the e.commandargument variable is populated with "<%= p.ImageId.ToString(); %>" instead of the actual value
David
Drop the ;. Use single quotes. And please use a repeater instead of for-eaching yourself.
Jan Jongboom
i used single quotes and removed the ; semi colon, still same error. I would like to use a repeater but sadly all code we are doing is slowly going to be ported to MVC and use fewer and fewer asp controls, this link button will also change at some point so I'm stuck with only being allowed to use a for loop for now :( still doesn't work
David
Surely using a repeater is better than embedding logic in your aspx file?
Jason Berkan
With MVC you can't even use the linkbutton. So drop that loop, and just use a repeater as long as you're not actually using MVC.
Jan Jongboom
A: 

have you tried it like "<%= p.ImageId.ToString(); %>"

= rather then #

i think # is used to databindings, like inside gridviews.

hope it helps

TeKapa
gives me the same error :( now the e.commandargument variable is populated with "<%= p.ImageId.ToString(); %>" instead of the actual value
David
A: 

I have absolutely no classic ASP experience, but from my testing, it looks like you cannot evaluate your C# code inside a control that is set to run at the server. For instance, the following works:

<%
    For x As Integer = 1 To 10
%>
    <p><%=x%></p>
<%
    Next
%>

while this does not work:

<%
    For x As Integer = 1 To 10
%>
    <p runat="server"><%=x%></p>
<%
    Next
%>

So, inside your asp:LinkButton control, the <% %> is not being executed - it is just being treated as a string, since it is wrapped in quotes.

Jason Berkan
Sorry didn't mean to downvote you.
Jan Jongboom
+1  A: 

What you are trying to do won't work. Some things you can do as alternatives are.

  1. Replace the for loop with a repeater and then use databinding to set the properties. In a comment you mentioned that you are going to be moving to MVC. I believe the Repeater control is still supported and you will need to replace the LinkButton anyway.

  2. If you have to use the for loop because you are moving to MVC, then you will need to write out your own hyperlink tag. Something like this:

    <a onclick="javascript:return confirm('Are you sure you want to delete this item?');" href="<%=Page.GetPostBackClientHyperlink(this, p) %>" style="color:Red;">X</a>

    And then you will need to add a RaisePostBackEvent method to the Page class:

    public void RaisePostBackEvent(string eventArgument)
    {
    ...
    }

    When you convert this page to MVC then replace the hyperlink with a Html.ActionLink or Url.Action call and pull the RaisePostBackEvent logic into your controller.

Mike J
A: 

Is there any chance that the problem is with the CommandEventArgs? I have always just used regular EventArgs, but I parse the sender back into a LinkButton is reference it's CommandArgument directly.

protected void deleteButton_Click(object sender, EventArgs e)
    {
        LinkButton theSender = (LinkButton)sender;
        int imageId = System.Convert.ToInt32(theSender.CommandArgument.ToString());
    }
Justin C