Calling metod on aspx page from ascx control
I have a aspx page default.aspx on this page is a panel control and I load ascx user controls in to the panel. Like this.
Code:
Public Sub loadpanel(ByVal filename As String)
Session("page") = filename
'If File.Exists(Request.MapPath(fileName)) Then
Dim demo As System.Web.UI.Control = Page.LoadControl(filename)
Panel1.Controls.Add(demo)
'End If
End Sub
That is all working, But! Now I need to call the sub loadpanel from the ascx page so I can load a new ascx usercontrol on a button click. Something like this
Code on the ascx page to call the sub on the default.aspx
Code:
Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As
Loadpanel(“top.ascx”) ‘ This sub is on the deafult.aspx page
End Sub
This is not the correct syntacs so how do I do this??
Re: Calling metod on aspx page from ascx control
You won't be able to call a method like that and you really shouldn't. You should design your architecture so each reference can work without needing to call a method on a parent object (that's kind of awkward, in my opinion).
What you should do is one of two things.
1. Is this button part of the page rather than the control (or, should it be)? If the answer is yes then remove the button from the control, add it to the page and you're on your way.
2. If this control is separate or contained then the control should be adding this new control inside of itself. Either put the entire control into a Panel so you can add the additional control to it or design it so you can do this.
Re: Calling metod on aspx page from ascx control
OK, that was kind of god and bad news!
This lead me to ask you all what you think of using callback panels on a web site, this is how I plan the site.
One Mater page.
One default page with a callback panel that is loading user controls.
Many ascx pages to be loaded in to the dafults callback panle.
What do you think about a site like that? Could there be any problem with it? As I see it the load on the server and the loading time for the pages will be less than a normal site since only the user control will be loaded on page change.
But the problem as I see it is that many times is it needed for the user control to change page inside the callback panel that is on the default.aspx page. Example. I have a user control with a simple messaging system once the user like to replay to a mail a new user control need to be loaded, so how can I do that or is the whole site a really bad idée???
Re: Calling metod on aspx page from ascx control
I would do multiple pages with page specific user controls. It could be, however, that your application should be structured in this way depending on what it does.
To avoid the post back issue you could use the AJAX control toolkit and the ajax web extensions.
Re: Calling metod on aspx page from ascx control
Remember that a control is meant to be reusable, so it shouldn't be aware of the page it is sitting in. The page can be aware of the controls it houses, though, because it is the parent and the controls define the page.
I'd encapsulate the entire 'messaging' functionality in a single control (I'm going with the assumption that it is a little shoutbox or chat app).