protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TextDataContext context = new TextDataContext(); CheckBoxList1.DataSource = context.Nation; CheckBoxList1.DataTextField = "Name"; CheckBoxList1.DataValueField = "Code"; CheckBoxList1.DataBind(); } } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { //设置点击全选和取消全选 foreach( ListItem item in CheckBoxList1.Items ) { item.Selected = CheckBox1.Checked; } } protected void Button1_Click(object sender, EventArgs e) { //取值 string zhi = ""; foreach( ListItem item in CheckBoxList1.Items ) { if(item.Selected) { zhi += item.Value.ToString()+",";//用,分割 } } //去掉最后多余的逗号 zhi = zhi.Substring(0, zhi.Length - 1); //拆分字符串,把选中的每一项都弹出一个窗体 string[] codes; codes = zhi.Split(‘,‘); //造js代码 string js = "<script type=‘text/javascript‘>"; for (int i = 0; i < codes.Length; i++) { js += "alert(‘"+codes[i]+"‘);"; } js += "</script>"; Literal1.Text = js; //弹出一个窗体,显示选中的值,借助一个工具Literall,在Literall中写js代码 //Literal1.Text = "<script type=‘text/javascript‘>alert(‘" + zhi + "‘)</script>"; }
时间: 2024-10-29 06:35:20