473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

unable to access User Control data HELP

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("MyUserCont rol.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
}
}
Nov 18 '05 #1
2 3002
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
well?

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:
http://msdn.microsoft.com/library/en...antiatinguserc
ontrolsprogrammatically.asp?frame=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:
http://msdn.microsoft.com/library/en...singpageletpro
perties.asp?frame=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
rights.)

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
rights.)
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
49
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...
1
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...
1
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...
16
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...
10
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...
4
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...
0
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...
6
Cintury
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
agi2029
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,...

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.