How to use a checkbox in a datagrid template column.

This post has generated lots of comments and over 1,500 hits per month, I hope that it is helpful to you. Before you comment on the post, I need to point out that this is just a weblog. Please use the following guidelines when considering comments.

  1. If you find my solution useful, or have trouble making it work, please post comments.
  2. If you have a different or related problem, I suggest you try experts exchange, which is a dedicated help forum.
  3. If you would like to send me an email directly, go here. I'll try to answer questions, but please understand that my time is limited, so I may not be able to get back to you right away.

-Scott:)


I'm currently working on an ASP.NET project that uses a datagrid with several checkboxes in the item templates, and we wanted to use the checkbox click event to modify appropriate data.

Problem:     You  need to respond to the checkbox change event, and know which check box on which row fired the event. The checkbox does not automatically call the datagrid's ItemCommand, so how can we access the datagridItem that represents the row we're clicking?

After some experimenting, here's what I believe to be the best solution:

In the page code behind file, create a Sub that matches the checkbox changed signature. It's important that the method be declared public or protected.

Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)

End Sub

     In the aspx page, modify the checkbox tag by adding the OnCheckedChanged attribute, and setting it to the name of the sub that you just created above. You'll also need autopostback set to true.

<ItemTemplate>    <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="Check_Clicked">asp:CheckBox> >

     Now when the checkbox is clicked, the Check_Clicked event will be fired. That's great, but the signature for the Checkbox_Clicked event does not give us immediate access to the datagridItem like the familiar ItemCommand signature does, so we've got a bit more to do.      It turns out that the checkbox's NamingContainer property evaluates to the datagridItem that we are seeking. Add the following code to the Check_Clicked procedure, and you're home free

Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)        Dim ck1 As CheckBox = CType(sender, CheckBox)        Dim dgItem As DataGridItem = CType(ck1.NamingContainer, DataGridItem)        'now we've got what we need!        Label1.Text = "You selected row " & dgItem.Cells(0).Text End Sub

This article is part of the GWB Archives. Original Author: Scott G.

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.