September 2008 - Posts - Web Dev And Stuff

September 2008 - Posts

Many times I needed to set a default value for a property.

For example at the property below

public string Property1 {get; set;}

I wanted a default value "Value1". The only way was to create a private field or to set it in code like Property1 = "Value1" somewhere.

What if I wanted to do this for all my properties ?

For this case i decided to create a custom attribute which does that thing. Takes a value and if the value of the current property is null sets it to the given value.


To be more specific let's begin with the custom attribute class:

 [global::System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
sealed class DefaultValueAttribute : Attribute
readonly object defaultValue;
readonly Type valueType;

public DefaultValueAttribute(object defaultValue, Type valueType)
this.valueType = valueType;
this.defaultValue = Convert.ChangeType(defaultValue, valueType);
public Type ValueType { get { return valueType; } }
public object DefaultValue
get { return defaultValue; }

Then we have a helper class with an extension method :

public static class DefaultValueAttributeHelper
public static void SetDefaultValues(this object obj)
Type t = obj.GetType();
foreach (var item in t.GetProperties())
foreach (var attr in item.GetCustomAttributes(typeof(DefaultValueAttribute), true))
DefaultValueAttribute val = ((DefaultValueAttribute)attr);
if (item.GetValue(obj, null) == null ||
((item.GetValue(obj, null).ToString() == "0") &&
(val.ValueType != typeof(string))))
item.SetValue(obj, val.DefaultValue, null);


And finally how this can be used  for example at an ASP.NET Page:


public partial class UserDetails : System.Web.UI.Page
public UserDetails()
protected void Page_Load(object sender, EventArgs e)
Response.Write(StringProp + "<br>" + IntProp + "<br>" + (DoubleProp + 1));

[DefaultValueAttribute("6.11354", typeof(double))]
public double DoubleProp { get; set; }

[DefaultValueAttribute("6", typeof(int))]
public int IntProp { get; set; }

[DefaultValueAttribute("String1", typeof(string))]
public string StringProp { get; set; }


Well that's all ... This is really simple. Next step is to create an interface so the above attirbute can be used with custom objects.

If anyone has any improvements please let me know!

kick it on

Today I ran into an excellent post of Bill Beckelman about a server side message box.

More info you can find on this post. And I thought what if I wanted to use it via the client ? So I decided to extended a little further and to add a client-side behavior.


How can be used via the client?

To use the MessageBox via client you should call the
ShowMessageBox(type,Header,Text,Width,Height) function.

The first argument is the type. At the moment possible values are info,error,success and warning.
The second is the the header of the messagebox and the third is the text. You can also use html...!
The other two are width and height of the message box.

Example : ShowMessageBox('info','A Simple Header','Just some text',400,125);

So simple :)

You can view a demo or you can download the updated version.

Again credits to Bill Beckelman!





kick it on

More Posts