472,354 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 2889
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Miller | last post by:
Hi everybody! These messages have been found in the System log of the Event Viewer application. These error messages have been generated after each logon attempt. Although dial-in client have been...
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
by: ASP.Net programmer | last post by:
I already posted this in the ASP.NET framework group, but no one could help me there. Maybe someone in here has encountered this problem: I have a very weird problem with an ASP.NET page I...
by: Reza Sadeghi | last post by:
Hi I am getting this error when I tried to browse any asp.net in visual studio.net. I can build the project but when I try to run and debug the project I get error message that "Unable to start...
by: Serdar Kalaycý | last post by:
Hi everybody, My problem seems a bit clichè but I could not work around. Well I read lots of MSDN papers and discussions, but my problem is a bit different from them. When I tried to run the...
by: robwharram | last post by:
Hi, I'm quite frustrated in the fact that I can't even display a simple "Hello World" message on .Net. I've been through all of the groups and searched all over the place and haven't been able...
by: Josema | last post by:
Hi to all, Im searching a hand to solve an Exception that i get when i try to give the user to a gmail account... Any help would be appreciated.. Thanks in advance This is a piece of my...
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.