By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

Composite Control Property Setting Problem

P: n/a
I have built a simple composite control that consists of a textbox,
requiredfieldvalidator and rangevalidator.

For properties that are unique to the individual control, I set/get them
directly from the control as follows:
[Description("The text value"),
Bindable(true),
Browsable(true),
Category("TextBox"),
DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return txt1.Text;
}

set
{
EnsureChildControls();
txt1.Text = value;
}
}

For properties that are common to more than one control or that I want to
additionally manipulate within the composite, I use private static variables
and then assign each control the variable's value before adding it to the
controls collection:

private static string _mstrValCssClass="";

[Description("CssClass to apply to validator portion of control"),
Browsable(true),
Category("Validators")]
public string ValCssClass
{
get
{
return _mstrValCssClass;
}
set
{
_mstrValCssClass = value;
}
}

This all seemed to work well when adding the composite dynamically or using
only one on a user control. My problem when I add two of the composites to a
user control in the designer. Using the above property as an example, if I
set the ValCssClass of mycomposite1 to myclass1 and set the ValCssClass of
mycomposite2 to myclass2, then the property also changes for mycomposite1
and viceversa. This only happens with the properties that utilize a static
variable, not the ones that are set per the first property example.

Can anyone explain this and possibly provide a solution?

TIA
--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us

Nov 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Alphonse Giambrone" <NO**********@example.invalid> wrote in message
news:ec****************@tk2msftngp13.phx.gbl...
I have built a simple composite control that consists of a textbox,
requiredfieldvalidator and rangevalidator.

For properties that are unique to the individual control, I set/get them
directly from the control as follows:
[Description("The text value"),
Bindable(true),
Browsable(true),
Category("TextBox"),
DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return txt1.Text;
}

set
{
EnsureChildControls();
txt1.Text = value;
}
}

For properties that are common to more than one control or that I want to
additionally manipulate within the composite, I use private static variables and then assign each control the variable's value before adding it to the
controls collection:

private static string _mstrValCssClass="";

[Description("CssClass to apply to validator portion of control"),
Browsable(true),
Category("Validators")]
public string ValCssClass
{
get
{
return _mstrValCssClass;
}
set
{
_mstrValCssClass = value;
}
}

This all seemed to work well when adding the composite dynamically or using only one on a user control. My problem when I add two of the composites to a user control in the designer. Using the above property as an example, if I
set the ValCssClass of mycomposite1 to myclass1 and set the ValCssClass of
mycomposite2 to myclass2, then the property also changes for mycomposite1
and viceversa. This only happens with the properties that utilize a static
variable, not the ones that are set per the first property example.

Can anyone explain this and possibly provide a solution?


Yes. Don't use statics. "static" means it's a member of the class, not a
member of a class instance. Each time you drop your control on a page, you
get a new instance of the control, but all instances will share the same
static members.

So, "don't do that"!
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2

P: n/a
Thanks for the speedy reply and info John.
I don't recall just why I made the variables static, but removing the static
does solve the problem.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
"Alphonse Giambrone" <NO**********@example.invalid> wrote in message
news:ec****************@tk2msftngp13.phx.gbl...
I have built a simple composite control that consists of a textbox,
requiredfieldvalidator and rangevalidator.

For properties that are unique to the individual control, I set/get them
directly from the control as follows:
[Description("The text value"),
Bindable(true),
Browsable(true),
Category("TextBox"),
DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return txt1.Text;
}

set
{
EnsureChildControls();
txt1.Text = value;
}
}

For properties that are common to more than one control or that I want to additionally manipulate within the composite, I use private static variables
and then assign each control the variable's value before adding it to the controls collection:

private static string _mstrValCssClass="";

[Description("CssClass to apply to validator portion of control"),
Browsable(true),
Category("Validators")]
public string ValCssClass
{
get
{
return _mstrValCssClass;
}
set
{
_mstrValCssClass = value;
}
}

