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

谁都会走 提交于 2019-12-01 06:02:40

问题


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

回答1:


Try this

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

Note the single ' in the CommandArgument




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3973723/asp-net-linkbutton-commandargument-property-ignores

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!