我有一个函数为我生成一个javascript代码。
<img src="abc.png" alt="hello" onclick="<%: getOnCilckJS(arg) %>" />c#代码
protected String getOnCilckJS(int arg)
{
if (arg==1)
{
return "alert('hello world');";
}
else
{ return "alert('hello universe');";
}
}除了加载页面时,所有操作都很好,asp.net将单个引号'转换为编码的html字符串(')。
如何避免这种情况并使'出现在html中?
发布于 2013-10-18 08:52:13
你的代码对我有用:
但是只需将<%:更改为<%= (我自己发送参数)。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xxxx.aspx.cs" Inherits="SampleAngularApp.xxxx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
**<img src="abc.png" alt="hello" onclick="<%= getOnCilckJS(11) %>" />**
</div>
</form>
</body>
</html>服务器端
**protected string getOnCilckJS(int arg)**
{
if (arg == 1)
{
return "alert('hello world');";
}
else
{
return "alert('hello universe');";
}
}我收到的警告信息没有任何单引号
发布于 2013-10-18 08:45:22
您使用的是<%: %>,它实际上是编码值。你在找<%= %>。
发布于 2013-10-18 08:46:26
您的应用程序是Web窗体还是MVC?
如果是MVC,您可以尝试Html.Raw(.)函数,如果是Web,则可以检查此链接。
https://stackoverflow.com/questions/19445196
复制相似问题