Search code examples
c#asp.netcheckboxlistdatabound

how to get id of every item in checkboxlists databound event?


I have a CheckBoxList and I need to get the id of each item in it on its DataBound event and I don't know how to get it, please help me.

Here is my code:

HTML:

<asp:CheckBoxList ID="chklstArea"
                  RepeatColumns="6" 
                  RepeatDirection="Vertical" 
                  runat="server"
                  ondatabound="chklstArea_DataBound">
</asp:CheckBoxList>

here is code behind code:

 protected void drpLocation_SelectedIndexChanged(object sender, EventArgs e)
 {
    if (drpLocation.SelectedItem.Value != "")
    {
        lbtnSelectArea.Visible = true;
        objAreaNew = new ClsAreaNew();
        ClsAreaNewProp objAreaNewProp = new ClsAreaNewProp();
        objAreaNewProp.LocationId = Convert.ToInt64(drpLocation.SelectedItem.Value);
        DataTable dtAreaByLocId = objAreaNew.GetAllAreaListByLocID(objAreaNewProp);
        if (dtAreaByLocId.Rows.Count > 0)
        {
            divAreaListingHeader.Visible = true;
            chklstArea.DataSource = dtAreaByLocId;
            chklstArea.DataTextField = "AreaName";
            chklstArea.DataValueField = "areaid";
            chklstArea.DataBind();
            lblStatusMessage.Text = "";
        }
        else
        {
            divAreaListingHeader.Visible = false;
            dtAreaByLocId = null;
            chklstArea.DataSource = dtAreaByLocId;
            chklstArea.DataTextField = "AreaName";
            chklstArea.DataValueField = "areaid";
            chklstArea.DataBind();
            lblStatusMessage.Text = "This Location does not have any area.";
        }
    }
    else
    {
        lbtnSelectArea.Visible = false;
        divAreaListingHeader.Visible = false;

        chklstArea.DataSource = null;
        chklstArea.DataTextField = "AreaName";
        chklstArea.DataValueField = "areaid";
        chklstArea.DataBind();
        lblStatusMessage.Text = "Please select location.";
    }
}

Actually what i need to do is: i need to bind another checkbox list on the bases of id of items binding in this checkbox list. like here i am binding areas. now i want to bind another checkbox list of rooms the id of area id i want to use to get rooms of that particular area.


Solution

  • According to your requirement and according to my understand you need to use repeater or any other databound control containing the checkbox list. as below:

    HTML Code is:

    <div class="outerlin" id="divAreaListingHeader" runat="server" style="margin-top: 15px;
                                width: 99%;">
                                <div class="maintitle">
                                    Areas</div>
                                <br />
                                <span style="float: left; padding-left: 7px;">
                                    <input type="checkbox" id="chkAll" />Select All<br />
                                </span>
                                <div id="divAreaListingByLocation">
                                    <asp:CheckBoxList ID="chklstArea" RepeatColumns="6" RepeatDirection="Vertical" runat="server">
                                    </asp:CheckBoxList>
                                </div>
                            </div>
    <div class="outerlin" id="divRoomListingHeader" style="margin-top: 15px; width: 99%;">
                                <asp:Repeater ID="repRooms" runat="server" OnItemDataBound="repRooms_ItemDataBound">
                                    <ItemTemplate>
                                        <div class="maintitle">
                                            Rooms</div>
                                        <br />
                                        <asp:Label ID="lblAreaName" ForeColor="Green" BackColor="Red" runat="server"></asp:Label>
                                        <br />
                                        <br />
                                        <span style="float: left; padding-left: 7px;">
                                            <asp:CheckBox runat="server" ID="Checkbox1" Text="Select All" />
                                            <%--<input type="checkbox" runat="server" id="Checkbox1" />Select All--%><br />
                                        </span>
                                        <br />
                                        <br />
                                        <div id="divRoomListingByLocation">
                                            <asp:CheckBoxList ID="chkRoomList" RepeatColumns="6" RepeatDirection="Vertical" runat="server">
                                            </asp:CheckBoxList>
                                        </div>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </div>
    

    And code behind is:

    protected void repRooms_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ClsRoomNew objRoom = new ClsRoomNew();
        CheckBoxList chkRoomList = (CheckBoxList)e.Item.FindControl("chkRoomList");
        CheckBox Checkbox1 = (CheckBox)e.Item.FindControl("Checkbox1");
        Label lblAreaName = (Label)e.Item.FindControl("lblAreaName");
    
        if (chkRoomList != null)
        {
            DataTable dt = objRoom.RoomListingByAreaId(Convert.ToInt64(DataBinder.Eval(e.Item.DataItem, "Areaid")));
            if (dt.Rows.Count > 0)
            {
                lblAreaName.Text = DataBinder.Eval(e.Item.DataItem, "Areaname").ToString();
                chkRoomList.DataSource = dt;
                chkRoomList.DataTextField = "RoomName";
                chkRoomList.DataValueField = "RoomId";
                chkRoomList.DataBind();
            }
            else
            {
                Checkbox1.Visible = false;
                chkRoomList.Visible = false;
            }
        }
    }
    

    try this hope this will solve your problem.