469,568 Members | 1,401 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

unable to access User Control data HELP


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
(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


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

//Load user controls
//load the User control and cast it to the variable type
userTest = (local.NewReg.webuser)Page.LoadControl("MyUserCont rol.ascx");
//add to the placeholder - will be stored as an object of type

//we also tried this...
//c is local but we will access via a class scope variable..
//Control c = Page.LoadControl("MyUserControl.ascx");

//and we also tried this..
//userTest= Page.LoadControl("/NewReg/MyUserControl.ascx");


public void submitClick(object Source, ImageClickEventArgs e)

//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 =
person.firstName = userTest.FirstName;

//or accessing the variable directly throws a
//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.

/// <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);

Nov 18 '05 #1
2 2754
Hi paul,

Thank you for using Microsoft Newsgroup Service. Based on your description,
you are trying to create an instance of a ASCX UserControl and add it into
a PlaceHolder control on an ASPX page programtically. However, you found
that it seems always nothing was in the PlaceHolder or event the
UserControl was not loaded correctly. Please correct me if my understanding
is not quite exact.

After reviewing the code you provided, I think I need to know something
else about your webpage and the UserControl:

1.Is the ASCX control and the containging page in the same web
project(using the same language to program)?

2.Are there any other UserControl in the same web project? Do they work

3.Since you can't dynamically loaded a control onto a page programtically,
have you tried adding the same ascx control just by draging the ascx page
onto the containing page in the VS.NET IDE? If there is no problem to add
the control this way, I think the problem is not at the UserControl, do you
think so?

4. Does your UserControl(ascx) have a CodeBehind ascx.cs file with it or
just have one ascx page and with all the code in the <script runat=server>
block in it? Since the two different style UserControl need to set
different kind of <@control> directive. If you used a CodeBehind
file(generated by VS.NET) that the <@control> directive will be created by
default such as
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="MyCodeBehind.ascx.cs" Inherits="MyWebApp.MyCodeBehind" %>
And since the control's class will be build into web app's application
assemble, you can easily reference the type in other pages. Otherwise, if
your ascx UserControl doesn't have a CodeBehind page, you need to add a
"ClassName" attribute in the <@Control > directive. just as :

<%@ Control Language="c#" ClassName="SimpleUC" %>

Also, the way how to add the two style of UserControl into a page are
different, to make sure , you can add both the directive in your aspx page
as below:
<%@ Reference Control="MyCodeBehind.ascx" %>
<%@ Register TagPrefix="uc1" TagName="MyCodeBehind" Src="MyCodeBehind.ascx"

If you feel my description not quite clear, you can view the relative
article on MSDN:
The article there shows many techniques about using a UserControl(
including how to programtically create a instance of a UserControl)

If the above info didn't help, I think you may try creating a new Simple
UserControl, I suggest that you reference the following linK:

There is a good example of a UserControl. Please try whether such a generic
UserControl can work?
Please try my preceding suggestions and let me know whether they will help.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no

Nov 18 '05 #2
Hi paul,

Have you had a chance to try my suggestion or have you resolved your
problem? Please let me know if you need
any help, Thanks.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

49 posts views Thread by Yannick Turgeon | last post: by
1 post views Thread by ASP.Net programmer | last post: by
1 post views Thread by Reza Sadeghi | last post: by
16 posts views Thread by Serdar Kalaycý | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.