ASP.net Update multiple GridView Checkbox

ok, getting close.

firstly you need to put this code into the for .. next loop

Code:
        If intUpdateValue = "1" AndAlso Original_ID > -1 Then
            hoursAdapter.UpdateApproveCheckBoxQuery(Original_ID)
        End If

otherwise your only going to update the last row in the grid.

With regards to the post, it's because I don't know what the DAL looks like. so I don't know how you apply the id parameter.
 
Sorry, i'm not sure what you mean the by the "for .. next" loop?

My DAL Looks like... (Not sure if this is what you're after though)

tblHours
ID - int (Auto-increment identity field)
TimeCode
Date
Hours
Username
Comments
Approved - Bit

Main query is simply: SELECT tblHours.* FROM tblHours
 
the for .. next loop is this. What you need now is just to make sure that your getting the id back (see highlight). if this isn't finding the correct control with the id contained (it may not actually exist in a control if it's read only) you'll have to use the debugger to figure this out

Code:
       For i As Integer = 0 To num - 1

            [COLOR="Red"]Dim IDlabel As Label = GridView1.Rows(i).FindControl("Label1")[/COLOR]
            Dim cbox As CheckBox = GridView1.Rows(i).FindControl("CheckBox2")


            If IDlabel.Text.Trim().Length > 0 Then
                Original_ID = Convert.ToInt32(IDlabel.Text)
            End If

            If cbox.Checked = True Then
                intUpdateValue = "1"
            Else
                intUpdateValue = "0"
            End If

            If intUpdateValue = "1" AndAlso Original_ID > -1 Then
                hoursAdapter.UpdateApproveCheckBoxQuery(Original_ID)
            End If
        Next
 
Ok I can't figure this out. I checked the GridView to make sure Label1 is the ItemTemplate for the ID field.

IDlabel isn't getting any value :S
 
It may not do, you need to debug it, and check against the GridView1.Rows(i).Cells collection and go through each cells to find it. I don't know how you have it setup on the app, but there's a very good chance thats it's not stored in the item template control. It may be stored as a literal control, or as a something else. You will need to look at the control collection of each cell to see where it is.
 
Code:
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="TimeCode" HeaderText="TimeCode" SortExpression="TimeCode" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
            <asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
            <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
            <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
            <asp:TemplateField HeaderText="Approved" SortExpression="Approved">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Approved") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CodeOwner" HeaderText="CodeOwner" ReadOnly="True" SortExpression="CodeOwner" />
        </Columns>
    </asp:GridView>
 
Right it looks like instead of trying to get the label you should try the following.

Code:
       For i As Integer = 0 To num - 1

            Dim cbox As CheckBox = GridView1.Rows(i).FindControl("CheckBox2")

            If IDlabel.Text.Trim().Length > 0 Then
                Original_ID = Convert.ToInt32(IDlabel.Text)
            End If

            Original_ID = Convert.ToInt32(GridView1.DataKeys(i).Value)

            If cbox.Checked = True Then
                intUpdateValue = 1
            Else
                intUpdateValue = 0
            End If

            If intUpdateValue = 1 AndAlso Original_ID > -1 Then
                hoursAdapter.UpdateApproveCheckBoxQuery(Original_ID)
            End If
        Next
 
Cracked it!

The problem with the Label was because I hadn't bound the control within the GridView. Because it's an ItemTemplate and isn't used for inserting etc I didn't think I needed to. Someone on the ASP.net forum told me to bind the Label control and not it's all working perfectly.

Thanks a lot for your help, it's really appreciated :)
 
Back
Top Bottom