473,399 Members | 4,192 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,399 software developers and data experts.

Another dynamic usercontrol issue

Hello All,

I have a page that dynamically adds one of many usercontrols to a
placeholder on the main page based on a parameter passed to the page.

Here is my problem.

The usercontrol may have something as simple as a label or a datagrid and I
get the same error.

The control renders in the designer and the code has the object declared.
Basically everything is were it should be. Once you run the page and try to
assign text to a label or bind data to a data grid you get a "Object
reference not set to an instance of an object." error. What the heck is
going on? This control works in a page that doesn't load it dynamic.

Does anyone have any ideas?

-----------------------------
MyControl.ascx - Code
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="floorplans.ascx.vb" Inherits="Tandem.floorplans"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div id="text">
<asp:Label id="Label1" runat="server"></asp:Label>
</div>

-----------------------------
MyControl.ascx.vb - Code

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
LoadContent()
End If
End Sub

Private Sub LoadContent()
Label1.Text = "Some Text"
End Sub

-----------------------------
This is the code that adds the control on the page side. The function that
calls the following code is in the onload event of the page

Dim ctl As New MyConrol

ctl.PropertyID = PropertyID
Page.LoadControl("controls/MyControl.ascx")
phControl.Controls.Add(ctl)
Thanks,
B

Nov 19 '05 #1
3 1513
Hello Bryan,

You need to do

ctl = Page.LoadControl("controls/MyControl.ascx")
ctl.PropertyID = PropertyID

phControl.Controls.Add(ctl)

Page.LoadControl returns a Control. This is what you need to add to your
controls collection.

--
Matt Berther
http://www.mattberther.com
Hello All,

I have a page that dynamically adds one of many usercontrols to a
placeholder on the main page based on a parameter passed to the page.

Here is my problem.

The usercontrol may have something as simple as a label or a datagrid
and I get the same error.

The control renders in the designer and the code has the object
declared. Basically everything is were it should be. Once you run the
page and try to assign text to a label or bind data to a data grid you
get a "Object reference not set to an instance of an object." error.
What the heck is going on? This control works in a page that doesn't
load it dynamic.

Does anyone have any ideas?

-----------------------------
MyControl.ascx - Code
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="floorplans.ascx.vb" Inherits="Tandem.floorplans"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div id="text">
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
-----------------------------
MyControl.ascx.vb - Code
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
LoadContent()
End If
End Sub
Private Sub LoadContent()
Label1.Text = "Some Text"
End Sub
-----------------------------
This is the code that adds the control on the page side. The function
that
calls the following code is in the onload event of the page
Dim ctl As New MyConrol

ctl.PropertyID = PropertyID
Page.LoadControl("controls/MyControl.ascx")
phControl.Controls.Add(ctl)
Thanks,
B

Nov 19 '05 #2
That did the trick!!!

Thanks

"Matt Berther" <mb******@hotmail.com> wrote in message
news:65***********************@news.microsoft.com. ..
Hello Bryan,

You need to do

ctl = Page.LoadControl("controls/MyControl.ascx")
ctl.PropertyID = PropertyID

phControl.Controls.Add(ctl)

Page.LoadControl returns a Control. This is what you need to add to your
controls collection.

--
Matt Berther
http://www.mattberther.com
Hello All,

I have a page that dynamically adds one of many usercontrols to a
placeholder on the main page based on a parameter passed to the page.

Here is my problem.

The usercontrol may have something as simple as a label or a datagrid
and I get the same error.

The control renders in the designer and the code has the object
declared. Basically everything is were it should be. Once you run the
page and try to assign text to a label or bind data to a data grid you
get a "Object reference not set to an instance of an object." error.
What the heck is going on? This control works in a page that doesn't
load it dynamic.

Does anyone have any ideas?

