Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Another dynamic usercontrol issue

Reply
Thread Tools

Another dynamic usercontrol issue

 
 
Bryan
Guest
Posts: n/a
 
      12-07-2004
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


 
Reply With Quote
 
 
 
 
Paul Hatcher
Guest
Posts: n/a
 
      12-07-2004
Bryan

Try moving the code to the CreateChildControls method of the page which is
overridable

Paul

"Bryan" <> wrote in message
news:OL7AXg$...
> 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
>
>



 
Reply With Quote
 
 
 
 
Bryan
Guest
Posts: n/a
 
      12-08-2004
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.



"Paul Hatcher" <> wrote in message
news:OXCz%...
> Bryan
>
> Try moving the code to the CreateChildControls method of the page which is
> overridable
>
> Paul
>
> "Bryan" <> wrote in message
> news:OL7AXg$...
>> 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
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting a property of a control inside a usercontrol, from another usercontrol Fabiano ASP .Net 2 06-03-2005 09:56 AM
Another dynamic usercontrol issue Bryan ASP .Net Web Controls 1 12-08-2004 08:24 PM
Another dynamic usercontrol issue Bryan ASP .Net 3 12-08-2004 08:24 PM
accessing usercontrol from another usercontrol Phl ASP .Net 2 11-18-2004 07:33 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57