when i use the code like this:myCheckBox.Attributes["onfocus"]="this.blur()"
You should be using RegisterStartupScript or RegisterClientScript. See this article by Scott Mitchell:
Injecting Client-Side Script from an ASP.NET Server Control
Darrell Norton, MVP
Darrell Norton's Blog
Please mark this post as answered if it helped you!
but when i use the code:checkbox.attributes['onclick']="alert('test')";
it works well,why?
What are you trying to achieve here? What behaviour are you expecting?
if you use the following codes,
<input type="checkbox" onfocus="onblur()">
you will find when you click the checkbox there is no broken line rectangle around the checkbox,so that the page looks more beautiful:)
but i can't do this in asp.net while it was easily solved in asp early versions
I know what you mean about the "focus" rectangle being ugly.
When testing some checkboxes, I only noticed this rectangle when tabbing through the controls ... clicking on a checkbox or its label would not show this rectangle. Is this what you're trying to stop ... the rectangle appearing when the Tab key is used?
If so, I wouldn't recommend removing it ... as it is important visual feedback for people not using a mouse to work with the page.
You can indeed stop this behaviour (as you noted, it is easy in ASP). However, in ASP.NET it doesn't appear you can set onfocus="this.blur()" on an ASP.NET checkbox control, due simply to the way it is rendered. If you *must* have this onfocus attribute, then I think you'll need to create your own checkbox controls, rather than using the corresponding ASP.NET server control.
thank you ,SomeNewKid2,i didn't think about the users with keyboard:(
it seems i shouldn't use this skill:P