Gripe #1 – the solution

Well this was easier than I thought it would be. The code below is the rough solution. It’s so simple I’m recreating it from memory rather than posting the code I wrote at work; hence the variable names are straight from the .NET Reflector.
Basically, you need to create a class that overrides ListBox or DropDownList and override the RenderContents method. This one is for DropDownList. To do ListBox, just get the basic code using .NET Reflector and add in the one extra line (the one all in white):
protected override void RenderContents(HtmlTextWriter writer)
      ListItemCollection collection1 = this.Items;
      int num1 = this.Items.Count;
      bool flag1 = false;
      if (num1 > 0)
            for (int num2 = 0; num2 < num1; num2++)
                  ListItem item1 = collection1[num2];
                  if (item1.Selected)
                        if (flag1)
                              throw new HttpException("Cant_Multiselect_In_DropDownList");
                        flag1 = true;
                        writer.WriteAttribute("selected", "selected", false);
                  writer.WriteAttribute("value", item1.Value, true);
                  HttpUtility.HtmlEncode(item1.Text, writer);

Now to me, this looks like Microsoft simply forgot to put the line in. Anyway. It works fine and has already come in handy.


EDIT: Hmm. Doesn’t really fit very well on the screen. Clicking on the Blog tab lets you see more, but the best solution is to use the reflector and grab the code for yourself. You’ll need to redo the way it throws the exception, though, as the MS code uses an internal call.

This entry was posted in Computers and Internet. Bookmark the permalink.

2 Responses to Gripe #1 – the solution

  1. Gavin says:

    Good god you can really tell you work with computers

  2. Rob says:

    Well… yes. I\’d have thought the line "Occupation: Computer Programmer" was the main hint, though! I can\’t get in to your blog. Is it publicly accessible?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s