12.16单选框,复选框

1:单选框 RadioButton
属性: GroupName 组名 想要产生互斥效果:将GroupName设置成统一名字

看是否选中: bool sex = RadioButton1.Checked;
Label1.Text = sex.ToString();


/ /选中
bool sex = RadioButton1.Checked;
//Label1.Text = sex.ToString();
if (sex == true)
{ Label1.Text = "女"; }
else
{
Label1.Text = "男";
}

2:单选按钮列表: RadioButtonList
看是否选中: bool sex = RadioButton1.Checked;

绑定数据:
if (!IsPostBack)
{
NationDataContext context = new NationDataContext();
RadioButtonList1.DataSource = context.Nation;
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "Code";

RadioButtonList1.DataBind();

}
取选中项的值:
Label2.Text = RadioButtonList1.SelectedValue.ToString();
设置那一项被选中;
只能设置一项被选中:RadioButtonList1.SelectedIndex = 1;

 

3:复选框:checkbox


4:复选框列表
属性:repeatdrection:项的布局方向
看是否选中: bool sex = CheckBoxList1.Checked;


绑定数据:
if (!IsPostBack)
{
NationDataContext context = new NationDataContext();
CheckBoxList1.DataSource = context.Nation;
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataValueField = "Code";

CheckBoxList1.DataBind();


}
取选中项的值:

protected void Button3_Click(object sender, EventArgs e)
{
foreach (ListItem item in CheckBoxList1.Items)
{

if (item.Selected)
{
Label3.Text += item.Text;

}
}

s设置哪项选中:
如果设置一项选中:checkBoxlist.selectedindex=2;
如果设置多项被选中:
protected void Button3_Click(object sender, EventArgs e)
{
foreach (ListItem item in CheckBoxList1.Items)
{

if (item.Selected)
{
Label3.Text += item.Value;//显示编号code
Label4.Text += item.Text;//显示民族name

}
}

 

练习,全选,有弹窗

1:全部选中一次弹出

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);
//Literal1.Text = "<script type='text/javascript'> alert('"+zhi+"')</script>";//全部选中 实现弹窗=
}
}

 


2 选中后,按照选中的号码一个一个的弹出

 

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);
//Literal1.Text = "<script type='text/javascript'> alert('"+zhi+"')</script>";//全部选中 实现弹窗=

//拆分字符串

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

 

posted @ 2015-12-16 08:55  小热包1  阅读(158)  评论(0编辑  收藏  举报