Dynamic User Controls on an ASP.net Page

by Andrew Jackson 23. January 2005 10:03

Spent quite a lot of time today getting to grips with dynamically placing user controls on a page at run time.  I wanted a series of buttons which were determined by database entries and these buttons needed to have code to display a page when clicked.  If your wondering how to do this yourself you need to know about Page.LoadControl and AddHandler;

  • Create a sub for the click handler with the usual ByVal sender As System.Object, ByVal e As System.EventArgs parameters.
  • Use controlvariable = Page.LoadControl to load an ascx into a variable (defined to be the type of the user control)
  • Assign the controlvariable.id to something useful
  • Do a me.controls.add controlvariable to add it to the page (or a me.container.controls.add if you've got your page divided up into tables or the like)
  • Do an AddHandler ctype(me.controls(controlindex), control type).Clicked, AddressOf nameofclickhandler

The click handlers sender property will have the control (as type object so you have to cast it again) so you can inspect the ID and find out which button was clicked and act accordingly.  For my example I made a custom control which had a few basic properties of a button object exposed plus an additional one for the page name associated with the button.  That way it was instantly available from the object passed into the click event handler and i could do a response.redirect with it.

A lot of work casting back and forth for a newbie to this but it works, would recommend putting these controls into a table/div/whatever block so you can keep track of the control indexes, also found it useful to make a quick hash table to store the control objects and their ID's for easy access to each control.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development

ASP.net Training

by Andrew Jackson 23. January 2005 10:02

Spent the last week on an ASP.net training course with Pygmalion.
Good course, even though I knew a little of asp.net before I started I learnt a lot and went away confident at writing apps from scratch rather than cobbling together bits.

A big thing that came out of it for me was a renewed determination to fully embrace Option Strict in all my .net code, being from a VB background I'd enjoyed years of clumsy programming letting the compiler work it out for me.  Spent the weekend prototyping a system for work and stuck Option Strict on and forced myself.  It was annoying at first but I soon found myself making informed decisions on what my code was doing rather than just object.mystical property to object.mystical property and keeping my fingers crossed.

Oh, and the best thing about the course.. great lunches every day, and some interesting people on the course from a wide background, all asking questions that I found useful as well.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.net | Development

New Printer

by Andrew Jackson 21. January 2005 09:50

Well my old HP deskjet all in one died over the Chrismas period so I had to go looking for a new one.  My love of all things networked led me to the HP OfficeJet 7310 and it's a very nice all in one printer/scanner/copier/fax thats networked.  Just install the client software and you can scan straight from the printer to a connected pc.
Has lots of nicities like memory card reader, adf unit and even remote printing over the web though this seems to use a port that my works firewall blocks.
Being directly networked means it can be used from any pc in the house without a "server" sharing it and having to be always on.
Thoroughly recommended.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Recommendations

Slimline Desktop

by Andrew Jackson 21. January 2005 09:48

I've recently re-installed my home pc and took the time to get some cool utilities that would be resource friendly and be very functional.

One of these is ObjectDock available from http://www.stardock.com/ a Mac like toolbar that you can put at the top/bottom/side of your screen and it zooms in and out with mouse overs.  I've got rid of all my desktop icons and just have this one toolbar (and windows taskbar though it can replace that if you so want).

Theres a freebie version which is really functional and a pro version that has a few extra bells and whistles.  So impressed I've actually bought the pro version.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Productivity

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About Me

Andrew Jackson

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008,2009 Andrew K. Jackson

RecentComments

Comment RSS