Scenario :

I have a ASPxGridview on client side and used checkbox control on server side. I have declared a aspxbutton(submit) to insert the checked checkbox value (v=1, empty=0) into database and at the same time count the number of checked checkbox for each row. In the checkboxtemplate, v indicates checked and empty indicates unchecked, and the number in the column indicates day based on the month. I want to count the number of checked checkbox for the emp no :234.

                                           YEAR : 2019      MONTH: 3
      ------------------------------------------------------------------
      |EMP NO| 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|.....|
      ------------------------------------------------------------------
      | 123  | v| | | | | | | | |  |  |  |  |  | v|  |  |  |  |  |.....|
      -------------------------------------------------------------------
   -->| 234  | v| | | | | | | | |  |  |v | v| v|  | v|  |  |  |  |.....|
      ------------------------------------------------------------------                                              
                                                       (Submit) (cancel)

Code for aspxgridview (aspx)

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                            <div class="flexs">                       
                                <dx:ASPxComboBox ID="year" runat="server" AutoPostBack="true" ClientInstanceName="year" ValueType="System.Int32" Width="100px" CssClass="ddstyle mr10px" OnSelectedIndexChanged="SelectedIndexChanged"></dx:ASPxComboBox>
                                <dx:ASPxComboBox ID="month" runat="server" AutoPostBack="true" ClientInstanceName="month" ValueType="System.Int32" Width="100px" CssClass="ddstyle mr10px" OnSelectedIndexChanged="SelectedIndexChanged"></dx:ASPxComboBox>
                                <dx:ASPxComboBox ID="section" runat="server" AutoPostBack="true" ValueType="System.String" Width="100px" CssClass="ddstyle" OnSelectedIndexChanged="SelectedIndexChanged"></dx:ASPxComboBox>
                            </div>
                        </div>
                        <dx:ASPxGridView ID="LeaveSystem" runat="server" AutoGenerateColumns="false" KeyFieldName="EMP_NO;Year;Month" Width="100%">
                            <SettingsBehavior AllowDragDrop="false" AllowSort="false" />
                            <SettingsPager Mode="ShowAllRecords" />
                        </dx:ASPxGridView>

                    </ContentTemplate>
                </asp:UpdatePanel>
                <dx:ASPxCallback ID="cb" ClientInstanceName="cb" runat="server" OnCallback="cb_Callback" ></dx:ASPxCallback>
                <dx:ASPxLabel ID="HiddenEmpID" runat="server" ClientInstanceName="HiddenEmpID" ClientVisible="false"></dx:ASPxLabel>
                <div class="flexs5 mt10px text-center">
                    <dx:ASPxButton ID="submitbtn" runat="server" Text="Submit" OnClick="submitbtn_Click" AutoPostBack="false" CssClass="mr10px"></dx:ASPxButton>
                   </div>

Code vb.net

  Friend Class CheckBoxTemplate
            Implements ITemplate
            Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
                Dim checkbox As New ASPxCheckBox
                Dim gridContainer As GridViewDataItemTemplateContainer = CType(container, GridViewDataItemTemplateContainer)
                'Dim empid As String = gridContainer.Grid.GetRowValues(gridContainer.VisibleIndex, "EmpID").ToString()
        checkbox.AllowGrayed = False
        checkbox.CheckedImage.Url = "images/checked_image.png"
        checkbox.UncheckedImage.Url = "images/unchecked_image.png"
        checkbox.CssClass += "customcheckbox"
        checkbox.ID = "DateCheckBox"
        checkbox.ClientSideEvents.CheckedChanged = String.Format("function (s, e) {{ cb.PerformCallback(HiddenEmpID.GetValue() + '|' + year.GetValue() + '|' + month.GetValue() + '|' + '{0}|' + s.GetChecked()); }}", gridContainer.Column.FieldName)
        checkbox.Value = DataBinder.Eval(gridContainer.DataItem, gridContainer.Column.FieldName)
        If checkbox.Value = 1 Then
            checkbox.Checked = True
        Else
            checkbox.Checked = False
        End If
        checkbox.ValueType = GetType(Int32)
        checkbox.ValueUnchecked = 0
        checkbox.ValueChecked = 1              
container.Controls.Add(checkbox)

            End Sub
        End Class

Protected Sub cb_Callback(source As Object, e As CallbackEventArgs)
    Session("par") = e.Parameter.Split("|"c)
End Sub

Problem :

I have searched online and tried method below , But it doesn't seem to work.

 Protected Sub submitbtn_Click(sender As Object, e As EventArgs)

        ' Dim fieldname1 As String = ""

        If IsPostBack Then

            Dim p() As String = Session("par")


            'Dim p() As String = e.Parameter.Split("|"c)


            'p(0) = empid, p(1) = year, p(2) = month, p(3) = Date, p(4) = Boolean
            If p(4) = True Then
                p(4) = "1"
            Else
                p(4) = "0"
            End If
            Dim query As String = String.Format("UPDATE LeaveSystem SET [{3}] = '{4}', UpdateTime = GETDATE() WHERE EmpID = '{0}' and Year = '{1}' and Month = '{2}' IF @@ROWCOUNT=0 INSERT INTO LeaveSystem (EmpID, Year, Month, [{3}], UpdateTime) values ('{0}', '{1}', '{2}', '{4}', GETDATE())", p(0), p(1), p(2), p(3), p(4))
            SetMssql(query)

        End If


        '' Check exceed 13 days

            Dim count1 As Integer = 0

               For I As Integer = 0 To LeaveSystem.VisibleRowCount - 1

  Dim chkRow As ASPxCheckBox = TryCast(LeaveSystem.FindRowCellTemplateControl(I, Nothing, "cb"), ASPxCheckBox)

            For k As Integer = 2 To LeaveSystem.Columns.Count - 1

                 Dim aheaderCb = TryCast(LeaveSystem.FindHeaderTemplateControl(LeaveSystem.Columns(k), "cb1"), ASPxCheckBox)
            If chkRow.IsVisible = True Then

                If aheaderCb.Checked = True Then

                    count1 = count1 + 1

                End If

            End If

        Next k

    Next I

    End Sub

When executing the submit button, the Object reference not set to an instance of an object error appear on line : If chkRow.IsVisible = True Then. I have attempted for many times but the same outcome appeared. Please guide me on this.

1 Answers

0
RaviMittal On

Try That

  protected void btnGetRecord_Click(object sender, EventArgs e)
        {
            var checkedRows = (from GridViewRow row in myGrid.Rows
                               let checkbox = (CheckBox)row.FindControl("chkSelect")
                               where checkbox != null && checkbox.Checked == true
                               select row).ToList();


        }

You can convert the code into VB.NET.