Composite Control Property Setting Problem

  • Thread starter Alphonse Giambrone
  • Start date
A

Alphonse Giambrone

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
 
J

John Saunders

Alphonse Giambrone said:
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"!
 
A

Alphonse Giambrone

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 said:
Alphonse Giambrone said:
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"!
 
M

MS News \(MS ILM\)

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

SA

John Saunders said:
Alphonse Giambrone said:
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"!
 
J

John Saunders

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) said:
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

John Saunders said:
Alphonse Giambrone said:
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
if
ValCssClass
of

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"!
 
J

John Saunders

Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail


MS News (MS ILM) said:
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

John Saunders said:
Alphonse Giambrone said:
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
if
ValCssClass
of

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"!
 
M

MS News \(MS ILM\)

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 said:
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail


MS News (MS ILM) said:
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

John Saunders said:
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 ValCssClass
 
T

Teemu Keiski

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) said:
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 said:
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail


MS News (MS ILM) said:
can I use the same attributes [Description("The text value"),
etc... in VB.NET

SA

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 page,
you
 
M

MS News \(MS ILM\)

Thank you Sir, will try it.


Teemu Keiski said:
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) said:
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 said:
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail


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

SA

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 not the
same
 
M

MS News \(MS ILM\)

that was it thank you.


Teemu Keiski said:
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) said:
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 said:
Sorry, I meant:

<Description("The text value")>

--
John Saunders
johnwsaundersiii at hotmail


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

SA

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 not the
same
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top