views:

240

answers:

2

I have an issue in asp.net 4.

When I add an attribute on controls, then the render it encoded.

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

I get render

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 ?

My Fail tries

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) function,

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

One Work Around

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.

Thank you all in advanced.

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

Use this one.

    Page.ClientScript.RegisterExpandoAttribute(txtQuestion.ClientID, "onfocus", 
 "if(this.value == this.title){this.value = '';this.style.backgroundColor='#FEFDE0';this.style.color='#000000';}");

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.

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.

+1  A: 

Have you tried using this new ASP.NET 4 feature to make a custom encoder?

bwarner
Yes, custom encoding is made for all controls on full site, I wont only for 2-3 controls only, not for every one on the site.
Aristos
The solution you say http://forums.asp.net/p/1554455/3818865.aspx, not the way I look for.
Aristos
There isn't a way to be able to pass unencoded single quotes without introducing a security issue. So either you don't care about security, and you write your own encoder that allows single quotes, or you do care about security, and then you have to find a different way to set the onFocus code for your control.
bwarner
@bwarner there are many ways to do that - open your mind ! there are always more roads.
Aristos
+1  A: 

From MSDN WebControl.Attributes Property Documentation...

Note

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.

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.

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.

kervin
@kervin +1 Thank you for your answer, this is an alternative way that working - not the one I like, but its a solution, I will update the question with this solution. Still search for something that just not encode attributes only on some controls - not on all.
Aristos