0

我有一个CheckBoxList,我需要id在其DataBound活动中获取其中的每个项目,但我不知道如何获取它,请帮助我。

这是我的代码:

HTML:

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

这是代码背后的代码:

 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.";
    }
}

实际上我需要做的是:我需要在此复选框列表中绑定的项目 id 的基础上绑定另一个复选框列表。像这里我是绑定区域。现在我想绑定另一个房间的复选框列表,我想使用区域 id 的 id 来获取该特定区域的房间。

4

2 回答 2

1

chklstArea.ClientID 将为您提供“CheckBoxList”控件的客户端 ID。对于获取单个复选框的 clientId,您可以使用以下代码。

int index = 0;
string checkBoxIDs = ""; //Comma Seperated IDs
foreach (ListItem listItem in chklstArea.Items)
{
  checkBoxIDs = chklstArea.ClientID + "_" + index + ",";
  index++;
}
于 2012-11-01T10:01:56.437 回答
0

根据您的要求和我的理解,您需要使用中继器或任何其他包含复选框列表的数据绑定控件。如下:

HTML代码是:

<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>

后面的代码是:

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;
        }
    }
}

试试这个希望这能解决你的问题。

于 2012-11-01T10:59:56.190 回答