all controls are left uninitialized in user control

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
LVL 13
davidlars99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Bob LearnedCommented:
>>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
 
davidlars99Author Commented:
No, this is not master page, and yes, it's 2.0.

These controls are defined as markups within the ASCX file.
0
 
Bob LearnedCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
davidlars99Author Commented:
>> 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
 
davidlars99Author Commented:
user control loading can also happen in Page_Init, but the end result is same.
0
 
Bob LearnedCommented:
Are you using 2005 SP1?

Bob
0
 
davidlars99Author Commented:
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
 
Bob LearnedCommented:
This is for #1:
   Control c = this.LoadControl("MyControl.ascx");

How did you do it for #2?

Bob
0
 
davidlars99Author Commented:


this.LoadControl(typeof(MyControl), new object[] { });
0
 
davidlars99Author Commented:
or

this.LoadControl(typeof(MyControl), null);
0
 
Bob LearnedCommented:
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
 
davidlars99Author Commented:
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
 
Bob LearnedCommented:
>>but I can't get it to work...
What have you tried?

Bob
0
 
davidlars99Author Commented:
>> 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
 
Bob LearnedCommented:
Dude, this ain't my problem.  I was looking for what you tried, so I could point you in the right direction.

Bob
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
davidlars99Author Commented:
Excellent answer!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.