Link to home
Start Free TrialLog in
Avatar of TrialUser
TrialUserFlag for Afghanistan

asked on

disable all checkboxes inside a nested datalist control when one checkbox inside the datalist is clicked

I have a datalistparent and a datalistchild . Inside the datalistchild itemtemplate, I have checkboxes. When the user clicks on a checkbox then all checkboxes inside that datalistchild should be disabled. all in server side code. I have attached the html and .vb code. Please help. Thanks

 Protected Sub Value_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = DirectCast(sender, CheckBox)
        Dim intValueID As Integer = DirectCast(chk.Parent.FindControl("lblAttributeValueID"), Label).Text
        Dim intNameID As Integer = DirectCast(chk.Parent.FindControl("lblAttrNameID"), Label).Text
        If chk.Checked Then
            AddNavigationProductFilter(intNameID, intValueID, True)
            'here diable all checkboxes in this particular item template (dlAttributeValues)
        Else
            AddNavigationProductFilter(intNameID, intValueID, False)
            'here enable all checkboxes in this particular item template (dlAttributeValues)
        End If
        BindProductFinderItems()
    End Sub


    <div class="Attributes">
<asp:DataList ID="dlAttributes" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" Width="100%">
<ItemStyle Width="33.3%"/>
    <ItemTemplate>    
    <asp:Label ID="lblAttributeNameID" runat="server" Visible = "false" Text='<%# DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
        <span style="color:#58595B;font-size:14px;font-weight:bold;"><%# DataBinder.Eval(Container.DataItem, "ShortName")%></span>    
        <div style="height:100px;overflow:auto; border: 1px solid #CACACA;margin:10px 10px 10px 0px;padding:10px;">
           <asp:DataList ID="dlAttributeValues" runat="server">
              <ItemTemplate>
                    <asp:Label ID="lblAttrNameID" runat="server" Visible = "false" Text='<%# DataBinder.Eval(Container.DataItem, "AttributeNameID")%>'></asp:Label>
                    <asp:Label ID="lblAttributeValueID" runat="server" Visible = "false" Text='<%# DataBinder.Eval(Container.DataItem, "AttributeValueID")%>'></asp:Label>
                    <asp:checkbox ID="chkValue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"AttributeValue")%>' AutoPostBack="true" OnCheckedChanged="Value_Clicked"/>
                </ItemTemplate>
            </asp:DataList>
        </div>      
    </ItemTemplate>
</asp:DataList>
</div>
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Try following


     Dim dList As DataList = chk.Parent.FindControl("dlAttributeValues")
 If chk.Checked Then
     AddNavigationProductFilter(intNameID, intValueID, True)
     'here diable all checkboxes in this particular item template (dlAttributeValues)
     For Each Item in dList.Items
         Dim chk As Checkbox = Item.FindControl("chkValue")
         cbk.Enabled = false
     Next
 Else
     AddNavigationProductFilter(intNameID, intValueID, False)
     'here enable all checkboxes in this particular item template (dlAttributeValues)
     For Each Item in dList.Items
         Dim chk As Checkbox = Item.FindControl("chkValue")
         cbk.Enabled = true
     Next
 End If

Open in new window

Try this mate ..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
<asp:DataList ID="dlAttributes" runat="server" DataKeyField="ID" RepeatDirection="Horizontal" RepeatColumns="3" Width="100%">
<ItemStyle Width="33.3%"/>
    <ItemTemplate>
     <asp:HiddenField ID="parentRowIndex" runat="Server" value='<%# Container.ItemIndex + 1 %>' />   
    <asp:Label ID="lblAttributeNameID" runat="server" Visible = "false" Text='<%# DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
        <span style="color:#58595B;font-size:14px;font-weight:bold;"><%# DataBinder.Eval(Container.DataItem, "name")%></span>    
        <div style="height:100px;overflow:auto; border: 1px solid #CACACA;margin:10px 10px 10px 0px;padding:10px;">
            <asp:DataList ID="dlAttributeValues" runat="server" DataSource='<%# getTable() %>' OnItemCommand="DlChild_ItemCommand">
                <ItemTemplate>
                    <asp:Label ID="lblAttributeValueID" runat="server" Visible = "true" 
                    Text='<%# DataBinder.Eval(Container.DataItem, "salary")%>'></asp:Label>
                    <asp:checkbox ID="chkValue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"degree")%>' 
                    AutoPostBack="true" OnCheckedChanged="GetLabel" />
                </ItemTemplate>
            </asp:DataList>
        </div>       
    </ItemTemplate>
</asp:DataList>
    </div>
    </form>
</body>
</html>

Open in new window


Imports System.Data

Partial Class dataListCheckBoxFireEvent
    Inherits System.Web.UI.Page
    Dim ds As New DataSet
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ds.ReadXml(Server.MapPath("~/xmlfile2.xml"))
        If Not IsPostBack Then
            dlAttributes.DataSource = ds
            dlAttributes.DataBind()
        End If
    End Sub

  
    Protected Sub GetLabel(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = DirectCast(sender, CheckBox)
        Dim parentIndex As Integer = getParentIndex(chk.ClientID)
        Dim dsAttValue As DataList = DirectCast(dlAttributes.Items(parentIndex).FindControl("dlAttributeValues"), DataList) 'This finds respect child datalist 
        If chk.Checked Then
            For Each Items1 In dsAttValue.Items
                Dim childCheckBox As CheckBox = Items1.FindControl("chkValue") 'Credits to CodeCruiser
                childCheckBox.Enabled = False
            Next
        End If
    End Sub

    Protected Function getTable() As DataTable
        Dim dsChild As New DataSet
        dsChild.ReadXml(Server.MapPath("~/xmlfile2.xml"))
        Return dsChild.Tables(0)
    End Function

    Function getParentIndex(ByVal id As String) As String
        Dim str As String
        Dim i As Integer
        str = id
        Dim chkNext As Integer = 0
        Dim parentIndex As String = ""
        For i = 1 To Len(str)
            If IsNumeric(Mid(str, i, 1)) Then 
                parentIndex += Mid(str, i, 1) 
                chkNext = i
            End If
            If parentIndex <> "" And chkNext <> i Then 
                Exit For
            End If
        Next i
        Return parentIndex
    End Function

End Class

Open in new window

XMLFile2.xml
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is the proper nested datalist , .

<asp:DataList ID="dlAttributes" runat="server" DataKeyField="ID" RepeatDirection="Horizontal" RepeatColumns="3" Width="100%">
<ItemStyle Width="33.3%"/>
    <ItemTemplate>
    <asp:Label ID="lblAttributeNameID" runat="server" Visible = "false" Text='<%# DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
        <span style="color:#58595B;font-size:14px;font-weight:bold;"><%# DataBinder.Eval(Container.DataItem, "name")%></span>    
        <div style="height:100px;overflow:auto; border: 1px solid #CACACA;margin:10px 10px 10px 0px;padding:10px;">
            <asp:DataList ID="dlAttributeValues" runat="server" DataSource='<%# getTable() %>' >
                <ItemTemplate>
                    <asp:Label ID="lblAttributeValueID" runat="server" Visible = "true" 
                    Text='<%# DataBinder.Eval(Container.DataItem, "salary")%>'></asp:Label>
                    <asp:checkbox ID="chkValue" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"degree")%>' 
                    AutoPostBack="true" OnCheckedChanged="GetLabel" />
                </ItemTemplate>
            </asp:DataList>
        </div>       
    </ItemTemplate>
</asp:DataList>

Open in new window