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