gists, aspnet, dotnet comments edit

Let’s say you have some text on your web site that has an image inline. Something like this:

Fields with a ! icon indicate failed validation.

You’ve seen that before.  You’ve probably done that before.  Now let’s say you’re using ASP.NET themes and skins to control your look and feel, and that icon is different based on your theme.  It might change to be:

Fields with a ! icon indicate failed validation.

You could have some ASPX code that handles that with the SkinID of the image, right?

Fields with a <asp:Image ID="icon" runat="server" SkinID="validationIcon" /> icon indicate failed validation.

Okay, that works acceptably, provided your skin defines an Image that has a SkinID “validationIcon.”  But you’re a savvy ASP.NET developer and you know better than to put literal strings like that right in your web application - you put your strings in resx files and look them up at runtime so your app can be localized.  Now what?

There are a few ideas you could try, but they don’t work (or not well):

  • String.Format - You could leave a {0} in the page text and try to String.Format the image into place.  That might work if you weren’t skinning, but you need to take advantage of ASP.NET skins… so that won’t work.
  • Client-side script - You could try some client-side script to render the image to a hidden place on the page and then place it into a <span /> or something in in the instructions, but that’s kind of painful.
  • Break the text up into separate controls - You could make the text three controls: the text before the image, the image, and the text after the image.  This could become hard to manage from a localization perspective (what if there isn’t any text after the image?) and it’s not terribly flexible.

The answer: ParseControl.

The System.Web.UI.Page class has a ParseControl method on it that it inherits from TemplateControl.  You can use this to your advantage - just put the markup inline into your resource string and use ParseControl method to create a control hierarchy on the fly.  Then just swap the parsed control hierarchy into your page where you want the text to display. Put a Literal in your ASPX and do your string lookup…

<asp:Literal ID="pageText" runat="server" Text="<%$Resources: MyResources, PageText %>"/>

And in your resx file, put the entire text, including the image markup:

<data name="PageText" xml:space="preserve">
  <value>Fields with a &lt;asp:Image ID="icon" runat="server" SkinID="validationIcon" /&gt; indicate failed validation.</value>
