Hi all, I have a couple of questions about User Controls. Basically i've adopted an approach of creating a base page from which all other pages will inherit. In the base page I programatically build up a table structure with placeholders. All other pages inherit
from this base page, and depending on what they do they load up specific user controls, such as a login control or a quicksearch control into the main page body. My questions are 1) What determines whether viewstate is enabled, the parent page or the loaded
user control? If a control, say a button, within my user control has viewstate=true, but the page has viewstate=false...what happens, which takes priority? 2) Where is the best place to deal with events. Say I have a login page derived from the base page,
which loads a login control into the page body. If the user enters an invalid username or password then fair enough, the user control displays the errors. However, if the login is correct, should the user control transfer to a new main page or should it notify
its host page which will make the transfer? Whats your thoughts? Whats the best practice in this case 3) Do user controls have constructors or can you only set values through properties? Say I have a menu that configures itself depending on the users permission.
Can I set a value during the loadcontrol or can I only set it after loading the control and before adding to a placeholder? 4) Im used to using ActiveX controls, ASP includes and server.executes. Have I got things wrong because I think of user controls as
separate entities within a page and should I, instead, be thinking of the page as one complete entity? Do all the methods & properties become merged with those of the base page? 5) Has anyone else developed a templating system. I can either progress in one
of two ways. The first is that I have one page that loads the required controls. This main page does everything and all work is routed through it. I would identify the control to load via a select case of IF against the querystring, i.e
Dim myControl1 As MyControl
If Request.Querystring("Page")="Quicksearch" Then
myControl1 = CType(LoadControl("Login.ascx"),MyControl1)
phBodyPlaceHolder.Controls.Add(myControl1)
End If
The second methos
is to have 1 page for each section, i.e a Login.aspx with derives from Basepage and loads the login.ascx, a quickserach.aspx that derives from basepage and loads the quicksearch.ascx. In this scenario I have 1 logical page for each section. Sure I have more
pages but I feel its a bit cleaner that the first example, especially if the site grows. Does anyone know any better practices than this one? Cheers all Davey
daveym
Member
200 Points
40 Posts
All about User Controls
Aug 06, 2003 03:18 PM|LINK
Dim myControl1 As MyControl If Request.Querystring("Page")="Quicksearch" Then myControl1 = CType(LoadControl("Login.ascx"),MyControl1) phBodyPlaceHolder.Controls.Add(myControl1) End IfThe second methos is to have 1 page for each section, i.e a Login.aspx with derives from Basepage and loads the login.ascx, a quickserach.aspx that derives from basepage and loads the quicksearch.ascx. In this scenario I have 1 logical page for each section. Sure I have more pages but I feel its a bit cleaner that the first example, especially if the site grows. Does anyone know any better practices than this one? Cheers all Davey