c#中checkbox怎么添加项

c#中checkbox怎么添加项,第1张

1)在Visual studio中新建一个WindowsApplication。在Form1上布置两个控件textBox1和button1,如下图

2)后台代码

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Linq

using System.Text

using System.Windows.Forms

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        // 定义一个checkbox

        CheckBox chkBox

        public Form1()

        {

            InitializeComponent()

        }

        // 点击button1

        private void button1_Click(object sender, EventArgs e)

        {

            // 如果chkBox已经加载,则退出

            if (chkBox != null) return

            // 如果textBox1没有内容输入则退出

            if(textBox1.Text.Trim()==string.Empty) return

            

            // 动态加载一个Checkbox

            chkBox = new CheckBox()

            chkBox.Left = 10

            chkBox.Top = 10

            

            // 将textBox1输入内容作为checkbox的提示

            chkBox.Text = textBox1.Text

            // 点击checkbox时的事件处理

            chkBox.Click += chkBox_Click

            this.Controls.Add(chkBox)

        }

        void chkBox_Click(object sender, EventArgs e)

        {

            if (chkBox.Checked)

            {

                MessageBox.Show(chkBox.Text + "被选中!")

            }

            else

            {

                MessageBox.Show(chkBox.Text + "没有选中!")

            }

        }

    }

}

<headertemplate>

<input onClick="checkAll(this)" type="checkbox" name="chkname" value="0">全选

</headertemplate>

<itemtemplate>

<input type="checkbox" value='<%# DataBinder.Eval(Container.DataItem,"ID")%>' name="chkname">

</itemtemplate>

<script>

function checkAll(obj){

var chks=document.getElementById("chkname")

if(obj.checked==true){

for(var ii=0ii<chks.lengthii++){

chks[ii].checked=true

}

}else{

for(var ii=0ii<chks.lengthii++){

chks[ii].checked=false

}

}

}

function getChecked(){

var ids=""

var chks=document.getElementById("chkname")

for(var ii=0ii<chks.lengthii++){

if(chks[ii].checked==true&&chks[ii].value!=0){

var val="'"+chks[ii].value+"',"

ids+=val

}

}

return ids//这就是所有选中项的value.

}

</script>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11531534.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存