</data>`

See how that looks just like ASPX page markup?  Perfect.  Now in the codebehind of your page class, replace the Literal with the control hierarchy that gets parsed from the text in that very Literal:

public partial class MyPage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    Control parsed = this.ParseControl(this.pageText.Text);
    Control parent = this.pageText.Parent;
    int index = parent.Controls.IndexOf(this.pageText);
    parent.Controls.AddAt(index, parsed);
    parent.Controls.Remove(this.pageText);
  }
}

In that example code, we:

  • Parse the text that got looked up from resources into a control hierarchy.  This lets us account for localization while still providing the inline themed icon we’re looking for.
  • Grab the parent of the original control.  This is important because you need to insert the parsed control hierarchy into the proper spot in the control tree.
  • Find the exact location of the original control in the parent’s control structure.  This tells us where to put the parsed control hierarchy.
  • Insert the parsed control hierarchy into the proper spot.
  • Remove the original control from the hierarchy - we’ve replaced it, so it’s no longer needed.

Pretty nifty, eh?  There are lots of other things you might want to consider if you do this, but I’ll leave them as “an exercise for the reader,” so to speak:

  • The example replaces the control with the parsed hierarchy; you might instead wish to add the parsed hierarchy as a child of the control you’re “replacing.”
  • If you use the same text twice in the same page, you may end up with control naming clashes; you might want to wrap the parsed control hierarchy in a naming container to avoid that.
  • You probably don’t want to repeat this code over and over in every page; you’d want to have a single service class that does this for you.

A tiny word of warning:  you probably don’t want to do this on every single string you localize. It’s not super expensive, but it isn’t necessarily “free,” either.  Here’s the timing from trace output - you can see the obvious difference in the amount of time the page Load event (where I’m doing this) takes vs. the other events.

Timings for parsing inline
controls.

It’s only ~0.01 seconds, but you wouldn’t want to, say, put this in the middle of a big databinding block.

General Ramblings comments edit

Woke up last night at 2:45a to the neighbors out in their yard arguing. No physical violence, and it was mostly the guy yelling, but super loud.  I don’t even think I’d heard them before, except for their dog barking incessantly a year or so back.  Sigh.

Jenn ended up calling the cops who couldn’t actually find our house on a map.  She kept having to give a bunch of nearby cross-streets and directions.  That makes me feel really safe.  I imagine a time where there’s a psycho killer in my house trying to stab me and the 911 conversation goes like this:

Travis: Oh my God, help, there’s someone in my house trying to stab me!  Here’s my address - get here as soon as possible! Dispatcher: Hmmm… you don’t seem to be on the map. What’s your nearest cross-street? Travis: I think he’s coming up the stairs! Wait - cross-street? Uh… Foo Ave. Dispatcher: Yeeeaaaah, um, is that north or south of Bar St.? Travis: South.  Hurry! Dispatcher: Are you sure? Where are you in relation to Baz Pl.? Travis: -stabbed to death-

That’s the first time I’ve called the cops on someone.  When they finally arrived, it was in total force - like, two cars and another cop running up a side street.  I’m surprised the SWAT team wasn’t there.

Had a hell of a time getting back to sleep, though.  It took me forever to get to sleep the first time, then this.  I’m dead tired today.  Damn, anyway.

gaming, xbox comments edit

Okay, so for a while I thought it might be that I got a bum set of drums with my copy of Rock Band because I’m only able to get like 98% on Easy level.  I mean, I swear I’m hitting them right, but it’s just not registering.

To test, I went into practice mode and played a song segment that basically has no drums at all - so I wouldn’t get confused by notes being hit or missed.  I did a roll on each drum to see how it went and all of the hits were registered.  I also hit various combinations of two pads simultaneously and all of those hits were registered.  So I was/am encountering a combination of issues, all adding up to Bad News for Me:

  1. The timing on the drums is utterly unforgiving.  If you’re playing guitar, you get a little plus/minus room for strumming and hitting a note.  Sort of a “close enough” buffer.  With drums, you’re either ON or you’re NOT.  No fudge room.  A millisecond or two off, and it won’t count.
  2. The USB hub that comes with the game is pretty bad and seems to introduce a little latency.  Not a lot, but with timing having to be pretty precise, I notice a little better response when I plug the drums into the Xbox directly and everything else goes in the hub.
  3. Calibrating the game is hugely important.  Sometimes the presets work; I manually calibrated mine and things seemed to be a little better.
  4. I may just be a sucky drummer.  After checking out the responsiveness of the drums, calibrating everything, and plugging the drums right into the Xbox, I’m doing a little better but am still missing a couple of notes per song on Easy mode.  It’s usually during a spot where I have to hit two pads simultaneously and I think it’s that I’m hitting them close to correctly but actually hitting one pad slightly earlier than the other, so it doesn’t count.

I’m pretty good at Donkey Konga bongos.  I think playing drums with your hands and playing them with sticks might be two different beasts. I’m OK with that - knowing that it means practice is one thing; doubting yourself without knowing if it’s the equipment or not is a whole other set of insecurities.

gaming, xbox comments edit

My copy of Rock Band arrived yesterday and it well and truly does rock.  The thing I was excited for was the drum kit; I’ve always fancied myself a drummer, and I finally get to test that out.

Turns out, I’m not all that bad.

A breakdown by instrument…

*Guitar **I’ve played my fair share of *Guitar Hero 2 and Guitar Hero 3, so I knew what to expect out of the guitar experience.  You can play lead or bass, your choice.  The actual playing experience with the guitar is roughly the same, so I won’t go too far into it.  That said, I did notice the difficulty level was significantly lower than GH3, and possibly slightly lower than GH2.  That’s not a bad thing - it just makes you feel cooler.

I can’t say much about the controller it ships with, though, because mine arrived broken (the directional pad on it is stuck so it constantly thinks the “down” arrow is being pushed - throws a wrench in the works, let me tell you).  Fortunately their support is really good and you can very quickly get an RMA and a replacement through their automated online system - no need for massive escalation or trouble.  Ordered my replacement this morning.

*Microphone (Vocals/Tambourine) **I haven’t played *SingStar but I gather the vocals portion here is the same as that.  It’s a little harder than I expected and it does require you to really know the words and the tune - including all the little fluctuations the singer makes while singing.  In some cases you can get by with volume over accuracy (which isn’t far from real life, right?) but generally you do need to know the song pretty well.

For example, I sing along to Bon Jovi’s “Wanted Dead or Alive” in the car, but it turns out I really only know about half the song… and thinking back, I do sort of hum my way through a lot in the car.  That said, I was able to get 100% (on Easy level) for The Clash song “Should I Stay Or Should I Go?”  So it’s not impossible, just sort of hard.

During musical solos, you can tap the mic in time with some “beats” that appear and play the tambourine.  This sort of reminds me of the clapping that you have to do in Donkey Konga.

Drums This is what I was waiting for.  It’s a heck of a setup and isn’t really small sitting in your living room, but it’s hella fun.  There are “notes” on the screen for hitting each of the four drum pads (just like Guitar Hero) and the kick drum is signified by a hard line that crosses all of the four note positions at once.  It takes a little getting used to, but once you figure it out, you’re in.  I’m working through Easy difficulty right now because I tried Medium and… well, there’s too much going on and I haven’t quite got drums down yet.

In some cases, though, I can’t figure out whether my drum controller is faulty or if I’m just a sucky drummer.  I swear I hit the right pad at the right time, it just says I didn’t.  Other times, everything’s fine.  I can get like 98% in some cases, I just miss a couple.  I would think if the controller was faulty it would be… more predictable. Again, the 60 day warranty might be good here.  Drums are definitely less forgiving than the guitar - with the guitar, you can “pretty much” get the note and it’ll count it; with drums, you either get it dead on or you don’t get credit.  I think that’s more likely what I’m running into.

That said, I really hope someone comes out with some after-market drums for this game.  I like the drums, but I’d like some of it to be a little more adjustable.  I’m 6’2” and I feel sort of cramped by the kit due to the placement of the kick drum pedal.  Maybe I just need to try some different positioning.

*The Game **The game itself is pretty good.  Far better set list than *Guitar Hero 3 had.  I also really like the way the campaign is set up as a “world tour” where you start in small venues and gather money and fans - it feels more tangible than the arbitrary progression you get in Guitar Hero.  You also have a really nice character creation system that allows you to personalize your character including face, hair, clothes… very cool.  There were only three points of confusion I had:

Point of Confusion 1:  Bands are attached to an Xbox Live profile and to a Rock Band character.  When you create a band, the person who’s creating the band gets the band saved to their Xbox Live profile. Further, as you select (or create) your character in the band, that specific character has to play in the band for the entire life of the band - they’re the band leader.  This is very important because…

Point of Confusion 2:  Characters can’t change instruments.  Once you create a character for a particular instrument (guitar/drums/mic), they can’t switch.  Jenn and I created a band where I was playing drums and she was playing guitar.  She wanted to try the drums out, so we tried to get it so her character was playing drums and mine was on guitar.  No dice - both of us were only allowed to create new characters.  And since my profile was the one with the band leader, and the band leader was playing drums, we either had to play under the other person’s profile or create a new band.  We created a new band.  (The third option, really, was to back out and do a “quick play” where you can form a band impromptu with anyone playing any instrument - no leader required.)

Point of Confusion 3: Once the instruments are attached to the Xbox 360, you can’t change their position.  So, say I attach the instruments and the drums are player 1, the guitar is player 2, and the mic is player 3.  By default, my console signs me in when it turns on, so I’m signed in as player 1 and I’m stuck on drums.  The easy way to fix this - connect all of the controllers to the Xbox, turn it on, and sign everyone out.  Everyone pick up the control they want to play with and sign in from there.  During the game if you want to change instruments, you can sign out and sign back in without exiting the game, so just do that - sign out on your current instrument and sign in on the one you want to switch to.  It sounds like a no-brainer when I say it here, but trust me, this was a huge problem for us to figure out.

Again, by-and-large, it’s an awesome game.  I’m also super happy [so far] with the ease of customer support.

Hey, since I’ve got the day off, I should probably go do a little rockin’ right now.

dotnet, vs comments edit

I just got done with installing Visual Studio 2008.  It now is the installation experience that I will rank other installation experiences against to see how badly they suck - on a scale of “Awesome” to “VS 2008.”  It went something like this:

  • Download VS 2008 ISO from MSDN.
  • Start up Virtual CD Control Panel and mount the ISO as a drive.
  • Install VS 2008.
  • Get about halfway finished and get asked to reboot.  Click the “OK” button and reboot.  No option available to not reboot.
  • Log in and watch the setup alert me, after “loading setup files” for 10 minutes or so, that setup has failed and I need to restart.
  • Figure out that there’s no option to automatically mount an ISO at startup using the Virtual CD Control Panel and guess the issue is probably that the drive disappeared after my reboot.  Hard to say, though, with no specific error message.
  • Mount the ISO again figuring I’ll give it another run.
  • Get about 75% finished and, again, get asked to reboot.  Again, no option not to reboot.  Click “OK” and reboot.
  • Watch very closely as I get logged in again and haul ass to get the Virtual CD Control Panel up and re-mount the ISO before the “loading setup files” gives me the failure message.  Get the drive mounted but still get the failure.
  • Decide to uninstall the bits that got installed, figuring something got corrupted.
  • 15 minutes into “generating setup script” for the uninstall, get a notice that I need to close Outlook because it has Word open.  Close Outlook.  Watch in horror as the “generating setup script” bit starts over from the beginning.
  • Finish the uninstall and reboot for good measure.
  • Decide that the Virtual CD Control Panel is bad news for me and that it’s time to burn the ISO to a DVD.
  • Realize after 20 minutes of futzing around that of the two computers in my office, one only has a CD-RW burner and the other has a DVD burner so old that the Windows Server 2003 OS doesn’t actually recognize it as a DVD burner.  No drivers, discontinued support.  No one else in my general cube vicinity has a DVD burner either.  What the…?
  • Download an ISO extraction utility and extract the contents of the ISO to my drive and start the installation from there.
  • Shut down everything on the machine that could remotely be construed as productive for fear that the install will be mad at me and restart midstream.  Includes Outlook, IE, Word, Messenger, etc.
  • Finally get through the install of both VS 2008 and the associated documentation.  Takes somewhere between “forever” and “holy crap” to finish.
  • …and now I’m installing all of my add-ins.

Elapsed time from start to installing add-ins: 6 HOURS.  Absolutely ridiculous.  From what I hear, I’m not the only one eating it on this one.  I don’t remember the betas kicking my ass like this.  What happened?