Dynamic User Controls on an ASP.net Page

by Andrew Jackson 23. January 2005 17: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 17: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

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen