3

I have a gridview that has one checkbox for each row of data.

When the checkbox is checked or unchecked, I need to update a bit flag in my database.

I'm trying to use the OnCheckedChanged event.

It does fire, however I am getting a null error which has to do with an ID.

My problem is, I'm not quite sure how to get the needed ID to the OnCheckedChanged event code.

I need this ID to update the appropriate row in my database.

I found a few relavent questions on StackOverflow but none of the supplied answers really helped me out.

Thanks!

My gridview code:

<asp:GridView ID="gvlisting" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Item Is Ready">
        <ItemTemplate>
            <asp:CheckBox ID="isReady" runat="server" AutoPostBack="true" OnCheckedChanged="isReady_CheckedChanged" Checked='<%#isReady(CInt(Eval("isReady")))%>'/>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item Name">
            <ItemTemplate>
                <asp:Label ID="itemName" runat="server" Text='<%#cleanString(Eval("itemName").ToString())%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

code behind:

Public Sub isReady_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim box As CheckBox = DirectCast(sender, CheckBox)
    If box.Checked = True Then
        'custom code for executing an SQL UPDATE statement
        db.ExecuteNonQuery(AddIsreadyFlag, New SqlParameter("@itemID", Me.ID))
    Else
        db.ExecuteNonQuery(RemoveIsreadyFlag, New SqlParameter("@itemID", Me.ID))
    End If

End Sub
SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

3 Answers3

3

Me.ID is the ID of the Page since Me is the current page-instance.

You could add another template-field with the ID:

<asp:TemplateField HeaderText="Item-ID" Visible="false">
    <ItemTemplate>
        <asp:Label ID="itemID" runat="server" Text='<%# Eval("itemID") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Now you can use the NamingContainer to get the row and FindControl to get the label:

Public Sub isReady_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim box As CheckBox = DirectCast(sender, CheckBox)
    Dim row = DirectCast(box.NamingContainer, GridViewRow)
    Dim itemID = DirectCast(row.FindControl("itemID"), Label).Text
    If box.Checked = True Then
        'custom code for executing an SQL UPDATE statement
        db.ExecuteNonQuery(AddIsreadyFlag, New SqlParameter("@itemID", itemID))
    Else
        db.ExecuteNonQuery(RemoveIsreadyFlag, New SqlParameter("@itemID", itemID))
    End If

End Sub
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

You can also try to add a custom attribute in the OnDataBinding event and retrieve the value in de CheckChanged event.

    protected void chkEnabled_DataBinding(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        string id = Eval("Id").ToString();
        chk.Attributes.Add("data-id", id);
    }

    protected void chkEnabled_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        string id = chk.Attributes["data-id"];
    } 
Sego
  • 310
  • 2
  • 5
1

Add the ID column to the grid, then if needed set it's visible property to false.

You can get the Gridview's selected row then get cell the value for ID column (the column does not need to be visisble, but it needs to exist in the grid).

 var someVar  = gvlisting.SelectedRow.Cells[0].Text; //change 0 to correct column

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.110).aspx

You may need to do this in a SelectedIndexChanged but i doubt that, if that is the case you can easily get the data from the cell using the above or findcontrol like...How to find control in TemplateField of GridView?

Community
  • 1
  • 1
fuzzybear
  • 2,325
  • 3
  • 23
  • 45