I wouldn't recommend writing large amounts of code within transformations; usually, I try to keep them as simple as possible so that front-end developers find them easy to work with. However, when you need to, this tip may be helpful.
It's important to remember Asp.net's Page Lifecycle when implementing code within a transformation. If you pick an event that's too early within the life cycle, you'll probably end up getting a null exception. When scripting within a transformation, you need to be familiar with which tags to use.