P
paul meaney
All,
myself and another developer have been staring blankly at a screen for
the past 48 hours and are wondering just what stunningly obvious thing
we are missing.
We are trying to load up 2 or more user controls dynamically by adding
to a placeholder defined in page_load. I've included the sample code
for how we are accessing one. The user controls are not rocket science
- just a few text boxes with public accessor properties. We've trawled
through all the newsgroups and have:
(1) declared class scope variables that are visible throughout the
class
(2) declared the User Controls outside of an isPostBack check so they
are always created
(3) defined public get and set methods in each user control to the
container page can access the data.
(4) we have made sure that we are casting object to the correct type
coming in and out of the Placeholder collection
when we build and step through we find that when we enter the
onClick() function if we try to access the placeholder collection, we
get an IndexOutOfBounds exception - the collection has NO ELEMENTS ANY
MORE!!! If we try to access the User Control by means of it's class
scope variable we get a NullReference Exception.
what are we missing - this is driving us nuts......
sample code to follow,
many thanks
Paul.
namespace local.NewReg
{
//all our usings
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
/// <summary>
/// Summary description for registration.
/// </summary>
public class registration : System.Web.UI.Page
{
//class scope placeholder
protected System.Web.UI.WebControls.PlaceHolder personal;
//class scope variable for our User Control
protected local.NewReg.webuser userTest;
//class scope declaration of a Csharp server object
protectedQA.Person.WebUserPerson person = null;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//outside of a postback command so that the controls are always
loaded
//Load user controls
//load the User control and cast it to the variable type
userTest = (local.NewReg.webuser)Page.LoadControl("MyUserControl.ascx");
//add to the placeholder - will be stored as an object of type
Control
personal.Controls.Add(userTest);
//we also tried this...
//c is local but we will access via a class scope variable..
//Control c = Page.LoadControl("MyUserControl.ascx");
//personal.Controls.Add(c);
//and we also tried this..
//userTest= Page.LoadControl("/NewReg/MyUserControl.ascx");
//Personal.Controls.Add(userTest);
}
public void submitClick(object Source, ImageClickEventArgs e)
{
try
{
//get access to out webuser
person = new QA.Person.WebUserPerson();
//..now try to get access to the user control from the
submitClick() function
//doing this throws an indexOurOfRangeException
//which is weird because it should have one control in the
collection as defined in page_load
userTest =
(local.NewReg.webuser)Personal.Controls[0];
person.firstName = userTest.FirstName;
//or accessing the variable directly throws a
NullreferenceException
//which I don't get because we have
instantiated the variable in page_load
person.firstName = userTest.FirstName;
}
catch(Exception exc)
{// handle errors gracefully}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
myself and another developer have been staring blankly at a screen for
the past 48 hours and are wondering just what stunningly obvious thing
we are missing.
We are trying to load up 2 or more user controls dynamically by adding
to a placeholder defined in page_load. I've included the sample code
for how we are accessing one. The user controls are not rocket science
- just a few text boxes with public accessor properties. We've trawled
through all the newsgroups and have:
(1) declared class scope variables that are visible throughout the
class
(2) declared the User Controls outside of an isPostBack check so they
are always created
(3) defined public get and set methods in each user control to the
container page can access the data.
(4) we have made sure that we are casting object to the correct type
coming in and out of the Placeholder collection
when we build and step through we find that when we enter the
onClick() function if we try to access the placeholder collection, we
get an IndexOutOfBounds exception - the collection has NO ELEMENTS ANY
MORE!!! If we try to access the User Control by means of it's class
scope variable we get a NullReference Exception.
what are we missing - this is driving us nuts......
sample code to follow,
many thanks
Paul.
namespace local.NewReg
{
//all our usings
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
/// <summary>
/// Summary description for registration.
/// </summary>
public class registration : System.Web.UI.Page
{
//class scope placeholder
protected System.Web.UI.WebControls.PlaceHolder personal;
//class scope variable for our User Control
protected local.NewReg.webuser userTest;
//class scope declaration of a Csharp server object
protectedQA.Person.WebUserPerson person = null;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//outside of a postback command so that the controls are always
loaded
//Load user controls
//load the User control and cast it to the variable type
userTest = (local.NewReg.webuser)Page.LoadControl("MyUserControl.ascx");
//add to the placeholder - will be stored as an object of type
Control
personal.Controls.Add(userTest);
//we also tried this...
//c is local but we will access via a class scope variable..
//Control c = Page.LoadControl("MyUserControl.ascx");
//personal.Controls.Add(c);
//and we also tried this..
//userTest= Page.LoadControl("/NewReg/MyUserControl.ascx");
//Personal.Controls.Add(userTest);
}
public void submitClick(object Source, ImageClickEventArgs e)
{
try
{
//get access to out webuser
person = new QA.Person.WebUserPerson();
//..now try to get access to the user control from the
submitClick() function
//doing this throws an indexOurOfRangeException
//which is weird because it should have one control in the
collection as defined in page_load
userTest =
(local.NewReg.webuser)Personal.Controls[0];
person.firstName = userTest.FirstName;
//or accessing the variable directly throws a
NullreferenceException
//which I don't get because we have
instantiated the variable in page_load
person.firstName = userTest.FirstName;
}
catch(Exception exc)
{// handle errors gracefully}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}