The CommandArgument property for an ASP.Net LinkButton object ignores <% = ..%>

I'm trying to do what I thought was a very simple operation to set a property in an ASP.Net LinkButton control, but for some reason ASP.Net ignores the tags and just passes the value as a string.

<asp:LinkButton id="viewDetails" runat="server" Text="Details" OnClick="btnDetails_Click" CommandName="ItemID" CommandArgument="<%= item.ItemID %>" />

When a link is clicked, I process it with:

   protected void btnDetails_Click(object sender, EventArgs e)
   {
       try
       {
           LinkButton btn = (LinkButton)sender;
           if (btn.CommandName == "ItemID")
           {
               string itemID = btn.CommandArgument.ToString();               
           }
       }
       catch (Exception excp)
       {
           lblError.ForeColor = System.Drawing.Color.Red;
           lblError.Text = excp.Message;
       }
   }

The problem is that the ItemID ends with the value "<% = item.ItemID%>".

I saw other people face the same problem and try things like below, but so far no one has worked for me.

<asp:LinkButton id="viewDetails" runat="server" Text="Details" OnClick="btnDetails_Click" CommandName="ItemID" CommandArgument=<%= item.ItemID %> />

<asp:LinkButton id="viewDetails" runat="server" Text="Details" OnClick="btnDetails_Click" CommandName="ItemID" CommandArgument="<%# item.ItemID %>" />
+3
source share
3 answers

try it

<asp:LinkButton id="viewDetails" runat="server" Text="Details" OnClick="btnDetails_Click" CommandName="ItemID" CommandArgument='<%= item.ItemID %>' />

Note the single 'in CommandArgument

+2

:

<asp:LinkButton id="viewDetails" runat="server" Text="Details" OnClick="btnDetails_Click" CommandName="ItemID" CommandArgument="<%# item.ItemID %>" />

.DataBind()? kb

+1

Refrences

<%: item.ItemID %>

ASP.NET 4.0:

<%:% > HTML ( , <% =% > ). HTML-, IHtmlString (.. -, , , HTML). , <% =% > (. http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx)

, .

0

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


All Articles