Default Values for Control Properties

D

dolfandon

I have a custom control that has a bunch of properties on it. I added
the DefaultValue attribute to it and it all seems to work fine with
showing up in the desiger and all. My question is, how do I get at that
default value at run time? If the user does not enter anything in the
designer then the entry never gets put in the control tag in the aspx
page and when I read the value it is empty. What is the magic to get
the default value or do I have to put it in the get of the property but
that seems clunky.

ASP.Net 2.0 for the record.

TIA
Don
 
B

Brock Allen

Use reflection:

object[] attrs = typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(typeof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
object value = attr.Value;
 
M

MSDN

Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the Width
What do you put , and is this good for FW 2.0?

Thanks


Brock Allen said:
Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(typeof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
object value = attr.Value;



I have a custom control that has a bunch of properties on it. I added
the DefaultValue attribute to it and it all seems to work fine with
showing up in the desiger and all. My question is, how do I get at
that default value at run time? If the user does not enter anything in
the designer then the entry never gets put in the control tag in the
aspx page and when I read the value it is empty. What is the magic to
get the default value or do I have to put it in the get of the
property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 
M

MasterGaurav

Adding to Brock's comments...

Default Value attribute is only for the designer.
Runtime has nothing to do with it... it never makes use of it.
It if your responsibility to provide the value.

btw, where are you writing the the code that Brock provided?

Simplest way:

public PropertyType PropertyName
{
get
{
object obj = GetFromViewState...
if(obj != null)
{
(PropertyType) obj;
}
return DefaultValue_Of_PropertyName;
}
}
 
D

dolfandon

I ended up doing what Gaurav has (sort of) :)

[DefaultValue("defaultForProperty1")]
public string Property1
{
get
{
return (ValueNotInViewState() ? "defaultForProperty1" :
ValueInViewState);
}
set
{
SaveValueInViewState(value);
}
}
 
B

Brock Allen

This works for me:

using System;
using System.ComponentModel;

public class Foo
{
[DefaultValue(5)]
public int Data
{
get
{
return 5;
}
}
}

class App
{
static void Main(string[] args)
{
object[] attrs =
typeof(Foo).GetProperty("Data").GetCustomAttributes(typeof(DefaultValueAttribute),
false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
Console.WriteLine(val);
}
}




Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the Width
What do you put , and is this good for FW 2.0?
Thanks

Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(typ
eof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
object value = attr.Value;
I have a custom control that has a bunch of properties on it. I
added the DefaultValue attribute to it and it all seems to work fine
with showing up in the desiger and all. My question is, how do I get
at that default value at run time? If the user does not enter
anything in the designer then the entry never gets put in the
control tag in the aspx page and when I read the value it is empty.
What is the magic to get the default value or do I have to put it in
the get of the property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 
M

MSDN

Brock,

First Thank you,
Second: When I drop a TextBox on a web form it has some default properties
like width and height.
Without assigning any values to the TextBox above, How do I get these
defaults?

Thank You,

SA


Brock Allen said:
This works for me:

using System;
using System.ComponentModel;

public class Foo
{
[DefaultValue(5)]
public int Data
{
get
{
return 5;
}
}
}

class App
{
static void Main(string[] args)
{
object[] attrs =
typeof(Foo).GetProperty("Data").GetCustomAttributes(typeof(DefaultValueAttribute),
false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
Console.WriteLine(val);
}
}




Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the Width
What do you put , and is this good for FW 2.0?
Thanks

Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(typ
eof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
object value = attr.Value;

I have a custom control that has a bunch of properties on it. I
added the DefaultValue attribute to it and it all seems to work fine
with showing up in the desiger and all. My question is, how do I get
at that default value at run time? If the user does not enter
anything in the designer then the entry never gets put in the
control tag in the aspx page and when I read the value it is empty.
What is the magic to get the default value or do I have to put it in
the get of the property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 
B

Brock Allen

In my sample code change "Foo" to "TextBox" and change "Data" to "Width".



Brock,

First Thank you,
Second: When I drop a TextBox on a web form it has some default
properties
like width and height.
Without assigning any values to the TextBox above, How do I get these
defaults?
Thank You,

SA

This works for me:

using System;
using System.ComponentModel;
public class Foo
{
[DefaultValue(5)]
public int Data
{
get
{
return 5;
}
}
}
class App
{
static void Main(string[] args)
{
object[] attrs =
typeof(Foo).GetProperty("Data").GetCustomAttributes(typeof(DefaultVal
ueAttribute),
false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
Console.WriteLine(val);
}
}
Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the Width
What do you put , and is this good for FW 2.0?
Thanks

Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(t
yp
eof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as
DefaultPropertyAttribute;
object value = attr.Value;

I have a custom control that has a bunch of properties on it. I
added the DefaultValue attribute to it and it all seems to work
fine with showing up in the desiger and all. My question is, how
do I get at that default value at run time? If the user does not
enter anything in the designer then the entry never gets put in
the control tag in the aspx page and when I read the value it is
empty. What is the magic to get the default value or do I have to
put it in the get of the property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 
M

MSDN

Used your exact code, still getting 0 (zero)

double wid = TextBox1.Width.Value;
object[] attrs =typeof(System.Web.UI.WebControls.TextBox).GetProperty("Width").GetCustomAttributes(typeof(System.ComponentModel.DefaultValueAttribute),false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;

I guess this info is not available for web controls???????????????????????

Thank you Brock
SA

Brock Allen said:
In my sample code change "Foo" to "TextBox" and change "Data" to "Width".



Brock,

First Thank you,
Second: When I drop a TextBox on a web form it has some default
properties
like width and height.
Without assigning any values to the TextBox above, How do I get these
defaults?
Thank You,

SA

This works for me:

using System;
using System.ComponentModel;
public class Foo
{
[DefaultValue(5)]
public int Data
{
get
{
return 5;
}
}
}
class App
{
static void Main(string[] args)
{
object[] attrs =
typeof(Foo).GetProperty("Data").GetCustomAttributes(typeof(DefaultVal
ueAttribute),
false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
Console.WriteLine(val);
}
}

Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the Width
What do you put , and is this good for FW 2.0?
Thanks

Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(t
yp
eof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as
DefaultPropertyAttribute;
object value = attr.Value;

I have a custom control that has a bunch of properties on it. I
added the DefaultValue attribute to it and it all seems to work
fine with showing up in the desiger and all. My question is, how
do I get at that default value at run time? If the user does not
enter anything in the designer then the entry never gets put in
the control tag in the aspx page and when I read the value it is
empty. What is the magic to get the default value or do I have to
put it in the get of the property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 
B

Brock Allen

Does the array have zero or one element in it? If it has one element, then
that's telling you the DefaultValue attribute has been appliied to the Width
proeprty and the default value is "" (the empty string). Looking with reflector,
that's what I'm expecting:

[WebCategory("Layout"), DefaultValue(typeof(Unit), ""), WebSysDescription("WebControl_Width")]
public virtual Unit Width
{
get
{
if (!this.ControlStyleCreated)
{
return Unit.Empty;
}
return this.ControlStyle.Width;
}
set
{
this.ControlStyle.Width = value;
}
}




Used your exact code, still getting 0 (zero)

double wid = TextBox1.Width.Value;
object[] attrs
=typeof(System.Web.UI.WebControls.TextBox).GetProperty("Width").GetCus
t
omAttributes(typeof(System.ComponentModel.DefaultValueAttribute),false
);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
I guess this info is not available for web
controls???????????????????????

Thank you Brock
SA
In my sample code change "Foo" to "TextBox" and change "Data" to
"Width".
Brock,

First Thank you,
Second: When I drop a TextBox on a web form it has some default
properties
like width and height.
Without assigning any values to the TextBox above, How do I get
these
defaults?
Thank You,
SA


This works for me:

using System;
using System.ComponentModel;
public class Foo
{
[DefaultValue(5)]
public int Data
{
get
{
return 5;
}
}
}
class App
{
static void Main(string[] args)
{
object[] attrs =
typeof(Foo).GetProperty("Data").GetCustomAttributes(typeof(DefaultVal
ueAttribute),
false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
Console.WriteLine(val);
}
}

Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the
Width
What do you put , and is this good for FW 2.0?
Thanks
Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(t
yp
eof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as
DefaultPropertyAttribute;
object value = attr.Value;

I have a custom control that has a bunch of properties on it. I
added the DefaultValue attribute to it and it all seems to work
fine with showing up in the desiger and all. My question is, how
do I get at that default value at run time? If the user does not
enter anything in the designer then the entry never gets put in
the control tag in the aspx page and when I read the value it is
empty. What is the magic to get the default value or do I have
to put it in the get of the property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 
M

MSDN

Brock,

We might be thinking about different things here. may be not?
when you drop a TextBox control on the Designer it has a default width,
height etc...
How do you get that default width.
Microsoft must be getting this default from some place ( meta data of
defaults etc.. )
I was hoping that your code can tell me how to get this default value.
Microsoft must be baking this someplace.

If the default is "" String.Empty(); then how does it show at design and run
time at some width greater than 50px ++ ; ??

Are we on the right page?

Thank you Brock

SA


Brock Allen said:
Does the array have zero or one element in it? If it has one element, then
that's telling you the DefaultValue attribute has been appliied to the
Width proeprty and the default value is "" (the empty string). Looking
with reflector, that's what I'm expecting:

[WebCategory("Layout"), DefaultValue(typeof(Unit), ""),
WebSysDescription("WebControl_Width")]
public virtual Unit Width
{
get
{
if (!this.ControlStyleCreated)
{
return Unit.Empty;
}
return this.ControlStyle.Width;
}
set
{
this.ControlStyle.Width = value;
}
}




Used your exact code, still getting 0 (zero)

double wid = TextBox1.Width.Value;
object[] attrs
=typeof(System.Web.UI.WebControls.TextBox).GetProperty("Width").GetCus
t
omAttributes(typeof(System.ComponentModel.DefaultValueAttribute),false
);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
I guess this info is not available for web
controls???????????????????????

Thank you Brock
SA
In my sample code change "Foo" to "TextBox" and change "Data" to
"Width".


Brock,

First Thank you,
Second: When I drop a TextBox on a web form it has some default
properties
like width and height.
Without assigning any values to the TextBox above, How do I get
these
defaults?
Thank You,
SA


This works for me:

using System;
using System.ComponentModel;
public class Foo
{
[DefaultValue(5)]
public int Data
{
get
{
return 5;
}
}
}
class App
{
static void Main(string[] args)
{
object[] attrs = typeof(Foo).GetProperty("Data").GetCustomAttributes(typeof(DefaultVal

ueAttribute),
false);
DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
object val = attr.Value;
Console.WriteLine(val);
}
}

Brock,

I can't make your reflection example to work.
For example in a class called "Foo", a TextBox1 and to get the Width

What do you put , and is this good for FW 2.0?
Thanks
Use reflection:

object[] attrs =
typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(t

yp
eof(DefaultPropertyAttribute),
false);
DefaultPropertyAttribute attr = attrs[0] as
DefaultPropertyAttribute;
object value = attr.Value;

I have a custom control that has a bunch of properties on it. I
added the DefaultValue attribute to it and it all seems to work
fine with showing up in the desiger and all. My question is, how
do I get at that default value at run time? If the user does not
enter anything in the designer then the entry never gets put in
the control tag in the aspx page and when I read the value it is
empty. What is the magic to get the default value or do I have
to put it in the get of the property but that seems clunky.

ASP.Net 2.0 for the record.

TIA Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top