This message was discovered on ASPFriends.com 'aspngbeta' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Patrick Barnes (VIP) |
I'm trying to access and use the properties (or public variables) and methods in a page's codebehind from within a usercontrol dynamically loaded into that page. If I declare an instance of the webform's codebehind in my usercontrol's codebehind I can access the page's properties and methods, e.g.:
Protected WithEvents myPage As FooBarWebFormCodebehindClass Protected WithEvents lblEmail As System.Web.UI.WebControls.Label ... lblEmail.Text = myPage.EmailAddress myPage.BN_Submit_Click(sender,e)
All compiles fine. But at runtime I continually get a "dereferencing null object reference" for the webform's codebehind class declared in the usercontrol codebehind. The page's codebehind class never seems to get instantiated. Any ideas?
Patrick Barnes Web Application Developer Geonetric Technologies 200 1st Avenue NE Suite 220 Cedar Rapids, Iowa 52401 P:319.221.1667 / F:319.221.1450 / www.geonetric.com
|
|
| |
| |
| Michael Gaertner (VIP) |
I'm not sure what code is in your main or default page, the one where the controls are loaded. When I load user controls dynamically, it happens in the Page_Init event of the page object. Page_Init happens before Page_Load, and it's how IBuySpy does it.
Maybe that would make a difference.
This is C#, but you get the idea:
string toolName = "sweet77"; void Page_Init(Object sender, EventArgs e) { if (Request.Params["ctrl"] != null) ContentPane.Controls.Add(Page.LoadControl(Request.Params["ctrl"])); else ContentPane.Controls.Add(Page.LoadControl( toolName + "/" + toolName + "home.ascx" ) ); }
The code checks for a ctrl value in the query string. If it's there, it loads that control, if not, a default control is loaded.
Good luck.
Michael
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:42 PM To: aspngbeta Subject: [aspngbeta] How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm trying to access and use the properties (or public variables) and methods in a page's codebehind from within a usercontrol dynamically loaded into that page. If I declare an instance of the webform's codebehind in my usercontrol's codebehind I can access the page's properties and methods, e.g.:
Protected WithEvents myPage As FooBarWebFormCodebehindClass Protected WithEvents lblEmail As System.Web.UI.WebControls.Label ... lblEmail.Text = myPage.EmailAddress myPage.BN_Submit_Click(sender,e)
All compiles fine. But at runtime I continually get a "dereferencing null object reference" for the webform's codebehind class declared in the usercontrol codebehind. The page's codebehind class never seems to get instantiated. Any ideas?
Patrick Barnes Web Application Developer Geonetric Technologies 200 1st Avenue NE Suite 220 Cedar Rapids, Iowa 52401 P:319.221.1667 / F:319.221.1450 / www.geonetric.com
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
|
|
| |
|
| |
| Patrick Barnes (VIP) |
I appreciate your detailed reply, Michael. I do load the usercontrol in the webform's Init Event. It will load fine. But, for example, when I click on a button in my usercontrol, I want the button to be handled by an event handler on the webform. So I do this in the usercontrol codebehind, where pgMyHealthFilters is the instance of my webform codebehind class:
Sub BN_Update_Click(ByVal sender As Object, ByVal e As EventArgs)
pgMyHealthFilters.BN_Update_Click(sender, e)
End Sub
When I click on the button, I get a "dereference null object reference". So I am trying to do something different than the IBuySpy example. I am trying to programmatically access properties and methods on the parent page from a usercontrol. Any other ideas?
Thanks,
Patrick
-----Original Message----- From: Michael Gaertner [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:39 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm not sure what code is in your main or default page, the one where the controls are loaded. When I load user controls dynamically, it happens in the Page_Init event of the page object. Page_Init happens before Page_Load, and it's how IBuySpy does it.
Maybe that would make a difference.
This is C#, but you get the idea:
string toolName = "sweet77"; void Page_Init(Object sender, EventArgs e) { if (Request.Params["ctrl"] != null) ContentPane.Controls.Add(Page.LoadControl(Request.Params["ctrl"])); else ContentPane.Controls.Add(Page.LoadControl( toolName + "/" + toolName + "home.ascx" ) ); }
The code checks for a ctrl value in the query string. If it's there, it loads that control, if not, a default control is loaded.
Good luck.
Michael
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:42 PM To: aspngbeta Subject: [aspngbeta] How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm trying to access and use the properties (or public variables) and methods in a page's codebehind from within a usercontrol dynamically loaded into that page. If I declare an instance of the webform's codebehind in my usercontrol's codebehind I can access the page's properties and methods, e.g.:
Protected WithEvents myPage As FooBarWebFormCodebehindClass Protected WithEvents lblEmail As System.Web.UI.WebControls.Label ... lblEmail.Text = myPage.EmailAddress myPage.BN_Submit_Click(sender,e)
All compiles fine. But at runtime I continually get a "dereferencing null object reference" for the webform's codebehind class declared in the usercontrol codebehind. The page's codebehind class never seems to get instantiated. Any ideas?
Patrick Barnes Web Application Developer Geonetric Technologies 200 1st Avenue NE Suite 220 Cedar Rapids, Iowa 52401 P:319.221.1667 / F:319.221.1450 / www.geonetric.com
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
|
|
| |
|
| |
| Michael Gaertner (VIP) |
Take a look at this post from S. Warren about raising events from user controls. I don't completely understand it, but I WAS able to use it in my pages:
Did you know you can raise events from a user control? Here's a little sample, LabelButton.ascx (essentially the equivalent of asp:LinkButton), that demonstrates implementing a postback event like click. Controls that fires postback events must implement the IPostBackEventHandler interface -- see the first directive of the user control. Cool, huh?
LabelButton.ascx
[Original message clipped]
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 4:20 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I appreciate your detailed reply, Michael. I do load the usercontrol in the webform's Init Event. It will load fine. But, for example, when I click on a button in my usercontrol, I want the button to be handled by an event handler on the webform. So I do this in the usercontrol codebehind, where pgMyHealthFilters is the instance of my webform codebehind class:
Sub BN_Update_Click(ByVal sender As Object, ByVal e As EventArgs)
pgMyHealthFilters.BN_Update_Click(sender, e)
End Sub
When I click on the button, I get a "dereference null object reference". So I am trying to do something different than the IBuySpy example. I am trying to programmatically access properties and methods on the parent page from a usercontrol. Any other ideas?
Thanks,
Patrick
-----Original Message----- From: Michael Gaertner [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:39 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm not sure what code is in your main or default page, the one where the controls are loaded. When I load user controls dynamically, it happens in the Page_Init event of the page object. Page_Init happens before Page_Load, and it's how IBuySpy does it.
Maybe that would make a difference.
This is C#, but you get the idea:
string toolName = "sweet77"; void Page_Init(Object sender, EventArgs e) { if (Request.Params["ctrl"] != null) ContentPane.Controls.Add(Page.LoadControl(Request.Params["ctrl"])); else ContentPane.Controls.Add(Page.LoadControl( toolName + "/" + toolName + "home.ascx" ) ); }
The code checks for a ctrl value in the query string. If it's there, it loads that control, if not, a default control is loaded.
Good luck.
Michael
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:42 PM To: aspngbeta Subject: [aspngbeta] How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm trying to access and use the properties (or public variables) and methods in a page's codebehind from within a usercontrol dynamically loaded into that page. If I declare an instance of the webform's codebehind in my usercontrol's codebehind I can access the page's properties and methods, e.g.:
Protected WithEvents myPage As FooBarWebFormCodebehindClass Protected WithEvents lblEmail As System.Web.UI.WebControls.Label ... lblEmail.Text = myPage.EmailAddress myPage.BN_Submit_Click(sender,e)
All compiles fine. But at runtime I continually get a "dereferencing null object reference" for the webform's codebehind class declared in the usercontrol codebehind. The page's codebehind class never seems to get instantiated. Any ideas?
Patrick Barnes Web Application Developer Geonetric Technologies 200 1st Avenue NE Suite 220 Cedar Rapids, Iowa 52401 P:319.221.1667 / F:319.221.1450 / www.geonetric.com
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
|
|
| |
|
| |
| Stuart C. Salsbury |
If you hard code the type of the page that will host a control into the control, what are you gaining by building a control? There is one circumstance where I can imagine benefitting from buidling such a control: if the parent page type is actually an abstract type that is providing relatively generic services to its child control
What does the CLR do with circular references during tear down, and doesn't holding the page instance in a control (and of course the page is indirectly holding a reference to the control) pose risks here? Or does the framework take care of this in the teardown of pages?
Wouldn't it be more graceful to raise a new server to server from the control to the page in response to the event raised from the browser to the control? I think M. Gaertner was suggesting this. This would eliminate the costs of building into your control dependence on a particular page type, as well as any circular references problems that might crop up.
Regards, Stuart Salsbury Ernst & Young LLP
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 4:20 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I appreciate your detailed reply, Michael. I do load the usercontrol in the webform's Init Event. It will load fine. But, for example, when I click on a button in my usercontrol, I want the button to be handled by an event handler on the webform. So I do this in the usercontrol codebehind, where pgMyHealthFilters is the instance of my webform codebehind class:
Sub BN_Update_Click(ByVal sender As Object, ByVal e As EventArgs)
pgMyHealthFilters.BN_Update_Click(sender, e)
End Sub
When I click on the button, I get a "dereference null object reference". So I am trying to do something different than the IBuySpy example. I am trying to programmatically access properties and methods on the parent page from a usercontrol. Any other ideas?
Thanks,
Patrick
-----Original Message----- From: Michael Gaertner [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:39 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm not sure what code is in your main or default page, the one where the controls are loaded. When I load user controls dynamically, it happens in the Page_Init event of the page object. Page_Init happens before Page_Load, and it's how IBuySpy does it.
Maybe that would make a difference.
This is C#, but you get the idea:
string toolName = "sweet77"; void Page_Init(Object sender, EventArgs e) { if (Request.Params["ctrl"] != null)
ContentPane.Controls.Add(Page.LoadControl(Request.Params["ctrl"])); else ContentPane.Controls.Add(Page.LoadControl( toolName + "/" + toolName + "home.ascx" ) ); }
The code checks for a ctrl value in the query string. If it's there, it loads that control, if not, a default control is loaded.
Good luck.
Michael
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:42 PM To: aspngbeta Subject: [aspngbeta] How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm trying to access and use the properties (or public variables) and methods in a page's codebehind from within a usercontrol dynamically loaded into that page. If I declare an instance of the webform's codebehind in my usercontrol's codebehind I can access the page's properties and methods, e.g.:
Protected WithEvents myPage As FooBarWebFormCodebehindClass Protected WithEvents lblEmail As System.Web.UI.WebControls.Label ... lblEmail.Text = myPage.EmailAddress myPage.BN_Submit_Click(sender,e)
All compiles fine. But at runtime I continually get a "dereferencing null object reference" for the webform's codebehind class declared in the usercontrol codebehind. The page's codebehind class never seems to get instantiated. Any ideas?
Patrick Barnes Web Application Developer Geonetric Technologies 200 1st Avenue NE Suite 220 Cedar Rapids, Iowa 52401 P:319.221.1667 / F:319.221.1450 / www.geonetric.com
|
|
| |
|
| |
| Patrick Barnes (VIP) |
Stuart,
I think you are on target here. Someone else I've been communicating with pointed me in this direction, too. I thus decided to raise an event in the usercontrol and then have it be handled on the webform. I think that's a good solution. Now I just have to figure out how to do that : - ).
Thanks for your input,
Patrick
-----Original Message----- From: Stuart C. Salsbury [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 5:52 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
If you hard code the type of the page that will host a control into the control, what are you gaining by building a control? There is one circumstance where I can imagine benefitting from buidling such a control: if the parent page type is actually an abstract type that is providing relatively generic services to its child control
What does the CLR do with circular references during tear down, and doesn't holding the page instance in a control (and of course the page is indirectly holding a reference to the control) pose risks here? Or does the framework take care of this in the teardown of pages?
Wouldn't it be more graceful to raise a new server to server from the control to the page in response to the event raised from the browser to the control? I think M. Gaertner was suggesting this. This would eliminate the costs of building into your control dependence on a particular page type, as well as any circular references problems that might crop up.
Regards, Stuart Salsbury Ernst & Young LLP
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 4:20 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I appreciate your detailed reply, Michael. I do load the usercontrol in the webform's Init Event. It will load fine. But, for example, when I click on a button in my usercontrol, I want the button to be handled by an event handler on the webform. So I do this in the usercontrol codebehind, where pgMyHealthFilters is the instance of my webform codebehind class:
Sub BN_Update_Click(ByVal sender As Object, ByVal e As EventArgs)
pgMyHealthFilters.BN_Update_Click(sender, e)
End Sub
When I click on the button, I get a "dereference null object reference". So I am trying to do something different than the IBuySpy example. I am trying to programmatically access properties and methods on the parent page from a usercontrol. Any other ideas?
Thanks,
Patrick
-----Original Message----- From: Michael Gaertner [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:39 PM To: aspngbeta Subject: [aspngbeta] RE: How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm not sure what code is in your main or default page, the one where the controls are loaded. When I load user controls dynamically, it happens in the Page_Init event of the page object. Page_Init happens before Page_Load, and it's how IBuySpy does it.
Maybe that would make a difference.
This is C#, but you get the idea:
string toolName = "sweet77"; void Page_Init(Object sender, EventArgs e) { if (Request.Params["ctrl"] != null)
ContentPane.Controls.Add(Page.LoadControl(Request.Params["ctrl"])); else ContentPane.Controls.Add(Page.LoadControl( toolName + "/" + toolName + "home.ascx" ) ); }
The code checks for a ctrl value in the query string. If it's there, it loads that control, if not, a default control is loaded.
Good luck.
Michael
-----Original Message----- From: Patrick Barnes [mailto:Click here to reveal e-mail address] Sent: Thursday, May 03, 2001 2:42 PM To: aspngbeta Subject: [aspngbeta] How do I access parent page properties and methods from one of its dynamically loaded usercontrols?
I'm trying to access and use the properties (or public variables) and methods in a page's codebehind from within a usercontrol dynamically loaded into that page. If I declare an instance of the webform's codebehind in my usercontrol's codebehind I can access the page's properties and methods, e.g.:
Protected WithEvents myPage As FooBarWebFormCodebehindClass Protected WithEvents lblEmail As System.Web.UI.WebControls.Label ... lblEmail.Text = myPage.EmailAddress myPage.BN_Submit_Click(sender,e)
All compiles fine. But at runtime I continually get a "dereferencing null object reference" for the webform's codebehind class declared in the usercontrol codebehind. The page's codebehind class never seems to get instantiated. Any ideas?
Patrick Barnes Web Application Developer Geonetric Technologies 200 1st Avenue NE Suite 220 Cedar Rapids, Iowa 52401 P:319.221.1667 / F:319.221.1450 / www.geonetric.com
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
|
|
| |
|
|
|
|
|