I'm writing a custom web server control that inherits from [b]System.Web.UI.WebControls.TextBox[/b]. Now, I know how to add extra attributes to the control by overriding the [b]AddAttributesToRender()[/b] method, but is it possible to do the opposite - that
is, [b]remove[/b] attributes when rendering? Specifically I need to remove the [b]type="text"[/b] attribute and value when rendering the control (for a good reason!). Anyone know a way of doing this? Thanks.
Since the TextBox control's AddAttributesToRender(HtmlTextWriter) method is where the type="text" key/value pair is added to the attribute array of the HtmlTextWriter, your control inheriting from TextBox could provide a
complete override of this method (rather than first calling this the base method and adding extra attributes). I would want to first take a look at the standard implementation of the TextBox control's AddAttributesToRender() method using Lutz Roeder's
.NET Reflector to be sure that I did not leave out any necessary attributes.
As an alternative, you could provide your own HtmlTextWriter class that would override the inherited HtmlTextWriter's OnAttributeRender function to look at each attribute as it is rendered and return FALSE for the Type="Text" key/value pair, thus filtering
out the unwanted attribute.
First, thanks for you help so far everyone, it has given me some ideas.
The reason why I want to remove the [b]type=text[/b] name/value pair is as follows:
One of the WAI guidelines for accessibility requires that "include default, place-holding characters in edit boxes and text areas". In other words, for a password field it must actually have the word "Password" pre-populated within it (I don't make these
rules, but our company is bound to them). However, the major problem is that if you can see the word "Password" then it's not a true password field as input is not "starred out".
What I've been doing is making a custom server control that registers a client-script and adds a javascript "onclick" method to the TextBox control. When the user clicks in the TextBox the default pre-populated text (ie. "Password") is removed and then the
type of the TextBox is changed, via javascript, to "type=password" so it now acts like a real password box. The control TextBoxMode is also changed to "password" for subsequent postbacks.
My problem is that, you can create Attributes via JavaScript DOM but changing them doesn't work (it should, but IE doesn't like it). So, you need to progmatically create attributes. That means I need to add the "type=password" myself in JS. But I don't want
the "type=text" to already be there when I add it, hence need to remove it when rendering the control. My JavaScript is :
I would probably create a composite control that had both a password control and a textbox control. In the client side javascript, I would just toggle which one was visible.
Write and think at the same time :P ... I'm not sure either of our approaches would play nice with a screen reader or other browsers that rely on accesibility information.
(Is there a name/label for these kind of browser clients?)
If you set the value attribute ( myPasswordInput.Attributes["value"] = "password" ) then the password will actualy be present in clear text in the source. This value might be used by a special browser.
If you make a custom class for your designer you can use something like this (my exemple remove 2 properties from an IconButtonDesigner control) but I don't know if this will work for the
type="text" attribute :
Connect
Contributor
2304 Points
455 Posts
Removing Attributes on Rendering WebControl
Dec 08, 2005 04:27 PM|LINK
imagemaker
Contributor
2385 Points
466 Posts
Re: Removing Attributes on Rendering WebControl
Dec 09, 2005 03:49 AM|LINK
Since the TextBox control's AddAttributesToRender(HtmlTextWriter) method is where the type="text" key/value pair is added to the attribute array of the HtmlTextWriter, your control inheriting from TextBox could provide a complete override of this method (rather than first calling this the base method and adding extra attributes). I would want to first take a look at the standard implementation of the TextBox control's AddAttributesToRender() method using Lutz Roeder's .NET Reflector to be sure that I did not leave out any necessary attributes.
As an alternative, you could provide your own HtmlTextWriter class that would override the inherited HtmlTextWriter's OnAttributeRender function to look at each attribute as it is rendered and return FALSE for the Type="Text" key/value pair, thus filtering out the unwanted attribute.
AndrewSeven
Contributor
2184 Points
437 Posts
Re: Removing Attributes on Rendering WebControl
Dec 09, 2005 02:10 PM|LINK
Any chance you want to share the reason? It might change the answer.
I usualy use Reflector, but you can also check the mono source trees to see a different but equivalent implementation.
Connect
Contributor
2304 Points
455 Posts
Re: Removing Attributes on Rendering WebControl
Dec 09, 2005 07:03 PM|LINK
First, thanks for you help so far everyone, it has given me some ideas.
The reason why I want to remove the [b]type=text[/b] name/value pair is as follows:
One of the WAI guidelines for accessibility requires that "include default, place-holding characters in edit boxes and text areas". In other words, for a password field it must actually have the word "Password" pre-populated within it (I don't make these rules, but our company is bound to them). However, the major problem is that if you can see the word "Password" then it's not a true password field as input is not "starred out".
What I've been doing is making a custom server control that registers a client-script and adds a javascript "onclick" method to the TextBox control. When the user clicks in the TextBox the default pre-populated text (ie. "Password") is removed and then the type of the TextBox is changed, via javascript, to "type=password" so it now acts like a real password box. The control TextBoxMode is also changed to "password" for subsequent postbacks.
My problem is that, you can create Attributes via JavaScript DOM but changing them doesn't work (it should, but IE doesn't like it). So, you need to progmatically create attributes. That means I need to add the "type=password" myself in JS. But I don't want the "type=text" to already be there when I add it, hence need to remove it when rendering the control. My JavaScript is :
unction
changeToXPassword(oInput,elemId){
var newEl = document.createElement('input');newEl.setAttribute(
'type', 'password');newEl.setAttribute(
'name', elemId);newEl.setAttribute(
'id', elemId);oInput.parentNode.replaceChild(newEl
,oInput);changeToXPassword.el
= newEl;setTimeout(
'changeToXPassword.el.focus()',100); return true;}
Then I'm rendering my control using:
writer
.AddAttribute("onclick", "changeToXPassword(this, '" + XText + "','" + this.ID + "')");Long story, I know :)
AndrewSeven
Contributor
2184 Points
437 Posts
Re: Removing Attributes on Rendering WebControl
Dec 09, 2005 07:18 PM|LINK
I think I would take a different route.
I would probably create a composite control that had both a password control and a textbox control. In the client side javascript, I would just toggle which one was visible.
Write and think at the same time :P ... I'm not sure either of our approaches would play nice with a screen reader or other browsers that rely on accesibility information.
(Is there a name/label for these kind of browser clients?)
If you set the value attribute ( myPasswordInput.Attributes["value"] = "password" ) then the password will actualy be present in clear text in the source. This value might be used by a special browser.
This is a usefull toolbar that was built by people who are serious about accesibility http://www.visionaustralia.org.au/ais/toolbar/
Pluginbaby
Contributor
2961 Points
486 Posts
MVP
Re: Removing Attributes on Rendering WebControl
Dec 12, 2005 10:37 AM|LINK
If you make a custom class for your designer you can use something like this (my exemple remove 2 properties from an IconButtonDesigner control) but I don't know if this will work for the type="text" attribute :
protected override void PostFilterProperties( _IDictionary Properties )
{
Properties.Remove( "BackgroundImage" );
Properties.Remove( "BackColor" );
}
Laurent Duveau
MVP / MCT / RD
http://weblogs.asp.net/lduveau/