Friday, May 30, 2008

Adding fields dynamically in ASP.NET

Adding a control to a page dynamically is a very powerful tool when you want to allow users to add varying amounts of data to their submission. These controls should keep their state through multiple postbacks.

I recently had to solve this problem to allow professors to upload any number of files to our library server using a web form. It seemed simple enough to solve, but as I searched around the net, it seemed many people were having a lot of trouble with this topic or were using some pretty hackish things to get it working. I hope this post helps someone out there.

Using Session

The main problem of adding new controls to a page is that the new controls will disappear whenever there is a new postback. To solve this problem we simply store the control in the users Session which allows them to keep their data as long as their session stays alive.

We can store the controls in a List to keep track of them. When the page is first created, we store the list inside of the Session, then when the page is loaded, we get the list from the Session. Getting the list from the session when the page is loaded guarantees that we have the list of controls for the current user.

Adding Controls Back to Page After Postback

Whenever a postback occurs, all of the controls on a page are recreated. Since a control spawned within the page cycle is not bound to anything, it is simply destroyed.

In order for the dynamically added controls to show up, they must be added at the beginning of each page in the Page_Load method. In this example, we simply iterate through the list and add the control to a placeholder.

Adding a New Control

Whenever a new control is added inside of a button click method, it will not show up on the page immediately. What happens is that it is actually created and added to the array, but the page cycle is already past the Page_Load method, so there is no chance to add the control to the page from the array. To counter this, we simply have to add the control to the page manually inside of the method. In the next postback, the control will be added automatically like the others.

Example Files

Unfortunately, I cannot host files on Blogger so I will copy the contents of the files directly to this post. These files were tested with ASP.Net 3.5 and Visual Studio 2008, but they are simple enough to work on pretty much any platform.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
<title>Adding Dynamic Controls to a Form</title>
<form id="form1" runat="server">
<asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder>
<asp:Button ID="addControl" runat="server" Text="Button"
onclick="addControl_Click" />


using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
List<Control> controls = new List<Control>();
protected void Page_Load(object sender, EventArgs e)
if (Page.IsPostBack == false)
Session["controls"] = controls;
controls = (List<Control>)Session["controls"];

/* The controls must be added back to the page each time there
* is a postback, otherwise the controls disappear.*/
foreach (Control control in controls)
protected void addControl_Click(object sender, EventArgs e)
TextBox box = new TextBox();
box.ID = "box" + controls.Count;

/* We add the box manually first since the Page_Load
method has already finished. The box will only show up
after the next postback otherwise.*/

Sunday, June 17, 2007

How to add games to the Windows Vista Games Explorer

Note: There is an update to this post regarding a program that automates this process at the bottom of this page.

It is a very arduous process to get all of your old games to show up correctly in the Vista Games Explorer. I hope this guide saves at least one person from all of the trial and error that I had to go through.

These steps help prettify all of those games that aren't discovered automatically by Vista and also allows the games running on Steam to open nicely with the Games Explorer. After these steps, you will have a Games Explorer that looks something like this:

I found this forum entry to be extremely helpful. This might be all you need to know, but my steps are a bit more concise.

First find a game that Vista automatically finds and adds to the Games Explorer. I used the Steam shortcut that is created for most Half-Life 2 games named Half-Life: Source. It shouldn't matter what shortcut you use, as long as it gets regenerated if you delete it (not hide it, but rather delete the GUID folder). Also, the shortcut must be auto-generated by Vista and not added by the game itself. Games For Windows labeled games' shortcuts will not work.
  1. Find the box art for the game you wish to add to the Games Explorer and place the image on the desktop.
  2. Open the registry editor (Open the start menu and type regedit then hit enter)
  3. Navigate to HKEY_LOCAL_MACHINE_\Software\Microsoft\
  4. You will see a few folders, one named Games and then a few others. Look through the other folders and you will find the games inside of the Games Explorer.
  5. Find the game that was auto-generated (Half-Life: Source in my case)
  6. Inside you should see various values describing the game. Look for the key named BoxArt and double click it.
  7. Point the BoxArt key to the picture you put on the desktop. This will cause the Games Explorer to copy the picture into its own cache and update the image in the Games Explorer Window.
  8. Now edit the other values to match the game you are adding. The important ones are Title (The name of the game) and AppExePath (Use the .exe file that the start menu shortcut for the game points to).
  9. The changes should be applied to the Games Explorer soon. After it updates, you can run the game who's shortcut you just edited (Half-Life: Source) again and its shortcut should once again appear in the Games Explorer. This new shortcut may be used to perform the above steps for another game.
In order to get Steam games to work well you can use these steps after you complete the above:
  1. Right click the game in Steam and select "Create desktop shortcut"
  2. Right click the new shortcut and select properties. Leave this window open.
  3. Go into Games Explorer and right click the new entry you created and select "Customize"
  4. In the screen that comes up, make sure the top entry (Play) is selected and click "Edit"
  5. This is a regular shortcut properties menu just like the other. Copy the "Target" and "Start in" entries from the desktop shortcut to the Games Explorer shortcut.
  6. That's it. The Games Explorer entry should run fine now, with or without Steam running when you click it.
