3

I'm trying to do what I thought was a very simple operation to set a property on an ASP.Net LinkButton control but for some reason ASP.Net is ignoring the tags and just passing through the value as a string.

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

When the link is clicked I handle 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 itemID ends up with a value of "<%= item.ItemID %>".

I've seen other people encounter the same issue and try things like the below but none have worked for me so far.

<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 %>" />
sipsorcery
  • 30,273
  • 24
  • 104
  • 155
  • You might want to clarify what "none have worked for me so far" actually means – Onkelborg Oct 19 '10 at 23:51
  • None of the examples shown in the question have resulted in the desired outcode of the CommandArgument holding the value of item.ItemID rather than the string "<%= item.ItemID %>". – sipsorcery Oct 20 '10 at 00:03
  • Even the last example? You get, literally, <%# item.ItemID %>? – Onkelborg Oct 20 '10 at 00:28
  • Yep. I've done some more reading and I think databinding is the way to go and you have pointed me in the right direction. – sipsorcery Oct 20 '10 at 00:37
  • The "<% =%>" construct is equivalent to calling `Response.Write` and is used to display directly to the response stream. It can't be used to set server control properties: instead use databinding ("<%# %>") as suggested in some answers. See http://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx – Joe Jun 13 '13 at 11:10

3 Answers3

2

Try this

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

Note the single ' in the CommandArgument

Jason Jong
  • 4,310
  • 2
  • 25
  • 33
1

This should work:

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

Have you called .DataBind()? See this kb

Onkelborg
  • 3,927
  • 1
  • 19
  • 22
0

You can check on here Refrences

<%: item.ItemID %>

Added with ASP.NET 4.0:

<%: %> is used to output an HTML encoded string (used in the same way as <%= %>). It automatically HTML encodes its input, unless the input is an IHtmlString (i.e. something that says it knows how to create valid HTML). The intention is that this would all but replace <%= %> (see http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx)

It may be help u, let me know for further help.

Mayur Borad
  • 1,295
  • 9
  • 23