asp.net CheckBoxList 取值與勾選,複選框後台控制前台checkbox選中
來源:互聯網
上載者:User
前台: Html代碼 <asp:CheckBoxList ID = "cblJL" runat = "server" RepeatDirection="Horizontal" Width="100%"> <asp:ListItem>目前記錄</asp:ListItem> <asp:ListItem>當前頁</asp:ListItem> <asp:ListItem>全部記錄</asp:ListItem> </asp:CheckBoxList> 後台3種方式: 第一種:獲得CheckBox的值 C#代碼 string save_cblJL = ""; for (int i = 0; i < this.cblJL.Items.Count; i++) { if (this.cblJL.Items[i].Selected == true) { save_cblJL += this.cblJL.Items[i].Value + ","; } } Response.Write(save_cblJL); 第二種:獲得CheckBox的值 C#代碼 string str_Save_cblJL = ""; foreach (ListItem li in cblJL.Items) { if (li.Selected == true) { str_Save_cblJL += li.Value + ","; } } Response.Write(str_Save_cblJL); 第三種:從後台給前台CheckBox賦值 C#代碼 string strapp = "目前記錄,當前頁,全部記錄,"; string[] strtemp = strapp.Split(','); foreach (string str in strtemp) { for (int i = 0; i < cblJL.Items.Count; i++) { if (this.cblJL.Items[i].Value == str) { this.cblJL.Items[i].Selected = true; } } }