Tips on editing the values to match the new game:
  • Microsoft pulls its information from, so doing a quick search of this site will get you the developer, publisher, and box art for your game and hopefully match their data in the future.
  • If AllGame does not have the data, you should try looking at the Wikipedia article relating to your game. There is always good information and box art available.
Update: There is now a program named VGE Editor that can complete these steps for you. It is currently in beta, but I have tried it out and it seems pretty stable.

Sunday, February 25, 2007

Windows Media Player kicks ass

I just transferred whats left of my music collection from my mobile phone to my PC. It's small but I spent a lot of effort hand picking good songs for my trip to Minnesota last spring break, so it represents my favorites.

The mp3s consist of the usual grab bag of random eMule files, LAN party shares, and anonymous CD rips, so of course the names are all totally wrong and none of the meta data are worthwhile. I am a clicker (I just click on a random song that sticks out at the moment) so I never really used meta data for anything. I have finally seen the light; semantic mp3s are amazing!

I started up WMP11 to play some music while configuring my system and while playing the song, WMP started downloading album art for the songs. I was like... whoa.

So I looked into it further and found that you can have WMP search your entire library for meta data and "fix" all of your mp3s for you automatically. Wow!

I went to Library-->More Options and looked at the options under Automatic media information updates for files.

I chose Overwrite all media information which corrects your meta data even if it was entered incorrectly already. Good for those weird Kazaa rips.

Then I selected rename music files using rip music settings so that all of my music was correctly labeled and named. No more NiN-WiTH_teeTH(08)-jacktheripper.mp3 woohoo!

And for good measure I went ahead and selected Rearrange music in rip music folder using rip music settings. Now my music directory under Windows is properly sorted and catagorized using industry standard titles and genres. Each band is given a folder and then inside that folder are albums holding mp3s for that album.

I know you can do this with other media players, but I haven't actually tried it and WMP just kind of floored me with its simplicity and organization. I am so happy now that I have a properly labeled and searchable music library. It even changed me from a clicker to listening by genre. Nice.

Installing Vista Ultimate Edition 64-bit

Well, I have had a fun 16 hours.

That is how long I have spent installing and configuring Vista and the applications I need. It's agonizing and I already miss the ease of Ubuntu.

The first problem occurred as I boot up Windows setup for the first time. When it reached the drive partitioner, it reported that it found no disks on my system. I kind of scratched my head for a minute before I remembered that I had trouble with my Promise RAID controller in Windows XP.

I previously had two 400GB drives connected in a RAID 0 array using my Promise chipset. One of them bit the dust so I was forced to switch the Promise controller into IDE mode. XP balked, but I got it working using a Promise SATA driver. On the other hand, Ubuntu refused to accept the RAID array, but the single SATA drive operated flawlessly. So I continued about my life.

Well Vista ending up choking the same way XP did without the driver. I did not want to deal with this BS again, so I shut off my computer and reconnected my drive to one of the VIA SATA ports. This solved the problem. Yay. I feel stupid for not doing this earlier.

So, with Vista now installed, I moved on to the exciting world of device drivers.

Since I chose to install the x64 version of Vista (as any true geek should), I expected at least one device to refuse to work. To my amazement, my Logitech QuickCam (the one that looks like HAL) was setup automatically by a tiny file received from Windows Update. Pretty slick in my opinion. My printer, video card, and USB hubs all worked out of the box. I didn't even have to deal with any unsigned driver issues.

I was feeling happy until I realized that I had absolutely no sound at all. Bleh. I have a Creative X-Fi Fatal1ty sound card and it wasn't even detected by Vista. It didn't even prompt me that it found an unrecognized device. Nothing.

So I went to Creative's website to download the driver. Low and behold it was listed as a beta version. <rant>WTF. I hate you Creative. Your shit doesn't work. Your drivers suck. Your cards don't even work in Linux!</rant> I downloaded it anyway and it installed smoothly.

Now that the hard part is over, I moved on to the easy stuff: applications.

I am tired of writing this blog entry for now. I will edit it later.

Friday, February 2, 2007

How to make a blog

Today I finally stopped procrastinating and started this blog.

My idea for this blog started from a notion that whenever I tried to inform someone of how to do something, I would end up finding holes in my factual knowledge that I would be eager to correct. If I told more people, then my knowledge of the subject would start to correctly align with the truth.

Also, I figured out that you can easily believe something is true without knowing all of the facts. It's easy to believe, but once you start asking others to believe, you end up needing many concrete facts.

Writing my thoughts down will force me to absorb the facts so that I can form an accurate and articulate thought. There is a very slim chance anyone will read this, but that slim chance does keep me on my toes where I feel I must not slip up or fudge the truth.

I hope this experiment works. If I continue after the novelty wears off and my classes get harder, then I will reap the fruits of my labor; better writing skills, more articulate thoughts, and proper understanding of subjects.

I felt like I learned something already with my first post. Here goes nothing!