-----------------------------
MyControl.ascx - Code
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="floorplans.ascx.vb" Inherits="Tandem.floorplans"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div id="text">
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
-----------------------------
MyControl.ascx.vb - Code
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
LoadContent()
End If
End Sub
Private Sub LoadContent()
Label1.Text = "Some Text"
End Sub
-----------------------------
This is the code that adds the control on the page side. The function
that
calls the following code is in the onload event of the page
Dim ctl As New MyConrol

ctl.PropertyID = PropertyID
Page.LoadControl("controls/MyControl.ascx")
phControl.Controls.Add(ctl)
Thanks,
B


Nov 19 '05 #3
Here is the resolution for all who would like to know.

Old code:
Dim ctl As New MyConrol
ctl.PropertyID = PropertyID
Page.LoadControl("controls/MyControl.ascx")
phControl.Controls.Add(ctl)
New Code:
Dim ctl As MyConrol
ctl = Page.LoadControl("controls/MyControl.ascx")
ctl.PropertyID = PropertyID
phControl.Controls.Add(ctl)

That was all that needed to be changed.

"Bryan" <bp*****@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello All,

I have a page that dynamically adds one of many usercontrols to a
placeholder on the main page based on a parameter passed to the page.

Here is my problem.

The usercontrol may have something as simple as a label or a datagrid and
I
get the same error.

The control renders in the designer and the code has the object declared.
Basically everything is were it should be. Once you run the page and try
to
assign text to a label or bind data to a data grid you get a "Object
reference not set to an instance of an object." error. What the heck is
going on? This control works in a page that doesn't load it dynamic.

Does anyone have any ideas?

-----------------------------
MyControl.ascx - Code
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="floorplans.ascx.vb" Inherits="Tandem.floorplans"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div id="text">
<asp:Label id="Label1" runat="server"></asp:Label>
</div>

-----------------------------
MyControl.ascx.vb - Code

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
LoadContent()
End If
End Sub

Private Sub LoadContent()
Label1.Text = "Some Text"
End Sub

-----------------------------
This is the code that adds the control on the page side. The function that
calls the following code is in the onload event of the page

Dim ctl As New MyConrol

ctl.PropertyID = PropertyID
Page.LoadControl("controls/MyControl.ascx")
phControl.Controls.Add(ctl)
Thanks,
B

Nov 19 '05 #4

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

Similar topics

13
by: HenryW | last post by:
I am writing a table out to display data from a recordset and my problem is that even though as you can see the table should not exceed that screen size, it does. My users will have to scroll a mile...
0
by: labelle | last post by:
I have two divs on an aspx, and each DIV holds a repeater which holds a UserControl. On the left hand side, I have a "feature list" with an "add to cart" button, and on the right hand side, a...
4
by: Luca | last post by:
HI, I've a asp.net application that add a dynamic usercontrol with a button. If i've tried to click it Click Event don't load. But into a static usercontrol it's happens. Anyone can help me?
0
by: Rob Blij | last post by:
Hi (sorry for repeated question) We have ported a .NET1.1 ASP.NET app to .NET2. All our user controls have been modified and I have found some strange behavior. Firstly I can see that the...
0
by: jobs | last post by:
In the below code, why can't I equate which form to a formview like we do with controls under the main control. The below gives me an error because whichform does not really take all the properties...
0
by: thinkoid | last post by:
Hi all, I have: 1. I have a place holder: phAT 2. I have a usercontrol which has just a datagrid in it. 3. I get data from the database and if get different categories. 4. I bind the datagrid...
1
by: thinkoid | last post by:
Hi all, I have: 1. I have a place holder 2. I have a usercontrol which has just a datagrid in it. 3. I get data from the database and if get different categories. 4. I bind the datagrid with...
2
by: pinson.nick | last post by:
I've been playing around with dynamic tables for the last couple days and have run into some interesting issues. I know how to work around this issue, but was wondering if anyone had any insight as...
31
by: pradeep | last post by:
Hello friends: I have the following code, char* MyClass::MyMethod() { char* ptr = new char; // space for NULL terminator ptr = "hello"; return ptr; delete ptr;
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
tracyyun
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...

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.