控制属性在 dot net 4 上呈现编码 - 如何禁用编码? [英] Control Attributes render Encoded on dot net 4 - how to disable the encoding?

查看:22
本文介绍了控制属性在 dot net 4 上呈现编码 - 如何禁用编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 asp.net 4 中遇到问题.

I have an issue in asp.net 4.

当我在控件上添加一个属性时,然后渲染它编码.

例如,当我输入此代码时

For example, when I type this code

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';}"

并且每个 ' 哈希值都更改为 '

And every ' hash been change to & #39;

有没有办法仅在某些控件上禁用这个新的未来?或制作自定义渲染的简单方法?

Is there a way to disable this new future only on some controls ? or an easy way to make a custom render ?

我已经准备好尝试一些想法,但我失败了.例如,这失败了.

I have all ready try some thinks but I fail. For example this fails.

txtQuestion.RenderingCompatibility = new Version("3.5");

我也找到了这个属性渲染的点

I also locate the point that this attributes renders and is on

public virtual void RenderBeginTag(HtmlTextWriterTag tagKey)函数,

如果他希望被编码,每个属性都有一个标志,但我不知道如何设置它.

there every attribute have a flag if he wish to be encoded, but I do not know how can anyone set it or not.

在asp net论坛的同一个问题中,有一个更改全局 EncodeType 的解决方案 - 这不是我搜索的解决方案 - 提供解决方案的人说这不是一个很好的解决方法,存在潜在的安全问题或其他渲染问题.

In the asp net forum in the same question, there is a solution that change the global EncodeType - this is not the solution that I search for - and the person that give the solution say that this is not a great workaround, with potential security issues or other render issues.

先谢谢大家.

直到现在 Kervin 发现微软建议改用这个命令.

Until now Kervin found that Microsoft suggest instead use this command.

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 在此控件上添加了焦点.我们甚至可以自己使用 jQuery 来做到这一点,并且可能更兼容.

And what MS render, is on the end of the page, a script that add onfocus on this control using JavaScript. We can do that even by our self with jQuery and probably be more compatible.

这是一个解决方案,但我仍然想知道是否有办法避免属性编码并让我按照自己的方式做事 - 而不是 MS 方式.

This is a solution, but still I am wish to know if there is a way to just avoid the Attribute Encoding and let me do what I wish my way - not MS way.

推荐答案

来自 MSDN WebControl.Attributes 属性 文档...

From MSDN WebControl.Attributes Property Documentation...

注意

您不能使用 Attributes 集合将客户端脚本添加到 WebControl 实例.要添加客户端脚本,请使用 Page 控件上的 ClientScript 属性.

You cannot add client-side script to a WebControl instance using the Attributes collection. To add client-side script, use the ClientScript property on the Page control.

问题在于 Attributes 需要在代码隐藏中设置的数据.

The problem is that Attributes expects data if it's being set in the code-behind.

解决方案是发回 客户端脚本与您的客户端处理程序函数一起使用,然后您可以使用您的函数名称设置属性.

The solution is to send back a client script with your client side handler functions then you may set the attribute with the name of your functions.

如果您的 javascript 是静态的,那么事情就更简单了,因为您可以在注册控件之前很久将它们发送到 script 标记中.

If your javascript is static, then things are even simpler, since you can send them in a script tag long before the controls are registered.

这篇关于控制属性在 dot net 4 上呈现编码 - 如何禁用编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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