学习顺序:1、如何绑定数据  2、如何设置绑定项  3、如何取出数据

1、RadioButton - 单选按钮  RadioButtonList - 单选按钮组

控件中的ID生成了相同名字的 ID、Name、Value

编译前

<asp:RadioButton ID="RadioButton1" runat="server" Text="男" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="女" />

页面展示  单选按钮之间不互斥,自动生成name,默认name不同(李献策lxc)

编译后

<input id="RadioButton1" type="radio" name="RadioButton1" value="RadioButton1" /><label for="RadioButton1">男</label>
<input id="RadioButton2" type="radio" name="RadioButton2" value="RadioButton2" /><label for="RadioButton2">女</label>

属性:

  GroupName - 分组

<asp:RadioButton ID="RadioButton1" runat="server" Text="男" GroupName="sex" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="女" GroupName="sex" />

RadioButtonList - 单选按钮组(李献策lxc)

一、绑定数据

新建一个类

1、自带构造函数  2、没有命名空间

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<States> slist = new List<States>(); States s1 = new States() { Code = "", Name = "张店" };
States s2 = new States() { Code = "", Name = "淄川" };
States s3 = new States() { Code = "", Name = "博山" };
States s4 = new States() { Code = "", Name = "桓台" };
States s5 = new States() { Code = "", Name = "临淄" };
States s6 = new States() { Code = "", Name = "周村" }; slist.Add(s1);
slist.Add(s2);
slist.Add(s3);
slist.Add(s4);
slist.Add(s5);
slist.Add(s6); RadioButtonList1.DataSource = slist;
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "Code";
RadioButtonList1.DataBind(); } }

为复选按钮组绑定信息

网页展示

后台代码

<div>

            <table id="RadioButtonList1">
<tr>
<td><input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="" /><label for="RadioButtonList1_0">张店</label></td>
</tr><tr>
<td><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="" /><label for="RadioButtonList1_1">淄川</label></td>
</tr><tr>
<td><input id="RadioButtonList1_2" type="radio" name="RadioButtonList1" value="" /><label for="RadioButtonList1_2">博山</label></td>
</tr><tr>
<td><input id="RadioButtonList1_3" type="radio" name="RadioButtonList1" value="" /><label for="RadioButtonList1_3">桓台</label></td>
</tr><tr>
<td><input id="RadioButtonList1_4" type="radio" name="RadioButtonList1" value="" /><label for="RadioButtonList1_4">临淄</label></td>
</tr><tr>
<td><input id="RadioButtonList1_5" type="radio" name="RadioButtonList1" value="" /><label for="RadioButtonList1_5">周村</label></td>
</tr>
</table> </div>

后台代码

1、自动按照索引排序  2、相同的name  3、自动生成value  

4、Table 布局(弊端:1、Table布的局,如果要将某个部分换到其他位置,则整个Table要重新做  2、搜索引擎是无法趴到Table内的内容)

二、设置选定项

1、通过索引选中

RadioButtonList1.SelectedIndex = 0;

如果是最后一个(李献策lxc)

RadioButtonList1.SelectedIndex = slist.Count - 1;

2、通过Value值选中

RadioButtonList1.SelectedValue = "001";

3、如何通过Text来设置选中项?

foreach(ListItem li in RadioButtonList1.Items)
{
if (li.Text == "临淄")
{
li.Selected = true;
}
}

通过遍历设置选中项

三、取出数据

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<States> slist = new List<States>(); States s1 = new States() { Code = "", Name = "张店" };
States s2 = new States() { Code = "", Name = "淄川" };
States s3 = new States() { Code = "", Name = "博山" };
States s4 = new States() { Code = "", Name = "桓台" };
States s5 = new States() { Code = "", Name = "临淄" };
States s6 = new States() { Code = "", Name = "周村" }; slist.Add(s1);
slist.Add(s2);
slist.Add(s3);
slist.Add(s4);
slist.Add(s5);
slist.Add(s6); RadioButtonList1.DataSource = slist;
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "Code";
RadioButtonList1.DataBind(); foreach (ListItem li in RadioButtonList1.Items)
{
if (li.Text == "临淄")
{
li.Selected = true;
}
}
} Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = RadioButtonList1.SelectedValue;
} }

设置按钮事件取出value值

void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = RadioButtonList1.SelectedItem.Text;
}

设置按钮事件取出Text值则为

======================================================

2、CheckBoxList - 复选按钮组

一、绑定数据

绑定

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<States> slist = new List<States>(); States s1 = new States() { Code = "", Name = "张店" };
States s2 = new States() { Code = "", Name = "淄川" };
States s3 = new States() { Code = "", Name = "博山" };
States s4 = new States() { Code = "", Name = "桓台" };
States s5 = new States() { Code = "", Name = "临淄" };
States s6 = new States() { Code = "", Name = "周村" }; slist.Add(s1);
slist.Add(s2);
slist.Add(s3);
slist.Add(s4);
slist.Add(s5);
slist.Add(s6); CheckBoxList1.DataSource = slist;
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataValueField = "Code";
CheckBoxList1.DataBind(); foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Text == "临淄")
{
li.Selected = true;
}
}
} Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = CheckBoxList1.SelectedItem.Text;
} }

数据

二、设置选定项

同RadioButtonList

三、取出数据

void Button1_Click(object sender, EventArgs e)
{
string s="";
foreach(ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
s += li.Text;
}
}
TextBox1.Text = s;
}

取出数据

===========================================================

