Batch modification in ASP.NET


I have a page on my website where I have a list of climbs with a checkbox next to each one. I’d like to allow my user to check/uncheck each line and then click “update” to make all the changes at once.


Is there a way I can easily do this with GridView or Repeater, something else, or will I need to hand-code it?


Comments (4)

  1. adhalejr says:

    I’ve done this a number of times with a GridView control. While there may be other ways to do it, the way I’ve done it is to add a TemplateField, like:

    <asp:TemplateField HeaderText="Select?">

    <HeaderStyle HorizontalAlign="Left"/>

    <ItemTemplate>

    <asp:Checkbox ID="chkRowSelected" runat="server" />

    </ItemTemplate>

    </asp:TemplateField>

    Each row in the GridView then gets a control with the specified ID. Then, in the handler for your Update button, you can iterate through all the GridView rows:

    foreach (GridViewRow row in idOfGridView.Rows)

    then find the checkbox control:

    CheckBox cb = row.FindControl("chkRowSelected") as CheckBox;

    and then test to see if it’s been checked or not:

    if ((cb != null) && (cb.Checked == true))

    After that, perform your update for the row.

    Hope that helps,

    Donnie

  2. What The says:

    I’m thinking that qualifies as hand-coding it…

  3. ericgu says:

    Thanks, Donnie, that’s exactly what I was looking for.

  4. RobertM says:

    I’m trying to accomplish the same thing but in VB.Net.  My problem is the checkbox(s) always evaluate to false.  Here’s some code:

    Protected Sub btnMakeNew_Click(ByVal sender As Object, ByVal e As System.EventArgs)

           ‘Dim vCheckArray As Array

           Dim vRow As GridViewRow

           Dim xyz As CheckBox

           

           For Each vRow In VoiceMailGrid.Rows

               ‘Response.Write(vRow.RowIndex())

               ‘Response.End()

               

               xyz = vRow.FindControl("CheckBox1")

               

               If xyz.Checked = True Then

                   Response.Write(xyz.Checked())

                   Response.End()

               End If

           Next

       End Sub

    and the template definition:

    <asp:TemplateField HeaderText="Hello World">

        <ItemTemplate>

             <asp:CheckBox ID="CheckBox1" runat="server" />

        </ItemTemplate>

    </asp:TemplateField>

    Cheers,

    rfm