inject extra code into all controls on a page

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I want to implement a simple form element focus script that will change the
color of a form element when it gets focus and revert back when it loses
focus.

I've got the code working (simple) but now I need to add it to ALL my form
controls and there are tons. Is there a way to override page rendering and
insert the javascript into the rendered page source?

For example, this is what I would like to add to my controls:
onfocus="hilightActiveElement(this);

So the whole control looks like:
<asp:TextBox ID="TextBox_PatientAddress2" runat="server" TabIndex="6"
Width="223px" onfocus="hilightActiveElement(this);" />

I'm using a function now so that as I tweak the look and feel I don't need
to update it too many places, but if I was going to inject it at render time
I would just use: onfocus="this.style.background = ''";
onblur="this.style.background = '';"

Any ideas welcome. Thanks!
Steve
 
There is no magic way to do it, but I have a couple of directions you can
go.

1. Loop through controls on load. This requires little thought.
2. Create your own controls that inherit from the controls in .NET and have
the bits added you need. This is a bit better as the control automagically
picks up the behavior. You can even have the first control on the page emit
the JavaScript and have others know the script is out there (with a bit more
work).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
Back
Top