Commandargument string not evaluated

I have a commandargument inside an aspx page set for an object variable inside a for loop, as shown 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, 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, C # code continues to return "System.FormatException: the input line was not in the correct format."

When the e.CommandArgument debug contains the string "<% # p.ImageId.ToString ();%>" and not the actual ImageId, why doesn't it evaluate? Although all my other variables are well rated?

+3
source share
5 answers

Use is <%# %>used for binding.

Use <%= p.propertyblahblah %>instead.

, : commandArgument = "...", ; ';' .

-edit: : .

-edit2: . , - . , :

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

, , . , .

  • for , . , MVC. , Repeater , LinkButton.

  • for, MVC, . - :

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

    RaisePostBackEvent Page:

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

    MVC, Html.ActionLink Url.Action RaisePostBackEvent .

+1

"<% = p.ImageId.ToString();% > "

=, #

, # , , gridviews.

,

0

ASP, , # , . , :

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

:

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

, asp: LinkButton control, <%% > - , .

0

, CommandEventArgs? EventArgs, LinkButton, CommandArgument.

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

Source: https://habr.com/ru/post/1721401/


All Articles