Dynamic Content


Another way of using content in this way is not so much to promote reuse, but for convenience. Let's consider the case where we have a page that displays several pieces of information, but we don't want them all displayed at once. One way we could achieve that is by wrapping each segment of information in a Panel control, and hiding them or making the visible when appropriate. The disadvantage with this is that the page gets quite long and is harder to edit.

Another issue is that you might like different people to be responsible for those different segments. Having several people responsible for a single page gives too many possibilities for errors as people invariable take copies, and overwrite others' content. So, if we have different people, then wouldn't separate pages be better? Yes, but this might not quite achieve what you want, perhaps keeping a unified look to parts of the page. One way around this is to have each segment of information in a user control, which you can then load on demand.

Let's consider an example using the downloadable sample case study, which is the website of the Cornflakes at Midnight fictitious band that we met previously. Here the band members each have a biography, detailing their ineptitude on a variety of instruments, and their love of strange music. We want the whole biography page to look the same, no matter which bio is being shown, but we also want the members to be able to update their own bio.

Try It Out—Loading User Controls at Run Time

  1. Create a new user control, called dave.ascx.

  2. Add some content to make a biography for Dave. You can copy the one from the samples, or just make up something – he's fairly thick skinned so you can say anything you like about him 8)

  3. Save this page and close it.

  4. Create another user control, called al.ascx, and do the same with the content to create a biography for Alex.

  5. Save this page and close it.

  6. Add a new ASP.NET Page called bio.aspx, and add some title text:

    Band Biography

  7. Underneath that, add two LinkButton controls. Change the Text properties to Alex and Dave.

  8. Underneath the LinkButtons add a PlaceHolder control.

  9. Double-click the first LinkButton (Alex) and add the following code:

     PlaceHolder1.Controls.Add(LoadControl("al.ascx")) 
  10. Back in Design view, double-click the second LinkButton (Dave) and add the following code:

     PlaceHolder1.Controls.Add(LoadControl("dave.ascx")) 
  11. Save the page and run it. Initially it will look like this:

    click to expand

  12. Click Alex and see how the content of the Alex user control is shown:

    click to expand

  13. Do the same for Dave:

    click to expand

How It Works

This code is extremely simple, relying on the fact that all controls can be created dynamically at run time and added to the page. This works because some controls can contain other controls (these are called Container Controls). The PlaceHolder is one of these, and is designed just for this purpose – as a holder in the page into which other controls can be added.

Container controls have a property called Controls, which is a collection (see Chapter 6 for more on these) of all of the controls they contain. One of the methods on this collection is Add, which allows us to add controls to the collection. To get the control to add we just use the LoadControl method and pass in the name of the user control:

PlaceHolder1.Controls.Add(LoadControl("al.ascx"))

So, this code simply loads a user control and adds it to the place holder.

You might wonder why we'd use this method instead of storing the content in a database. The simple reason is that databases aren't great at storing free-form content. They are very good at rigid structures, but not if you want each piece of information to differ in structure. Using the method shown above, the user controls can contain anything – there's no reason why each has to be similar to (or even the same as) the others.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net