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

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

P: n/a
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);

}

}
}

Oct 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)
HammRadio wrote:
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);

}

}
}
Oct 24 '07 #2

P: n/a
Thanks bruce.

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

On Oct 23, 9:29 pm, bruce barker <nos...@nospam.comwrote:
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)

HammRadio wrote:
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 -

Oct 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.