Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net LinkButton CommandArgument property ignores <%= .. %>

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 %>" />
like image 664
sipsorcery Avatar asked Aug 20 '26 05:08

sipsorcery


2 Answers

Try this

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

Note the single ' in the CommandArgument

like image 123
Jason Jong Avatar answered Aug 23 '26 00:08

Jason Jong


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

like image 34
Onkelborg Avatar answered Aug 23 '26 00:08

Onkelborg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!