我在 asp.net 4 中有一个问题。
当我在控件上添加一个属性时,然后对其进行编码。
例如,当我键入此代码时
txtQuestion.Attributes["onfocus"] =
"if(this.value == this.title)
{
this.value = '';
this.style.backgroundColor='#FEFDE0';
this.style.color='#000000';
}";
我得到渲染
onfocus="if(this.value == this.title){this.value =
'';this.style.backgroundColor='#FEFDE0';
this.style.color='#000000';}"
并且每个'哈希都更改为& #39;
有没有办法只在某些控件上禁用这个新的未来?还是制作自定义渲染的简单方法?
我的失败尝试
我已经准备好尝试一些想法,但我失败了。例如,这失败了。
txtQuestion.RenderingCompatibility = new Version("3.5");
我还找到了此属性呈现并打开的点
公共虚拟 void RenderBeginTag(HtmlTextWriterTag tagKey)函数,
如果他希望被编码,每个属性都有一个标志,但我不知道任何人如何设置它。
一项工作
在同一个问题的 asp net 论坛中,有一个解决方案可以更改全局 EncodeType - 这不是我要搜索的解决方案 - 提供解决方案的人说这不是一个很好的解决方法,存在潜在的安全问题或其他渲染问题。
先谢谢大家了。
通过凯文
直到现在,Kervin 才发现微软建议改用这个命令。
txtQuestion.Attributes["onfocus"] =
"if(this.value == this.title){this.value = '';this.style.backgroundColor='#FEFDE0';this.style.color='#000000';}";
使用这个。
Page.ClientScript.RegisterExpandoAttribute(txtQuestion.ClientID, "onfocus",
"if(this.value == this.title){this.value = '';this.style.backgroundColor='#FEFDE0';this.style.color='#000000';}");
而 MS 呈现的是页面末尾,一个使用 JavaScript 将 onfocus 添加到此控件的脚本。我们甚至可以自己使用 jQuery 来做到这一点,而且可能更兼容。
这是一个解决方案,但我仍然想知道是否有一种方法可以避免属性编码,让我按照自己的方式做- 而不是 MS 方式。