ASP.NET单引号将被转换为&放大器;#39; [英] ASP.NET single quotes are converted to '

查看:645
本文介绍了ASP.NET单引号将被转换为&放大器;#39;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:最有可能,这将是一个双重的问题,但因为我还没有找到一个明确的答案,我要求也无妨。

Note: Most probably this will be a double question, but since I haven't found a clear answer, I'm asking it anyway.

在ASP.NET我想一些JavaScript添加到CheckBox的onclick事件。我已经简化的情况下,以这样的:

In ASP.NET I'd like to add some JavaScript to the onclick event of a CheckBox. I've simplified the case to this:

<asp:CheckBox ID="TestCheckBox" runat="server" onclick="alert('test');" Text="Test" />

将所得的HTML如下:

The resulting HTML is as follows:

<input id="MainContainer_TestCheckBox" type="checkbox" name="ctl00$MainContainer$TestCheckBox" onclick="alert(&#39;test&#39;);" /><label for="MainContainer_TestCheckBox">Test</label> 

什么特别困扰我的是一个单引号'自动'被转换成'和;#39;。如果我忽略的onclick的标记,并在Page_Load中分配给它,完全相同的结果显示在HTML

What particularly bothers me is that a single quote 'automatically' gets converted into '&#39;'. If I omit the onclick in the markup and assign it in Page_Load, the exact same results show in the HTML.

protected void Page_Load(object sender, EventArgs e)
{
    this.TestCheckBox.Attributes["onclick"] = "alert('test');";
}

任何人有一个关于发生了什么玄机?或如何解决/避免呢?

Anyone got a clue about what's happening? Or how to fix/ avoid it?

推荐答案

首先asp的复选框控件不拿的onclick为有效的属性。

First of all the asp check box control doesn't take onclick as a valid attribute.

所以,你可以做两件事情:

So you can do two things:

1如果你不需要值的服务器端,你可以把一个正常的复选框,而不是ASP复选框。

1- If you don't need the value server-side, you can just put a normal check box instead of the asp check box.

2 - 如果你需要的值服务器端,增加您的复选框=服务器属性,地点和ID,所以你可以在你的code引用它。

2- If you need the value server side, add the runat="server" attribute and place and ID on your check box so you can reference it in your code.

<input type="checkbox" id="chk1" onclick="alert('hello');" runat="server" />

这篇关于ASP.NET单引号将被转换为&放大器;#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