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 %>" />
source
share