RadioButtonList 与 CheckButtonList 的属性

  RepeatDirection:项的布局方向

    Horizontal:横向排列  Vertical:纵向排列

  RepeatLayout:项是否在某个表或流入中重复

    Talbe:数据在表格中  Flow:数据在流式布局中  (李献策lxc)

    OrderedList:数据按照有序列表排列(只能纵向排列)  UnorderedList:数据在无序列表中排列(只能纵向排列)

  RepeatColumns:用于布局项的列数

===========================================================

3、下拉列表DropDownList - 对应单选按钮组

WinForm - ComboBox  WebForm - DropDownList

一、绑定数据

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<States> slist = new List<States>(); States s1 = new States() { Code = "", Name = "张店" };
States s2 = new States() { Code = "", Name = "淄川" };
States s3 = new States() { Code = "", Name = "博山" };
States s4 = new States() { Code = "", Name = "桓台" };
States s5 = new States() { Code = "", Name = "临淄" };
States s6 = new States() { Code = "", Name = "周村" }; slist.Add(s1);
slist.Add(s2);
slist.Add(s3);
slist.Add(s4);
slist.Add(s5);
slist.Add(s6); DropDownList1.DataSource = slist;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind(); }
}

绑定数据

二、设置选定项

同上

三、取出选定项

同时

属性

=========================================================

4、ListBox - 对应复选按钮组,默认单选

属性:

  SelectionMode:选择模式

    Single:单选  Multiple:多选

C#-WebForm-复合控件的更多相关文章

  1. webform 复合控件

    RadioButtonList  单选按钮列表 属性:RepeatColumns 用于布局项的列数(每一行的个数) RepeatDirection 选择Vertical,纵向排列:选择Horizont ...

  2. 【2017-05-19】WebForm复合控件

    自动提交的属性: AutoPostBack="True" 1.RadioButtonList     单选集合 -属性:RepeatDirection:Vertical (垂直排布 ...

  3. WebForm复合控件RadioButtonList、CheckBoxList、DropDownList

    1.RadioButtonList     单选集合 -属性:RepeatDirection:Vertical (垂直排布)/Horizontal (横向排布) RepeatLayout:Table ...

  4. 【2017-05-19】WebForm复合控件、用DropDownList实现时间日期选择。

    自动提交的属性: AutoPostBack="True" 1.RadioButtonList     单选集合 -属性:RepeatDirection:Vertical (垂直排布 ...

  5. Webform(简单控件、复合控件)

    一.简单控件: 1.label控件 <asp:Label ID="Label1" runat="server" Text="账 号:" ...

  6. webform简单、复合控件

    简单控件: 1.Label 会被编译成span标签 属性: Text:文本内容 CssClass:CSS样式 Enlabled:是否可用 Visible:是否可见 2.Literal 空的,C#会把里 ...

  7. WebForm简单控件,复合控件

    简单控件: 1.Label 会被编译成span标签 属性: Text:文本内容 CssClass:CSS样式 Enlabled:是否可用 Visible:是否可见 __________________ ...

  8. WebForm 简单控件、复合控件

    简单控件: Label:被编译成span 样式表里设置lable的高度:  display:inline-block; Text  --文本 ForeColor  --字体颜色 Visible  -- ...

  9. WebForm 【复合控件】

    一 复合控件(取值,赋值用法相近)  RadioButtonList      --单选按钮 (一组列表)  <asp:RadioButtonList ID="RadioButtonL ...

  10. webform(复合控件)

    一.组合单选 RadioButtonList 单选按钮与简单控件不同,可理解为在集合中放置多对象 例: <asp:RadioButtonList ID="RadioButtonList ...

随机推荐

  1. C语言实现2个大数相加。

    #include<stdio.h>#include<string.h>int main(){    char s1[100],s2[100];    int num1[31], ...

  2. QML 从无到有 (基础)

    小公司,没办法,什么都得自己亲自来. 服务端是MVC,现在需要可PC客户端和移动APP. 考虑到网页应用有很多界面框架,可以做出很漂亮的界面来,就尝试着使用nwjs来实现,可是在使用了2天的nwjs后 ...

  3. C++_系列自学课程_第_12_课_语句_《C++ Primer 第四版》

    前面的文章说完了表达式和类型转换的部分内容,在我参考的书里面,接下来讨论的是各种语句,包括:顺序语句.声明语句.复合语句(块语句).语句作用域 .if语句.while语句.for语句.do...whi ...

  4. 打造自定Select样式

    打造自定Select样式 我们为什么要自定义select样式? 1.select最大的一个缺陷就是不能自定义下拉按钮的样式. 效果图: 在线演示地址: http://www.smallui.com/j ...

  5. 将SHP导入MySQL中

    ogr2ogr -f MySQL MySQL:smfs,host=127.0.0.1,user=root,password=gis D:\spatialData\HB\HuBeiPicture\HuB ...

  6. linux安装中文语言包

    相关配置如下: yum install fonts-chinese.noarch yum install m17n-db-common-cjk yum install m17n-db-chinese安 ...

  7. 在View and Data API中更改指定元素的颜色

    大家在使用View and Data API开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...

  8. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

  9. PHP对象在内存堆栈中的分配

    对象在PHP里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上说大体上是分为4段,栈空间段.堆空间段. ...

  10. 强大的flash头像上传插件(支持旋转、拖拽、剪裁、生成缩略图等)

    今天介绍的这款flash上传头像功能非常强大,支持php,asp,jsp,asp.net 调用 头像剪裁,预览组件插件. 本组件需要安装Flash Player后才可使用,请从http://dl.pc ...