我有一个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。
答案 0 :(得分:1)
chklstArea.ClientID将为您提供&#34; CheckBoxList&#34;的客户端ID。控制。 要获取单个复选框的clientIds,您可以使用以下代码。
int index = 0;
string checkBoxIDs = ""; //Comma Seperated IDs
foreach (ListItem listItem in chklstArea.Items)
{
checkBoxIDs = chklstArea.ClientID + "_" + index + ",";
index++;
}
答案 1 :(得分: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;
}
}
}
试试这个希望这将解决你的问题。