2012-11-01 76 views
0

我有一個CheckBoxList,我需要在其DataBound事件中獲得其中每個項目的id,我不知道如何獲取它,請幫助我。如何獲取checkboxlists數據綁定事件中每個項目的id?

這裏是我的代碼:

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來獲取該特定區域的房間。

+0

你如何填寫你的項目? –

+0

@AmiramKorach請參閱最新的問題 – Ram

回答

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

試試這個希望,這將解決您的問題。

1

chklstArea.ClientID將爲您提供「CheckBoxList」控件的客戶端ID。 爲了獲得個人複選框的clientIds,你可以使用下面的代碼。

int index = 0; 
string checkBoxIDs = ""; //Comma Seperated IDs 
foreach (ListItem listItem in chklstArea.Items) 
{ 
    checkBoxIDs = chklstArea.ClientID + "_" + index + ","; 
    index++; 
} 
相關問題