Saturday, March 31, 2012

Set alternate text to empty string

Hi,

I have found that with both the HtmlImage and the Image that if I set the AlternateText property to string.Empty then the 'alt' tag is omitted from the output img tag.

Is there a way around this? The accessibility checkers demand an alt tag even if its empty. If this is not possible, does anyone know if there's much difference between an lt tag set to empty string and an alt tag set to a single space?

Cheers, WT.

the only real difference between setting an alt tag to " " rather than haveing it string.empty is that if left empty it will take it off the form so that .net does not have to keep up with that tag. It's the same thing if you set it to visible = False it just doen't show up on the form. so ultimately it comes down to speed (an almost imeasurable amount). My suggestion is to leave it with one single space.


Ahh, well actually " " results in a dodgy tool tip being displayed! Adding the 'alt' attribute with 'Control.Attributes.Add()' apears to allow empty string alt tags though.


string.empty return a NULL, and a single space is actually an instance of string. they are totally different.
if you assign string.empty to thebase.Attributes["alt"](as shown below), then that "alt" atrribute is actually set to NULL. so, in the render function, it would not be generated to html code. while for a single space, it's not null but an
reference to an realy object, so, "alt" will get rendered. For information i suggest you to look up the .net "control","htmlcontrol" nd "htmlImg" source code. :)
 
public string Alt{get {string text =base.Attributes["alt"];if (text ==null) {return string.Empty; }return text; }set {base.Attributes["alt"] = HtmlControl.MapStringAttributeToString(value); }}

string.Emptyis an instance of a string. Directly setting Attributes["alt"] to string.Empty results in an empty alt tag instead of no alt tag. It must be 'HtmlControl.MapStringAttributeToString' that's returning null when passed string.Empty.


agree. 
internalstaticstringMapStringAttributeToString(string s){if ((s !=null) && (s.Length ==0)) {returnnull; }returns;}
string.Empty is actually an object of string type.

publicstaticreadonlystringEmpty;

the code above is copied fromReflector.Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information.

0 comments:

Post a Comment