Jun Rikson
Jun Rikson

Reputation: 1884

CheckBox.Checked always return false in Repeater

I have problem with CheckBox in Repeater always return False Value. I have read many problems about this and always pointing me to binding the data with Not Page.IsPostBack, and I have done all the sample problem that I found but still didn't fix my problem.

Here how I bind the checkbox :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        If Not Page.IsPostBack Then
            Call Load_menu()
        End If
    Catch ex As Exception
        Response.Write("Page_Load Exception :<br>" & ex.ToString)
    End Try
End Sub

Private Sub Load_menu()
    Try
        sqlstring = "SELECT a.menuID, a.name, b.[status] " & _
                    "FROM masterMenu a LEFT JOIN rolesDetail b ON b.menuID=a.menuID " & _
                    "WHERE a.[status] = 1 and b.RoleID = '" & roleID & "' " & _
                    "ORDER BY a.menuID "
        DS = SQLExecuteQuery(sqlstring)
        DT = DS.Tables(0)
        rptMenu.DataSource = DT
        rptMenu.DataBind()
    Catch ex As Exception
        Response.Write("Load_Menu Exception :<br>" & ex.ToString)
    End Try
End Sub

Private Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
    Try
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lblID As Label = e.Item.FindControl("LblID")
            Dim lblName As Label = e.Item.FindControl("LblName")
            Dim cblChild As CheckBox = e.Item.FindControl("cblChild")
            Dim dr As DataRowView = e.Item.DataItem

            lblID.Text = dr.Item("menuID").ToString
            lblName.Text = dr.Item("name").ToString

            If dr.Item("status").ToString = "1" Then
                cblChild.ID = dr.Item("menuID").ToString
                cblChild.Checked = True
            Else
                cblChild.ID = dr.Item("menuID").ToString
                cblChild.Checked = False
            End If
        End If
    Catch ex As Exception
        Throw New Exception("<b>Error RptMenu Data bound :</b>" & ex.ToString)
    End Try
End Sub

And here how I render the repeater :

<form id="form1" runat="server" class="form-horizontal">
                <div class="box-header">
                <h3 class="box-title"><asp:Label ID="lblHeader" runat="server"></asp:Label></h3>
                </div>
                <div class="box-body no-padding">
    <asp:Repeater ID="rptMenu" runat="server">
        <HeaderTemplate>
                    <table class="table table-striped">
                        <tr>
                            <th style="width: 10px">#</th>
                            <th>ID</th>
                            <th>Nama</th>
                        </tr>
        </HeaderTemplate>
        <ItemTemplate>
                        <tr>
                            <td><asp:CheckBox ID="cblChild" runat="server"></asp:CheckBox></td>
                            <td><asp:Label ID="LblID" runat="server"></asp:Label></td>
                            <td><asp:Label ID="LblName" runat="server"></asp:Label></td>
                        </tr>
        </ItemTemplate>
      <FooterTemplate>
                    </table>
      </FooterTemplate>
    </asp:Repeater>     
                  <div class="box-footer text-center">
                      <dxe:aspxbutton id="btSimpan" runat="server" text="Simpan" cssclass="btn btn-primary" enabledefaultappearance="False"></dxe:aspxbutton>
                  </div>
                </div>      
</form>

And here how the Submit button handle it :

Protected Sub btSimpan_Click(sender As Object, e As EventArgs) Handles btSimpan.Click
    Try
        sqlstring = ""
        For i As Integer = 0 To rptMenu.Items.Count - 1
            Dim item As RepeaterItem = rptMenu.Items(i)
            Dim lblID As Label = item.FindControl("LblID")
            Dim cblChild As CheckBox = item.FindControl("cblChild")
            Dim status As String = "0"

            If cblChild.Checked Then
                status = "1"
            End If

            sqlstring = sqlstring & " UPDATE rolesDetail SET [status] = " & status & " " & _
                        " WHERE roleID = '" & roleID & "' AND menuID = '" & lblID.Text.ToString.Replace("'", "''") & "'; "
        Next

        If SQLExecuteNonQuery(sqlstring) > 0 Then
            Response.Redirect("roles.aspx")
        End If
    Catch ex As Exception
        Response.Write("Error btSimpan_Click <BR> " & ex.ToString)
    End Try
End Sub

I have trace it using Profiler in SQL Server and it always return 0 values which mean the cblChild.Checked always return False.

I don't know what I missed here.

Upvotes: 2

Views: 778

Answers (1)

M_Idrees
M_Idrees

Reputation: 2172

Try using HtmlInputCheckBox control instead of CheckbBox

Change this line

Dim cblChild As CheckBox = item.FindControl("cblChild")

to

Dim cblChild As HtmlInputCheckBox = DirectCast(item.FindControl("cblChild"), HtmlInputCheckBox)

This also needs to use html checkbox control with runat="server" :

<input type="checkbox" runat="server" id='cblChild'/>

Since we assigning ID for checkbox from html. Remove setting this ID in rptMenu_ItemDataBound event, this line should be removed:

cblChild.ID = dr.Item("menuID").ToString

Upvotes: 2

Related Questions