This all seemed to work well when adding the composite dynamically or

using
only one on a user control. My problem when I add two of the composites to a
user control in the designer. Using the above property as an example, if

I set the ValCssClass of mycomposite1 to myclass1 and set the ValCssClass of mycomposite2 to myclass2, then the property also changes for mycomposite1 and viceversa. This only happens with the properties that utilize a static variable, not the ones that are set per the first property example.

Can anyone explain this and possibly provide a solution?


Yes. Don't use statics. "static" means it's a member of the class, not a
member of a class instance. Each time you drop your control on a page, you
get a new instance of the control, but all instances will share the same
static members.

So, "don't do that"!
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #3

P: n/a
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
"Alphonse Giambrone" <NO**********@example.invalid> wrote in message
news:ec****************@tk2msftngp13.phx.gbl...
I have built a simple composite control that consists of a textbox,
requiredfieldvalidator and rangevalidator.

For properties that are unique to the individual control, I set/get them
directly from the control as follows:
[Description("The text value"),
Bindable(true),
Browsable(true),
Category("TextBox"),
DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return txt1.Text;
}

set
{
EnsureChildControls();
txt1.Text = value;
}
}

For properties that are common to more than one control or that I want to additionally manipulate within the composite, I use private static variables
and then assign each control the variable's value before adding it to the controls collection:

private static string _mstrValCssClass="";

[Description("CssClass to apply to validator portion of control"),
Browsable(true),
Category("Validators")]
public string ValCssClass
{
get
{
return _mstrValCssClass;
}
set
{
_mstrValCssClass = value;
}
}

This all seemed to work well when adding the composite dynamically or

using
only one on a user control. My problem when I add two of the composites to a
user control in the designer. Using the above property as an example, if

I set the ValCssClass of mycomposite1 to myclass1 and set the ValCssClass of mycomposite2 to myclass2, then the property also changes for mycomposite1 and viceversa. This only happens with the properties that utilize a static variable, not the ones that are set per the first property example.

Can anyone explain this and possibly provide a solution?


Yes. Don't use statics. "static" means it's a member of the class, not a
member of a class instance. Each time you drop your control on a page, you
get a new instance of the control, but all instances will share the same
static members.

So, "don't do that"!
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #4

P: n/a
Yes, you can, but the syntax is different. You need angle brackets instead
of square brackets:

<[Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
"Alphonse Giambrone" <NO**********@example.invalid> wrote in message
news:ec****************@tk2msftngp13.phx.gbl...
I have built a simple composite control that consists of a textbox,
requiredfieldvalidator and rangevalidator.

For properties that are unique to the individual control, I set/get them directly from the control as follows:
[Description("The text value"),
Bindable(true),
Browsable(true),
Category("TextBox"),
DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return txt1.Text;
}

set
{
EnsureChildControls();
txt1.Text = value;
}
}

For properties that are common to more than one control or that I want to additionally manipulate within the composite, I use private static variables
and then assign each control the variable's value before adding it to the controls collection:

private static string _mstrValCssClass="";

[Description("CssClass to apply to validator portion of control"),
Browsable(true),
Category("Validators")]
public string ValCssClass
{
get
{
return _mstrValCssClass;
}
set
{
_mstrValCssClass = value;
}
}

This all seemed to work well when adding the composite dynamically or

using
only one on a user control. My problem when I add two of the
composites to
a
user control in the designer. Using the above property as an example,
if
I set the ValCssClass of mycomposite1 to myclass1 and set the
ValCssClass
of mycomposite2 to myclass2, then the property also changes for mycomposite1 and viceversa. This only happens with the properties that utilize a static variable, not the ones that are set per the first property example.

Can anyone explain this and possibly provide a solution?


Yes. Don't use statics. "static" means it's a member of the class, not a
member of a class instance. Each time you drop your control on a page,

you get a new instance of the control, but all instances will share the same
static members.

So, "don't do that"!
--
John Saunders
johnwsaundersiii at hotmail


