开发者

commandargument string not evaluating

I have a开发者_C百科 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?


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;   
    }


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.


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

= rather then #

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

hope it helps


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.


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());
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