Scott Hanselman

Bringing data "along for the ride" with CheckBoxes and the ASP.NET DataGrid

December 4, '03 Comments [5] Posted in ASP.NET | XML
Sponsored By

Recently I needed to have a DataGrid that had multiple checkboxes to activate and deactivate something.  So, you could check a whole slew of checkboxes in a big grid, and depending on the NEW state of the checkbox (checked or not), cause an operation on a theorectical back end.

Here's some opinion/thought/zen on the DataGrid, and DataGridGirl will have to tell you if I'm wrong or not.

  • Work WITH the Grid, not against it: If you find yourself filling up the Grid via DataBinding, THEN running around in the rendered grid with foreach this and foreach that looking for HTML controls, you probably want to rethink things.  Look to the Events, my son.
  • Listen to the Events and Nudge/Influence the Grid: Between OnItemCreated and OnItemDataBound, you've got some serious influence on the grid.  If you can't get things to happen declaratively in the ItemTemplate, get them to happen Programmatically in these events.
  • Avoid Hidden Columns with IDs of things: In my case, I needed to hear when a checkbox's state changed, and at that time, I needed to know the ProductID associated with that checkbox.  Rather than wadnering around in the Control Tree (ala DataItem[x].Parent.Parent.Parent, etc.FindControl("yousuck"), just bring the data along for the ride.  See my solution below.
  • Just because code is on CodeProject or in Google Groups doesn't mean the writer knows the DataGrid from his Ass:  <rant>If I see one more solution in CodeProject or Google where someone says, "ya, just (DataTime)(((TextBox)Whatever.Item.Parent.Parent.Child[0].Parent.BrotherInLaw).Text).IntValue.ParseExact('yyyy/mm/dd')) and you're there" I will seriously hurt someone.  The DataGrid has it's faults, sure, but it's a lot more subtle that .DataBind and brute force it.  This some problem has happened with the XML DOM, and it's all the fault of QuickWatch.  "Gosh, I can SEE the data, so it must be OK for me to spelunk for it."  </rant>

I needed to know when the checkbox changed, then act on it.  So I remembered the order of events during a postback (and you should too):

  • Events fire in control tree order
  • THEN the control that CAUSED the PostBack fires it's event LAST.

So, I'd get a bunch of CheckBox_Changed events, and finally a Button_Click (from an 'Update' button).

In the Changed events I loaded a page-scoped ArrayList of things to act on, like, ProductIdsToActivateArrayList or ProductIdsToDeleteArrayList, depending on how many columns in my grid had checkboxes.  As the Changed events fire, I just wanted to load up the ProductIDs for that CheckBox's row.  But, how to avoid running around in the control tree? (which is, as I said before, gross)

I needed the data to come 'along for the ride' with the CheckBox_Changed Events.  So in the .ASPX page:

<ItemTemplate><asp:CheckBox id='checkbox' ProductId='<%#DataBinder.Eval(Container.DataItem, "ProductID")%> OnChecked='CheckBoxChanged'>

Notice that!  A totally random and unknown attribute in the CheckBox's statement.  Is that allowed? How will it render?  Well, it is allowed (although VS.NET's Designer will complain).

It renders, interestingly enough, like this:

<span ProductId="4"><input type="checkbox" id="checkbox" name="checkbox"></span>

Look at the extra span!  Crazay.  Then in the CheckBoxChanged event on the server-side after someone clicks a bunch of CheckBoxes and clicks Update:

public void CheckBoxChanged(object sender, EventArgs e)
{
     string ProductID = ((CheckBox)sender).Attributes["ProductID"]).ToString();
     ProductsToDeleteArrayList.Add(ProductID);
}

Then in the Button_Click event (remember, that happens AFTER all this) I spin through the ArrayLists (there are several, one for each action) and perform the actions in a batch.  This makes the Button_Click code CLEAN.  It makes the CheckBoxChanged code CLEAN, and it bypasses a whole lot of running around in the control tree.

Also, before I forget: Congratulations to Robert for the birth of the definitive Scrolling Data Grid.  Kudos.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Pentium 4s, DVD Burning, ASP.NET, and Longhorn...oy...what a day.

December 4, '03 Comments [2] Posted in ASP.NET | Web Services
Sponsored By

Been out of the loop for a few days.  It's the holiday season, and it's just hard to blog when it's so darn crappy outside. :)

Anyway, there's lots of stuff going on and I assure you I will blog the crap out of it soon.

