Search code examples
c#asp.netvb.netcode-behindaspxgridview

Do an update statement when Value of a databound Templatefield(checkbox) is changed


I have a template field which is databound like this :

                       <asp:TemplateField HeaderText="NewLine">
                        <HeaderStyle CssClass="TDAll" />
                        <ItemStyle BorderStyle="Solid" HorizontalAlign="Center" CssClass="TDAll" />
                        <ItemTemplate>                                
                            <asp:CheckBox ID="chkNewLine" runat="server" AutoPostBack="True" Checked='<%# Bind("NewLine") %>' CommandArgument='<%# Eval("QuestionPartNumber") %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>

Whenever the checkbox is checked or unchecked I want to write an update statement in codebehind.

Could anyone suggest in which event I can write this update statement keeping in mind that i need the datakey value while writing the update query

My update statemnt will look something like this

        For Each row As GridViewRow In DGSubQuestions.Rows
        Dim cb As CheckBox = row.FindControl("NewLine")
        QuestPartID = DGSubQuestions.DataKeys(e.CommandArgument).Value

        SQLHelper.NonQuery("UPDATE [GPsSubQuestions] SET [NewLine] =" & cb.Checked & " WHERE QuestPartID = @QuestPartID", _
        New SqlParameter("@QuestPartID", QuestPartID))

    Next

Solution

  • You have to use CheckedChanged Event ....

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged%28v=vs.110%29.aspx