473,585 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 IndexOutOfBound s 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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;

/// <summary>
/// Summary description for registration.
/// </summary>
public class registration : System.Web.UI.P age

//class scope placeholder
protected System.Web.UI.W ebControls.Plac eHolder personal;

//class scope variable for our User Control
protected local.NewReg.we buser userTest;

//class scope declaration of a Csharp server object
protectedQA.Per son.WebUserPers on person = null;

private void Page_Load(objec t sender, System.EventArg s 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.w ebuser)Page.Loa dControl("MyUse rControl.ascx") ;
//add to the placeholder - will be stored as an object of type
personal.Contro ls.Add(userTest );

//we also tried this...
//c is local but we will access via a class scope variable..
//Control c = Page.LoadContro l("MyUserContro l.ascx");
//personal.Contro ls.Add(c);

//and we also tried this..
//userTest= Page.LoadContro l("/NewReg/MyUserControl.a scx");
//Personal.Contro ls.Add(userTest );


public void submitClick(obj ect Source, ImageClickEvent Args e)

//get access to out webuser
person = new QA.Person.WebUs erPerson();

//..now try to get access to the user control from the
submitClick() function

//doing this throws an indexOurOfRange Exception
//which is weird because it should have one control in the
collection as defined in page_load
userTest =
(local.NewReg.w ebuser)Personal .Controls[0];
person.firstNam e = userTest.FirstN ame;

//or accessing the variable directly throws a
NullreferenceEx ception
//which I don't get because we have
instantiated the variable in page_load
person.firstNam e = userTest.FirstN ame;

catch(Exception exc)
{// handle errors gracefully}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InitializeCompo nent();

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
this.Load += new System.EventHan dler(this.Page_ Load);

Nov 18 '05 #1
2 3015
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(asc x) 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="MyC odeBehind.ascx. cs" Inherits="MyWeb App.MyCodeBehin d" %>
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="Simp leUC" %>

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="MyCode Behind.ascx" %>
<%@ Register TagPrefix="uc1" TagName="MyCode Behind" Src="MyCodeBehi nd.ascx"

If you feel my description not quite clear, you can view the relative
article on MSDN:
ontrolsprogramm atically.asp?fr ame=true
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:
perties.asp?fra me=true

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 connected to the host pc, But client was unable to open the target host, and its failure attempts have been recorded in the System log. It seems...
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 application is relatively big: around 200 tables, 200 forms and sub-forms, 150 queries and 150 repports, 5GB of data (SQL Server 2000), 40 users. ...
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 developed in Visual Studio .Net 2003: The page has an asp panel which contains a user control (to keep it in a certain place). The User control...
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 debugging on the web server. server side error occurred on sending debug HTTP Request. I am running WinXP Pro SP1 with all hotfixes and VS.NET...
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 project in debug mode (by hitting F5) it gives an error message "Error while trying to run project: Unable to start debugging on the web server.
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 to figure out the solution to this problem. First off, the server is Windows Server 2003 - Web Edition (IIS 6) and I'm using .Net Framework...
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 code: Server=new TcpClient("pop.gmail.com",995); //stablish a connection
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 '/CinemaBookingSystem' Application. -------------------------------------------------------------------------------- ERROR General error Unable to open registry...
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 server mobile ce database. Until recently I was synching everything thru a com port serial cable. The devices would connect to the computer thru...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.