The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Scott Hanselman

Posts: 1031
Nickname: glucopilot
Registered: Aug, 2003

Scott Hanselman is the Chief Architect at Corillian Corporation and the Microsoft RD for Oregon.
Bringing data "along for the ride" with CheckBoxes and the ASP.NET DataGrid Posted: Dec 4, 2003 3:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Scott Hanselman.
Original Post: Bringing data "along for the ride" with CheckBoxes and the ASP.NET DataGrid
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Scott Hanselman
Latest Posts From Scott Hanselman's ComputerZen.com

Advertisement

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.

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

Topic: Why not ++ and --? Previous Topic   Next Topic Topic: Why my HTTP Compression is not working in IIS 6.0?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use