VS2005: Events from User Control Stored in Session Do Not Fire

H

HammRadio

I am exploring converting my web app (current Framework 1.1) to
Framework 2.0. Everything went smoothly save for one item. To reduce
the trips to the database, when loading user controls (like a large
datagrid or form), we store the grid or form in Session Object. On
PostBack we grab the control from the session, add it to our main
form. In VS 2003 (1.1), the page will fire any event called in the
custom control, e.g, cmdSave_Click. In VS 2005 (2.0), the control is
loaded, but no event occurs.

In VS 2003
Page Init -> Load UserControl -> cmdSave -> Click -> PostBack -> Page
Init -> Load Control From Session -> Page Load -> cmdSave_Click fires


In VS 2003
Page Init -> Load UserControl -> cmdSave -> Click -> PostBack -> Page
Init -> Load Control From Session -> Page Load -> cmdSave_Click does
not fire.

Any thoughts on why one would work in 2003 and one wouldn't work in
2005.

I thought it might be just my code. So I created a whole new
application that simplifies everything, here is the user control:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Sandbox
{
public partial class ucUserControlTest : System.Web.UI.UserControl
{



protected override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
}

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
this.TextBox1.Text = "Page is Loaded!";
}
}

private void InitializeComponent()
{
this.cmdSave.Click += new
EventHandler(this.cmdSave_Click);

}

public void cmdSave_Click(object sender, EventArgs e)
{

this.TextBox1.Text = "I Clicked Save.";

}


}
}


Here is the form:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Sandbox
{
public partial class UserControlTestForm : System.Web.UI.Page
{

protected override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
}

private void InitializeComponent()
{
if (!IsPostBack)
{
LoadUserControls();
}
else
{
LoadSessionControl();

}


}

protected void Page_Load(object sender, EventArgs e)
{
/// Nothing Happens Here

}


private void LoadUserControls()
{

UserControl temp = (UserControl)
LoadControl("ucUserControlTest.ascx");
this.pForm.Controls.Add(temp);
Session.Add("CurrentFormControl", temp);
}

private void LoadSessionControl()
{

UserControl temp = (UserControl)
Session["CurrentFormControl"];
/// Clears any Controls on Page. Behavior is the same
whether this is here or not.
this.pForm.Controls.Clear();
this.pForm.Controls.Add(temp);

}

}
}
 
B

bruce barker

your approach is not valid. asp.net does not support this model. as the
controls can be disposed at form unload, they may be resurrected. most
are not writtn to support this. also if they keep any state of their
initialization, you are defeating it.

you should save the dataset in session not the controls.

-- bruce (sqlwork.com)

I am exploring converting my web app (current Framework 1.1) to
Framework 2.0. Everything went smoothly save for one item. To reduce
the trips to the database, when loading user controls (like a large
datagrid or form), we store the grid or form in Session Object. On
PostBack we grab the control from the session, add it to our main
form. In VS 2003 (1.1), the page will fire any event called in the
custom control, e.g, cmdSave_Click. In VS 2005 (2.0), the control is
loaded, but no event occurs.

In VS 2003
Page Init -> Load UserControl -> cmdSave -> Click -> PostBack -> Page
Init -> Load Control From Session -> Page Load -> cmdSave_Click fires


In VS 2003
Page Init -> Load UserControl -> cmdSave -> Click -> PostBack -> Page
Init -> Load Control From Session -> Page Load -> cmdSave_Click does
not fire.

Any thoughts on why one would work in 2003 and one wouldn't work in
2005.

I thought it might be just my code. So I created a whole new
application that simplifies everything, here is the user control:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Sandbox
{
public partial class ucUserControlTest : System.Web.UI.UserControl
{



protected override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
}

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
this.TextBox1.Text = "Page is Loaded!";
}
}

private void InitializeComponent()
{
this.cmdSave.Click += new
EventHandler(this.cmdSave_Click);

}

public void cmdSave_Click(object sender, EventArgs e)
{

this.TextBox1.Text = "I Clicked Save.";

}


}
}


Here is the form:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Sandbox
{
public partial class UserControlTestForm : System.Web.UI.Page
{

protected override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
}

private void InitializeComponent()
{
if (!IsPostBack)
{
LoadUserControls();
}
else
{
LoadSessionControl();

}


}

protected void Page_Load(object sender, EventArgs e)
{
/// Nothing Happens Here

}


private void LoadUserControls()
{

UserControl temp = (UserControl)
LoadControl("ucUserControlTest.ascx");
this.pForm.Controls.Add(temp);
Session.Add("CurrentFormControl", temp);
}

private void LoadSessionControl()
{

UserControl temp = (UserControl)
Session["CurrentFormControl"];
/// Clears any Controls on Page. Behavior is the same
whether this is here or not.
this.pForm.Controls.Clear();
this.pForm.Controls.Add(temp);

}

}
}
 
H

HammRadio

Thanks bruce.

Any idea why Framework 1.1 kept the state and all events remained
valid? Was that jsut a "bug" in 1.1?

your approach is not valid. asp.net does not support this model. as the
controls can be disposed at form unload, they may be resurrected. most
are not writtn to support this. also if they keep any state of their
initialization, you are defeating it.

you should save the dataset in session not the controls.

-- bruce (sqlwork.com)


I am exploring converting my web app (current Framework 1.1) to
Framework 2.0. Everything went smoothly save for one item. To reduce
the trips to the database, when loading user controls (like a large
datagrid or form), we store the grid or form in Session Object. On
PostBack we grab the control from the session, add it to our main
form. In VS 2003 (1.1), the page will fire any event called in the
custom control, e.g, cmdSave_Click. In VS 2005 (2.0), the control is
loaded, but no event occurs.
In VS 2003
Page Init -> Load UserControl -> cmdSave -> Click -> PostBack -> Page
Init -> Load Control From Session -> Page Load -> cmdSave_Click fires
In VS 2003
Page Init -> Load UserControl -> cmdSave -> Click -> PostBack -> Page
Init -> Load Control From Session -> Page Load -> cmdSave_Click does
not fire.
Any thoughts on why one would work in 2003 and one wouldn't work in
2005.
I thought it might be just my code. So I created a whole new
application that simplifies everything, here is the user control:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Sandbox
{
public partial class ucUserControlTest : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.TextBox1.Text = "Page is Loaded!";
}
}
private void InitializeComponent()
{
this.cmdSave.Click += new
EventHandler(this.cmdSave_Click);

public void cmdSave_Click(object sender, EventArgs e)
{
this.TextBox1.Text = "I Clicked Save.";


Here is the form:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Sandbox
{
public partial class UserControlTestForm : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
}
private void InitializeComponent()
{
if (!IsPostBack)
{
LoadUserControls();
}
else
{
LoadSessionControl();


protected void Page_Load(object sender, EventArgs e)
{
/// Nothing Happens Here

private void LoadUserControls()
{
UserControl temp = (UserControl)
LoadControl("ucUserControlTest.ascx");
this.pForm.Controls.Add(temp);
Session.Add("CurrentFormControl", temp);
}
private void LoadSessionControl()
{
UserControl temp = (UserControl)
Session["CurrentFormControl"];
/// Clears any Controls on Page. Behavior is the same
whether this is here or not.
this.pForm.Controls.Clear();
this.pForm.Controls.Add(temp);

}
}- Hide quoted text -

- Show quoted text -
 

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

Top