Nov 18 '05 #5

P: n/a
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
"Alphonse Giambrone" <NO**********@example.invalid> wrote in message
news:ec****************@tk2msftngp13.phx.gbl...
I have built a simple composite control that consists of a textbox,
requiredfieldvalidator and rangevalidator.

For properties that are unique to the individual control, I set/get them directly from the control as follows:
[Description("The text value"),
Bindable(true),
Browsable(true),
Category("TextBox"),
DefaultValue("")]
public string Text
{
get
{
EnsureChildControls();
return txt1.Text;
}

set
{
EnsureChildControls();
txt1.Text = value;
}
}

For properties that are common to more than one control or that I want to additionally manipulate within the composite, I use private static variables
and then assign each control the variable's value before adding it to the controls collection:

private static string _mstrValCssClass="";

[Description("CssClass to apply to validator portion of control"),
Browsable(true),
Category("Validators")]
public string ValCssClass
{
get
{
return _mstrValCssClass;
}
set
{
_mstrValCssClass = value;
}
}

This all seemed to work well when adding the composite dynamically or

using
only one on a user control. My problem when I add two of the
composites to
a
user control in the designer. Using the above property as an example,
if
I set the ValCssClass of mycomposite1 to myclass1 and set the
ValCssClass
of mycomposite2 to myclass2, then the property also changes for mycomposite1 and viceversa. This only happens with the properties that utilize a static variable, not the ones that are set per the first property example.

Can anyone explain this and possibly provide a solution?


Yes. Don't use statics. "static" means it's a member of the class, not a
member of a class instance. Each time you drop your control on a page,

you get a new instance of the control, but all instances will share the same
static members.

So, "don't do that"!
--
John Saunders
johnwsaundersiii at hotmail


Nov 18 '05 #6

P: n/a
John,
I tried that but its telling me that Description is not defined ???
Am I missing an Import??
I checked and can not figure it out.

Thanks
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
"Alphonse Giambrone" <NO**********@example.invalid> wrote in message
news:ec****************@tk2msftngp13.phx.gbl...
> I have built a simple composite control that consists of a textbox,
> requiredfieldvalidator and rangevalidator.
>
> For properties that are unique to the individual control, I set/get them > directly from the control as follows:
> [Description("The text value"),
> Bindable(true),
> Browsable(true),
> Category("TextBox"),
> DefaultValue("")]
> public string Text
> {
> get
> {
> EnsureChildControls();
> return txt1.Text;
> }
>
> set
> {
> EnsureChildControls();
> txt1.Text = value;
> }
> }
>
> For properties that are common to more than one control or that I want
to
> additionally manipulate within the composite, I use private static
variables
> and then assign each control the variable's value before adding it
to
the
> controls collection:
>
> private static string _mstrValCssClass="";
>
> [Description("CssClass to apply to validator portion of control"),
> Browsable(true),
> Category("Validators")]
> public string ValCssClass
> {
> get
> {
> return _mstrValCssClass;
> }
> set
> {
> _mstrValCssClass = value;
> }
> }
>
> This all seemed to work well when adding the composite dynamically or using
> only one on a user control. My problem when I add two of the

composites
to
a
> user control in the designer. Using the above property as an example, if
I
> set the ValCssClass of mycomposite1 to myclass1 and set the

ValCssClass
of
> mycomposite2 to myclass2, then the property also changes for

mycomposite1
> and viceversa. This only happens with the properties that utilize a

static
> variable, not the ones that are set per the first property example.
>
> Can anyone explain this and possibly provide a solution?

Yes. Don't use statics. "static" means it's a member of the class, not

a member of a class instance. Each time you drop your control on a page,

you get a new instance of the control, but all instances will share the same static members.

So, "don't do that"!
--
John Saunders
johnwsaundersiii at hotmail



Nov 18 '05 #7

P: n/a
Hi,

