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
I can answer some of these.. =) I've run into the same problem (having common aspects of pages that I want them all to share), but I've implemented this by making the common aspects user controls rather than the other way around (so, I have a control for the
Heading, Navigation, etc, and then my individual pages contain these controls at the right places). Most of my events, therefore, end up in the pages (with the unique controls), and my standard user controls usually don't end up with too much in them. User
Controls do have constructors, but if you add them to a page declaratively (in the HTML), you don't really have access to them. It's easy, however, to define public attributes in your user control, which you can then assign values in your HTML. Quick Sample:
Navigation.ascx
<script runat="server">
Public CurrentPage As String
...
</script>
...
MainPage.aspx
...
<form runat="server">
...
User Controls are separate classes from the page they reside in, and it's probably a good idea to treat them as isolated classes for the most part. In some cases, you may create a user control which is really just a set of controls, and in that case
the page ends up more directly interacting with the control. Otherwise, you just set properties and call methods. Hope this helps! Let me know if there's something I've left unclear. You've covered a lot of ground in your question. -Scott
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Scott, Cheers for your reply, it makes sense. I happened to come across a chapter on user controls in a book I have and it enlightened me about it all. The one thing I hadnt been doing is assigning my control to a variable before adding it to a placeholder,
ie
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 DaveyScott Louvau
Participant
1347 Points
267 Posts
Microsoft
Re: All about User Controls
Aug 07, 2003 01:52 AM|LINK
daveym
Member
200 Points
40 Posts
Re: All about User Controls
Aug 07, 2003 08:09 AM|LINK