Tag Archives: html

HTMLWriter 1.7

Just a quick refresh on my HTMLWriter utility library. Some minor changes/additions:

1. Added support for the NOBR tag

2. Improved the special character functions including ignoring the ‘&’ sign when needed.

Latest version here.

HTMLWriter 1.5

I’ve updated this little utility library by simplifying some method overloads plus added a static method for getting escaped html special characters.

To make the code more maintainable for the special characters (like adding new ones in the future) I’ve added an enum that use some .Net ‘attributes’ so you can see, use and reference everything about these ‘things’ in one place. To do that I had to add a special helper class so the enum can carry some additional properties (like the actual strings for the special character and the escaped string). To do this I created a custom attribute class called ‘StringValueAttribute’ like this:

public class StringValueAttribute : Attribute
{

public string StringExpandedValue;
public string StringValue;

public StringValueAttribute(string value, string stringExpandedValue)
{

this.StringValue = value;
this.StringExpandedValue = stringExpandedValue;

}

}

To retrieve the associated values from an enum another static helper class is needed: StringValueAttributeUtil

internal static class StringValueAttributeUtil
{

public static string GetStringValue(Enum value)
{

// Get the type
Type type = value.GetType();

// Get fieldinfo for this type
System.Reflection.FieldInfo fieldInfo = type.GetField(value.ToString());
// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
// Return the first if there was a match.
return attribs.Length > 0 ? attribs[0].StringValue : null;

}
public static string GetStringExpandedValue(Enum value)
{

// Get the type
Type type = value.GetType();

// Get fieldinfo for this type
System.Reflection.FieldInfo fieldInfo = type.GetField(value.ToString());
// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
// Return the first if there was a match.
return attribs.Length > 0 ? attribs[0].StringExpandedValue : null;

}

}

 

Then to actually make use of this helper class you decorate the enum like this: (showing only part of it…)

public enum HTMLSpecialCharacter
{

[StringValue(“<“, “&amp;lt;”)]
LessThanSign,
[StringValue(“>”, “&amp;gt;”)]
GreaterThanSign,
[StringValue(“\””, “&amp;quot;”)]
DoubleQuote,
[StringValue(“‘”, “&amp;#39;”)]
SingleQuote,

}

Happy HTML’ing 🙂

Update: Version 1.6 has now been released since…

Hiding parts of html page on printing

If you need to create a web page on which some parts should not be visible when it is printed (or even print-previewed) you can use the following CSS:

@media print
{

.notPrint
{

display:none;

}

}

Then in the html you can specify something like this:

<div class=’notPrint’>This is not visible in printing</div>

This is useful if you have things like buttons and other stuff that is not applicable when printing.

Center an html table

Quick tip.

Since css is the more ‘accepted’ way to apply formatting to html I discovered that the normal styles like align:center or text-align:center does not work anymore to center an html table horizontally.

Instead to be able to do it you need to add the following css definition:

table
{

margin-left:auto; margin-right:auto;

}

That will make it work in Firefox but not IE (9 as tested). To get IE to display it properly you need to enclose the whole table inside another tag – like a div:

<div class=”centerme”>

<table>

</table>

</div>

plus add css for the div:

div.centerme
{

margin-left:auto; margin-right:auto;text-align:center;

}

Not pretty but it works.