?
Solved

all controls are left uninitialized in user control

Posted on 2007-08-08
16
Medium Priority
?
389 Views
Last Modified: 2008-02-01
Hi,

This is a very strange situation; I created user control (ascx) and put some controls in it (i.e. grid, button etc.), when I try to access those controls in user control's Page_Load event, they are all null... How is this possible, and what's going on?... I'm setting all the attributes correctly... :(


Thanks,
David
0
Comment
Question by:davidlars99
  • 9
  • 7
16 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19659042
>>when I try to access those controls in user control's Page_Load event
How are you loading the controls, and trying to access them?  Is this 2.0 with Master Pages?

Bob
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19659331
No, this is not master page, and yes, it's 2.0.

These controls are defined as markups within the ASCX file.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19660879
David,

I still don't see enough detail to suggest anything.  What is your approach?  What does the code look like (if it is not too much)?

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Author Comment

by:davidlars99
ID: 19662045
>> I still don't see enough detail to suggest anything.

Because there isn't any...  :-) It is as simple as this :)


[inside ASCX]:

<%@ Control Language="C#" CodeBehind="MyControl.ascx.cs" Inherits="MyApp.MyControl" %>

<asp:GridView ID="myGrid" runat="server">
   <Columns>
   </Columns>
</asp:GridView>

<asp:TextBox ID="TextBox1" Text="Sample Text" runat="server" />



[inside ASCX's code-behind]:

protected void Page_Load(object sender, EventArgs e)
{
   // I place the breakpoint here

   GridView g = this.myGrid;
 
   // variable "g" is always null...
}



[inside Page's code-behind]:

protected void Page_Load(object sender, EventArgs e)
{
   Control c = this.LoadControl("MyControl.ascx");
   this.PanelControl.Controls.Add(c);
}
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19662082
user control loading can also happen in Page_Init, but the end result is same.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19662610
Are you using 2005 SP1?

Bob
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19663238
Bob,

Let me re-phrase what I've said before. Problem is somewhat different, and it is directly related to one of the method overloads...

There are two overloads of LoadControl:

1  public Control LoadControl(string virtualPath)
2. public Control LoadControl(Type t, Object[] parameters)

If I use the first one, then everything work, but when I'm using the second one, aforementioned problem arises  :(

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19663371
This is for #1:
   Control c = this.LoadControl("MyControl.ascx");

How did you do it for #2?

Bob
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19664044


this.LoadControl(typeof(MyControl), new object[] { });
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19664048
or

this.LoadControl(typeof(MyControl), null);
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19670665
I don't see anything wrong with either form.

This is what the framework does:

public Control LoadControl(Type t, object[] parameters)
{
    return this.LoadControl(null, null, t, parameters);
}

public Control LoadControl(string virtualPath)
{
    return this.LoadControl(VirtualPath.Create(virtualPath));
}

internal Control LoadControl(VirtualPath virtualPath)
{
    virtualPath = VirtualPath.Combine(base.TemplateControlVirtualDirectory, virtualPath);
    BuildResult vPathBuildResult = BuildManager.GetVPathBuildResult(this.Context, virtualPath);
    return this.LoadControl((IWebObjectFactory) vPathBuildResult, virtualPath, null, null);
}

They are divergent paths, so I could see a bug possibility here.

Bob
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19675959
This is defenately a bug. Take a look at this article, it explains how to do workaround, but I can't get it to work...

http://odetocode.com/Blogs/scott/archive/2005/10/01/2293.aspx
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19676951
>>but I can't get it to work...
What have you tried?

Bob
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19677570
>> What have you tried?

What have you tried? Have you actually tried the example given by Scott Allen?... If not, please use the link I provided...
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19691335
Dude, this ain't my problem.  I was looking for what you tried, so I could point you in the right direction.

Bob
0
 
LVL 13

Author Comment

by:davidlars99
ID: 19696221
Excellent answer!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question