Where do I initialize?

  • Thread starter Phillip N Rounds
  • Start date
P

Phillip N Rounds

I'm working on a UserControl ( ASP.NET 1.1, C#, VS 2003), where I can't
manage the state of multiple controls correctly.

The controls have the following constraints

1: It has two possible appearances: One 25px high ( InActiveState) with
just a summary & an 'Activate / DeActivate' button, or the complete (400px)
high (Active) control
2: Only one control can be active on a page at any given time.

My problem is that my logic doesn't work ( or, why else would I be posting
this).
I have a test container with 3+ controls on it. I activate the first
control just fine. When I activate the second control, it appears but the
first control doesn't de-activate. When I activate the third control, the
first two deactivate. I then activate one of the first two, and the third
doesn't deactivate. When I activate another, the two previously active
controls deactivate. I'm wondering if I should move some of the following
logic to the Page_Unload method, or to the Initialize or PreRender methods.

My existing logic is as follows:

I am handling this using Session Variables, all defined/modified in the
Page_Load or the btnActivate_DeActivate_Click methods of the MyControl
class.
I create a Session variable for each control: Session[ this.ClientId +
"_ActiveState" ] , = "1" for the active state, "0" otherwise,
and one session variable to indicate which control is active: Session[
"WhichControlIsActive" ] = this.ClientId
of the currently active control

In MyControl