Here's what I've been doing in the meantime, and what I think about it.

  • Longhorn.  What an install.  We all know the "10 minute hardware detection period" is really overnight, so that's OK.  I've got it on a P4 3Ghz with a Gig of RAM.  WinFS takes 170 megs of memory, and Explorer takes at least 200 megs.  I've heard people complain about this.  Geez people, it's NOT EVEN ALPHA.  It's so not Alpha, it's Zeta.  NEVER before has Microsoft given the masses such early code, so let's be happy about it. 
    • That said, when you get it installed and dig into the SDK, you see the work they've (and are) putting into it.  Very evolutionary.  Using the "stacking" features are AWESOME.  They are Outlook 2003 Search Folders on Context-Sensitive Steroids.
    • If you're interested in Longhorn: Stop by the Portland Area .NET Users Group Thursday night.  I'll be joined by Jim Blizzard and we'll be riffing on Longhorn.  I'll drag my box down there and you'll get to play with it hands on.  We'll look at the Three Pillars: Avalon, WinFS, and Indigo.  Also, Jim will be kicking off the MCSD Study Group with the arrival of the books!
  • Pentium 4.  My P4 3.2Ghz machine has been chewing inside of NeroVision Express for 8.5 hours and no end in sight.  I taught a class recently and it was taped on Hi-8.  I used my Digital-8 Camera with Firewire to 'rip' the video to my 250Gig Firewire External Drive.  Each hour is roughly 11Gigs.  Then I setup an interactive DVD Menu and I'm now transcoding the video into something I can burn to a DVD-R.  WOW. That's quite a process. 
    • The Pentium Active Monitor has been complaining as the temperature reaches in excess of 142 degrees F on the processor, and over 100 inside the case. 
    • WHY do I have to open my case (which has 4 separate fans) to recode a DVD?
  • DVD Writing.  I got the new External Firewire Sony DVD+-*.* drive that writes to every <5" disc in the world, other than 45RPM Vinyl, and it may do that soon.  The software that came with the drive sucked.  So, NERO.  Again, I say: Nero is the single greatest value in off the shelf software today.  Truly, $1000 of value for only $69.  And some places there's a $50 rebate. Oy.
  • Tablet PC. My new Toshiba M200 is NOT here yet.  Lame.  Why 'release' something when you know you only have 10 to sell?  I don't even know if it's been built.  Sigh.  Maybe before Christmas.  Anyone get yours?
  • Teaching C#The Final Approaches!  The class is stoked and so am I.  Now, do I teach next term?  Possibly a Distributed Objects or Web Services class?  Possibly Beginners Yoga.  Hm.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Bring out of your element breeds productivity?

November 27, '03 Comments [4] Posted in ASP.NET | XML
Sponsored By

As I look at my blog calendar (currently up and to your left) I see that I have blogged less this month and previous.  To say I have been heads down lately would be an understatement.  And let me tell you, when it rains it pours. 

Apparently I am such a complete wimp that even something as challenging as the stairs in my home have the power to crush me.  Yes, my back is out, and it happened on the 11th step.  Was I lifting something?  Something heavy?  A computer monitor?  Some new myterious hardware?  No.  Lifting my wife? Sweeping her off her feet and into the Master Bedroom.  Sadly, no.  I was holding nothing heavier than a bottle of Formula 409 to clean the can.  Long story short, this is the first day I've been able to walk without pain like you can't believe.  The prognosis?  Good.  The cause?  My sedentary lifestyle.  I am the fattest thin man you'll ever meet.  I'm like 65% fat, seriously, it's just thin and stringy.  Anyway, what doesn't kill you, makes you write more code, right?

So we've been working on this fabulous ASP.NET site for a bank that is using our glorous eFinance XML Application Server, Voyager.  We've been hitting milestones left and right, and tonight was a big one.  Over the last few months, I've been "Chief-Architecting" less, and "Programming my ass off" more.  Gotta exercise those carpal-tunnel crippled hands, right?

Anyway, I've come to this conclusion.  Working in your office is ONE kind of productive.  But, when you're in a bullpen, or more generally, when you're working ELSEWHERE, that's a different kind of productive.

If you've travelled you know this.  If you're on a plane, you can catch up on an INSANE amount of email that you'd NEVER answer in the office.  From New York to Portland can find me with 30+ emails in the outbox, tying up loose ends I'd never have otherwise. 

I work with some talented engineers at Corillian, and while there have been some great late-night coding sessions alone, there's something to be said for being shacked up in SOMEONE ELSE'S cube.  No phone, no interruptions, just pure dual-brain programming.  We got some great stuff done tonight.  I was so far out of my element that I was in the zone. 

I've decided that being off-kilter a smidge, being elsewhere, can bring on a level of creative thought that is stiffled by the home office, or the work cube.  I've gone and coded in the lobby, or in the elevator, up and down, just for a change of venue.  The results - for me - speak for themselves.

Where do you do your best work?  Home office?  Starbucks?  Always changing?  What about that/those location(s) makes your little gray cells turn better?

I've decided to stay frosty by moving around.  It makes the work fly by.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Glory be, my MSDN Article is up

November 25, '03 Comments [1] Posted in ASP.NET
Sponsored By

After a few small logisitical snafus my article on back-porting cross-site-scripting from ASP.NET 1.1 to ASP.NET 1.0 is up at Kent Sharkey's ASP.NET section on MSDN.  Thanks Kent!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

I'm late but: How I got into Computers

November 21, '03 Comments [3] Posted in Musings
Sponsored By

This is late, as many others have already shared their stories, but here's the short story on how I got into computers.

In the neighborhood I grew up in, I was apparently be labeled "at risk."  Probably would have been vote most likely to be convicted of a white-collar crime.  One day there was some kind of parent-teacher-school district superintendant meeting and they decided if we don't give this kid something to do, we're going 'lose him.'  So, my fifth grade teacher let my dad and I borrow the Apple II computer over the weekends.  I started using friends' TI89/A's and spent more than one weekend typing and printing a 20 page (20 pages of 8x11 ASCII ) poster of Mister Spock on a TRS/Trash80.

Then, a few months later, the parents sold our van to buy me a Commodore 64, and a few copies of COMPUTE! magazine pretty much insured I'd be indoors for life.

Fast forward 5 years (various operating systems I used described here in an earlier unrelated post) and I was selling my services (which consisted of a complex 'document recovery' racket at local high schools [né 'business'] as Tweak Inc. running entirely off a single copy of Norton Disk Doctor).

Fast forward a few more years and I was running Tweak Computer Support and BBS and programming Win16 apps in C.

Fast forward a few more years and I sell Don Box a Newton on opening day

Fast forward a bit more and you can find me posting on USENET from nike.com.

Fast forward <10 years and I'm working at Corillian Corporation and blogging with you fine people.  And it all started with my fifth grade teacher letting me type 'CATALOG' into an Apple II.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.