you need to have System.ComponentModel namespace imported

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
John,
I tried that but its telling me that Description is not defined ???
Am I missing an Import??
I checked and can not figure it out.

Thanks
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
> "Alphonse Giambrone" <NO**********@example.invalid> wrote in message
> news:ec****************@tk2msftngp13.phx.gbl...
> > I have built a simple composite control that consists of a textbox, > > requiredfieldvalidator and rangevalidator.
> >
> > For properties that are unique to the individual control, I set/get
them
> > directly from the control as follows:
> > [Description("The text value"),
> > Bindable(true),
> > Browsable(true),
> > Category("TextBox"),
> > DefaultValue("")]
> > public string Text
> > {
> > get
> > {
> > EnsureChildControls();
> > return txt1.Text;
> > }
> >
> > set
> > {
> > EnsureChildControls();
> > txt1.Text = value;
> > }
> > }
> >
> > For properties that are common to more than one control or that I want to
> > additionally manipulate within the composite, I use private static
> variables
> > and then assign each control the variable's value before adding it to the
> > controls collection:
> >
> > private static string _mstrValCssClass="";
> >
> > [Description("CssClass to apply to validator portion of
control"),
> Browsable(true),
> > Category("Validators")]
> > public string ValCssClass
> > {
> > get
> > {
> > return _mstrValCssClass;
> > }
> > set
> > {
> > _mstrValCssClass = value;
> > }
> > }
> >
> > This all seemed to work well when adding the composite dynamically or > using
> > only one on a user control. My problem when I add two of the composites
to
> a
> > user control in the designer. Using the above property as an example,
if
I
> > set the ValCssClass of mycomposite1 to myclass1 and set the

ValCssClass
of
> > mycomposite2 to myclass2, then the property also changes for
mycomposite1
> > and viceversa. This only happens with the properties that utilize
a static
> > variable, not the ones that are set per the first property example. > >
> > Can anyone explain this and possibly provide a solution?
>
> Yes. Don't use statics. "static" means it's a member of the class,

not a > member of a class instance. Each time you drop your control on a
page, you
> get a new instance of the control, but all instances will share the

same > static members.
>
> So, "don't do that"!
> --
> John Saunders
> johnwsaundersiii at hotmail
>
>



Nov 18 '05 #8

P: n/a
Thank you Sir, will try it.
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

you need to have System.ComponentModel namespace imported

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
John,
I tried that but its telling me that Description is not defined ???
Am I missing an Import??
I checked and can not figure it out.

Thanks
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
> can I use the same attributes [Description("The text value"),
> etc... in VB.NET
>
> SA
>
> "John Saunders" <jo**************@notcoldmail.com> wrote in message
> news:Of**************@tk2msftngp13.phx.gbl...
> > "Alphonse Giambrone" <NO**********@example.invalid> wrote in message > > news:ec****************@tk2msftngp13.phx.gbl...
> > > I have built a simple composite control that consists of a textbox, > > > requiredfieldvalidator and rangevalidator.
> > >
> > > For properties that are unique to the individual control, I set/get them
> > > directly from the control as follows:
> > > [Description("The text value"),
> > > Bindable(true),
> > > Browsable(true),
> > > Category("TextBox"),
> > > DefaultValue("")]
> > > public string Text
> > > {
> > > get
> > > {
> > > EnsureChildControls();
> > > return txt1.Text;
> > > }
> > >
> > > set
> > > {
> > > EnsureChildControls();
> > > txt1.Text = value;
> > > }
> > > }
> > >
> > > For properties that are common to more than one control or that I
want
> to
> > > additionally manipulate within the composite, I use private
static > > variables
> > > and then assign each control the variable's value before adding
it to
> the
> > > controls collection:
> > >
> > > private static string _mstrValCssClass="";
> > >
> > > [Description("CssClass to apply to validator portion of control"),
> > > Browsable(true),
> > > Category("Validators")]
> > > public string ValCssClass
> > > {
> > > get
> > > {
> > > return _mstrValCssClass;
> > > }
> > > set
> > > {
> > > _mstrValCssClass = value;
> > > }
> > > }
> > >
> > > This all seemed to work well when adding the composite
dynamically or
> > using
> > > only one on a user control. My problem when I add two of the
composites
> to
> > a
> > > user control in the designer. Using the above property as an

example,
if
> I
> > > set the ValCssClass of mycomposite1 to myclass1 and set the
ValCssClass
> of
> > > mycomposite2 to myclass2, then the property also changes for
> mycomposite1
> > > and viceversa. This only happens with the properties that
utilize a > static
> > > variable, not the ones that are set per the first property example. > > >
> > > Can anyone explain this and possibly provide a solution?
> >
> > Yes. Don't use statics. "static" means it's a member of the class, not
a
> > member of a class instance. Each time you drop your control on a

page, you
> > get a new instance of the control, but all instances will share

the same
> > static members.
> >
> > So, "don't do that"!
> > --
> > John Saunders
> > johnwsaundersiii at hotmail
> >
> >
>
>



Nov 18 '05 #9

P: n/a
that was it thank you.
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
Hi,

you need to have System.ComponentModel namespace imported

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
John,
I tried that but its telling me that Description is not defined ???
Am I missing an Import??
I checked and can not figure it out.

Thanks
"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail
"MS News (MS ILM)" <sq***********@hotmail.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
> can I use the same attributes [Description("The text value"),
> etc... in VB.NET
>
> SA
>
> "John Saunders" <jo**************@notcoldmail.com> wrote in message
> news:Of**************@tk2msftngp13.phx.gbl...
> > "Alphonse Giambrone" <NO**********@example.invalid> wrote in message > > news:ec****************@tk2msftngp13.phx.gbl...
> > > I have built a simple composite control that consists of a textbox, > > > requiredfieldvalidator and rangevalidator.
> > >
> > > For properties that are unique to the individual control, I set/get them
> > > directly from the control as follows:
> > > [Description("The text value"),
> > > Bindable(true),
> > > Browsable(true),
> > > Category("TextBox"),
> > > DefaultValue("")]
> > > public string Text
> > > {
> > > get
> > > {
> > > EnsureChildControls();
> > > return txt1.Text;
> > > }
> > >
> > > set
> > > {
> > > EnsureChildControls();
> > > txt1.Text = value;
> > > }
> > > }
> > >
> > > For properties that are common to more than one control or that I
want
> to
> > > additionally manipulate within the composite, I use private
static > > variables
> > > and then assign each control the variable's value before adding
it to
> the
> > > controls collection:
> > >
> > > private static string _mstrValCssClass="";
> > >
> > > [Description("CssClass to apply to validator portion of control"),
> > > Browsable(true),
> > > Category("Validators")]
> > > public string ValCssClass
> > > {
> > > get
> > > {
> > > return _mstrValCssClass;
> > > }
> > > set
> > > {
> > > _mstrValCssClass = value;
> > > }
> > > }
> > >
> > > This all seemed to work well when adding the composite
dynamically or
> > using
> > > only one on a user control. My problem when I add two of the
composites
> to
> > a
> > > user control in the designer. Using the above property as an

example,
if
> I
> > > set the ValCssClass of mycomposite1 to myclass1 and set the
ValCssClass
> of
> > > mycomposite2 to myclass2, then the property also changes for
> mycomposite1
> > > and viceversa. This only happens with the properties that
utilize a > static
> > > variable, not the ones that are set per the first property example. > > >
> > > Can anyone explain this and possibly provide a solution?
> >
> > Yes. Don't use statics. "static" means it's a member of the class, not
a
> > member of a class instance. Each time you drop your control on a

page, you
> > get a new instance of the control, but all instances will share

the same
> > static members.
> >
> > So, "don't do that"!
> > --
> > John Saunders
> > johnwsaundersiii at hotmail
> >
> >
>
>



Nov 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.