Page_Load
{
if ( Session[this.ClientId + "_ActiveState"] == null ) Session[
this.ClientId + "_ActiveState"] = "0"; // Initialize null session
variable for active state to inactive
if ( Session[ "WhichControlIsActive" ] == null ) Session[
"WhichControlIsActive" ] == "None"; // Initialize null session
variable for active control to none

if ( (Session[this.ClientId + "_ActiveState"] == "0") || (
Session["WhichControlIsActive" ] != this.ClientId)) // this control is
inactive
{
SetHeight( this, 25);
this.btnActivateDeActivate.text = "Activate";
Session[this.ClientId + "_ActiveState] == "0" ;
}
if ( ( Session[this.ClientId + "_ActiveState] == "1") && (
Session["WhichControlIsActive" ] == this.ClientID) ) // this control
is active
{
SetHeight( this, 400);
this.btnActivateDeAtivate.text = "De Activate";
}

}

btnActivateDeActivate_Click( .... )
{
// Determine whether to Activate or Deactivate the control
if ( Session[ "WhichControlIsActive" ] == this.ClientID
// This was the active control, so deactivate it
{
Session[ this.ClientID + "_ActiveState" ] = "0";
SetHeight( this, 25);
this.btnActivateDeActivate.text = "Activate";
Session[ "WhichControlIsActive" ] = "None";
// I just deactivated the active control, so no control is active
Session[ this.ClientID + "_ActiveState" ] = "0";
// Set this controls session variable of active state to "0"
DoTheRestOfMyStuff( .. );

}
else // This control is
newly the active control
{
Session[ this.ClientID + "_ActiveState" ] = "1"
// Set this control's session variable to "1";
Session[ "WhichControlIsActive " ] = this.ClientID;
// Set the 'Global' session variable to this control
SetHeight( this, 400);
this.btnActivateDeActivate.text = "De Activate";
}
}


Any Suggestions?

Thanks
 
G

Guest

Phillip,

pre_render

Good Luck
DWS

Phillip N Rounds said:
I'm working on a UserControl ( ASP.NET 1.1, C#, VS 2003), where I can't
manage the state of multiple controls correctly.

The controls have the following constraints

1: It has two possible appearances: One 25px high ( InActiveState) with
just a summary & an 'Activate / DeActivate' button, or the complete (400px)
high (Active) control
2: Only one control can be active on a page at any given time.

My problem is that my logic doesn't work ( or, why else would I be posting
this).
I have a test container with 3+ controls on it. I activate the first
control just fine. When I activate the second control, it appears but the
first control doesn't de-activate. When I activate the third control, the
first two deactivate. I then activate one of the first two, and the third
doesn't deactivate. When I activate another, the two previously active
controls deactivate. I'm wondering if I should move some of the following
logic to the Page_Unload method, or to the Initialize or PreRender methods.

My existing logic is as follows:

I am handling this using Session Variables, all defined/modified in the
Page_Load or the btnActivate_DeActivate_Click methods of the MyControl
class.
I create a Session variable for each control: Session[ this.ClientId +
"_ActiveState" ] , = "1" for the active state, "0" otherwise,
and one session variable to indicate which control is active: Session[
"WhichControlIsActive" ] = this.ClientId
of the currently active control

In MyControl


Page_Load
{
if ( Session[this.ClientId + "_ActiveState"] == null ) Session[
this.ClientId + "_ActiveState"] = "0"; // Initialize null session
variable for active state to inactive
if ( Session[ "WhichControlIsActive" ] == null ) Session[
"WhichControlIsActive" ] == "None"; // Initialize null session
variable for active control to none

if ( (Session[this.ClientId + "_ActiveState"] == "0") || (
Session["WhichControlIsActive" ] != this.ClientId)) // this control is
inactive
{
SetHeight( this, 25);
this.btnActivateDeActivate.text = "Activate";
Session[this.ClientId + "_ActiveState] == "0" ;
}
if ( ( Session[this.ClientId + "_ActiveState] == "1") && (
Session["WhichControlIsActive" ] == this.ClientID) ) // this control
is active
{
SetHeight( this, 400);
this.btnActivateDeAtivate.text = "De Activate";
}

}

btnActivateDeActivate_Click( .... )
{
// Determine whether to Activate or Deactivate the control
if ( Session[ "WhichControlIsActive" ] == this.ClientID
// This was the active control, so deactivate it
{
Session[ this.ClientID + "_ActiveState" ] = "0";
SetHeight( this, 25);
this.btnActivateDeActivate.text = "Activate";
Session[ "WhichControlIsActive" ] = "None";
// I just deactivated the active control, so no control is active
Session[ this.ClientID + "_ActiveState" ] = "0";
// Set this controls session variable of active state to "0"
DoTheRestOfMyStuff( .. );

}
else // This control is
newly the active control
{
Session[ this.ClientID + "_ActiveState" ] = "1"
// Set this control's session variable to "1";
Session[ "WhichControlIsActive " ] = this.ClientID;
// Set the 'Global' session variable to this control
SetHeight( this, 400);
this.btnActivateDeActivate.text = "De Activate";
}
}


Any Suggestions?

Thanks
 
P

Phillip N Rounds

Thanks, that's where I ended up putting most of my logic.

I'm still not happy that I create approximately 200 controls in Page_Load
for each of 10 instances of my user control, then proceed to hide them in
Pre_Render in all but one instance, but that seems the best we can do. Some
other functionality took a lot of really kludgy workaround.

I still think it would be nice if there were some class of variables, e.g.
ImmediateSession["MyKey"], which would reflect actions from a previous
instance of a webform available in the Page_Load of the repost.

Thanks again

DWS said:
Phillip,

pre_render

Good Luck
DWS

Phillip N Rounds said:
I'm working on a UserControl ( ASP.NET 1.1, C#, VS 2003), where I can't
manage the state of multiple controls correctly.

The controls have the following constraints

1: It has two possible appearances: One 25px high ( InActiveState) with
just a summary & an 'Activate / DeActivate' button, or the complete
(400px)
high (Active) control
2: Only one control can be active on a page at any given time.

My problem is that my logic doesn't work ( or, why else would I be
posting
this).
I have a test container with 3+ controls on it. I activate the first
control just fine. When I activate the second control, it appears but
the
first control doesn't de-activate. When I activate the third control,
the
first two deactivate. I then activate one of the first two, and the
third
doesn't deactivate. When I activate another, the two previously active
controls deactivate. I'm wondering if I should move some of the
following
logic to the Page_Unload method, or to the Initialize or PreRender
methods.

My existing logic is as follows:

I am handling this using Session Variables, all defined/modified in the
Page_Load or the btnActivate_DeActivate_Click methods of the MyControl
class.
I create a Session variable for each control: Session[ this.ClientId +
"_ActiveState" ] , = "1" for the active state, "0" otherwise,
and one session variable to indicate which control is active: Session[
"WhichControlIsActive" ] = this.ClientId
of the currently active control

In MyControl


Page_Load
{
if ( Session[this.ClientId + "_ActiveState"] == null ) Session[
this.ClientId + "_ActiveState"] = "0"; // Initialize null session
variable for active state to inactive
if ( Session[ "WhichControlIsActive" ] == null ) Session[
"WhichControlIsActive" ] == "None"; // Initialize null session
variable for active control to none

if ( (Session[this.ClientId + "_ActiveState"] == "0") || (
Session["WhichControlIsActive" ] != this.ClientId)) // this control
is
inactive
{
SetHeight( this, 25);
this.btnActivateDeActivate.text = "Activate";
Session[this.ClientId + "_ActiveState] == "0" ;
}
if ( ( Session[this.ClientId + "_ActiveState] == "1") && (
Session["WhichControlIsActive" ] == this.ClientID) ) // this
control
is active
{
SetHeight( this, 400);
this.btnActivateDeAtivate.text = "De Activate";
}

}

btnActivateDeActivate_Click( .... )
{
// Determine whether to Activate or Deactivate the control
if ( Session[ "WhichControlIsActive" ] == this.ClientID
// This was the active control, so deactivate it
{
Session[ this.ClientID + "_ActiveState" ] = "0";
SetHeight( this, 25);
this.btnActivateDeActivate.text = "Activate";
Session[ "WhichControlIsActive" ] = "None";
// I just deactivated the active control, so no control is active
Session[ this.ClientID + "_ActiveState" ] = "0";
// Set this controls session variable of active state to "0"
DoTheRestOfMyStuff( .. );

}
else // This control
is
newly the active control
{
Session[ this.ClientID + "_ActiveState" ] = "1"
// Set this control's session variable to "1";
Session[ "WhichControlIsActive " ] = this.ClientID;
// Set the 'Global' session variable to this control
SetHeight( this, 400);
this.btnActivateDeActivate.text = "De Activate";
}
}


Any Suggestions?

Thanks
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top