在这里只写,绑定数据库数据的RadioButtonList控件:

一:

首先,先在数据库中建立一张表:

1 CREATE TABLE KK
2 (
3 id INT,
4 [name] VARCHAR(20),
5 )

然后插入数据:

1 INSERT INTO KK (id, [name]) VALUES (1, '张三')
2 INSERT INTO KK (id, [name]) VALUES (2, '李四')
3 INSERT INTO KK (id, [name]) VALUES (3, '王五')
4 INSERT INTO KK (id, [name]) VALUES (4, '赵六')
5 ·
6 ·
7 ·
8 ·
9 ·

这是最终建立的表:

二:

前台代码:

 <div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"></asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick ="Button1_Click"/><br /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>

*其中:RepeatDirection="Horizontal"是设置其选项横向显示。

后台代码(Page_Load):

 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
/* 为什么判断IsPostBack:当你需要执行一些仅需要在页面第一次浏览时执行的事件
* 比如页面初始化,数据绑定之类的操作时,需要将操作放在if(!IspostBack)里面,
* 这样当你在点击页面中的按钮或者执行其他回发事件时,不贵再次初搜索始化或者
* 重复绑定数据,提高了执行效率。
*/
{
string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string sql = "SELECT * FROM KK"; SqlCommand cmd = new SqlCommand(sql, connection); SqlDataReader sdr = cmd.ExecuteReader(); //任意给的字段名,只要是想显示在界面上的就行。其值给了:Text
this.RadioButtonList1.DataTextField = "name"; //任意给得字段名,只要是想在后台看到前台看不到的数据就行。其值给了:Value
this.RadioButtonList1.DataValueField = "id";//此字段可以去掉。value的值默认为Text的值。 this.RadioButtonList1.DataSource = sdr; this.RadioButtonList1.DataBind(); sdr.Close(); connection.Close();
}
}

后台代码(Button1):

 /// <summary>
/// Button1按钮的单机事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string s = string.Empty; for (int i = ; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected)
{
s = RadioButtonList1.Items[i].Text;
}
} this.Label1.Text = "你选中的是:" + s;
}

最终效果:

以上就是RadioButtonList控件。

RadioButtonList控件的更多相关文章

  1. RadioButtonList控件如何取得选中的值

    1.需求:我现在页面上有放两个单选控件,现在要通过判断不同的单选控件来对页面上的标签进行显示和隐藏操作 2.控件如下 <asp:RadioButtonList ID=" RepeatD ...

  2. [PDF] - 获取 RadioButtonList 控件值的方法

    背景 目标是通过 iTextSharp 读取 PDF 模板,填充内容后以生成新 PDF 文件.利用 福昕PDF编辑器个人版 可以获取到 RadioButtonList 的组名,但是获取不到每一个 Ra ...

  3. js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等)

    js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等) 欢迎访问原稿:http://hi.baidu.com/2wixia ...

  4. ASP.NET控件之RadioButtonList

    “RadioButtonList”控件表示一个封装了一组单选按钮控件的列表控件. 可以使用两种类型的 ASP.NET 控件将单选按钮添加到网页上:各个“RadioButton”控件或一个“RadioB ...

  5. javascript获取asp.net服务器端控件的值

    代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...

  6. Web控件文本框Reset的功能

    在前一篇中<怎样实现Web控件文本框Reset的功能>http://www.cnblogs.com/insus/p/4120889.html Insus.NET只实现了文本框的功能.单个或 ...

  7. 基本的Web控件四

    基本的Web控件用法二 ListBox控件 页面布局: <div> <h1>ListBox控件</h1> 学生列表: <br/> <asp:Lis ...

  8. 基本的Web控件三

    基本的Web控件用法一 ListBox控件 页面布局: <div> <h1>ListBox控件</h1> 学生列表: <br/> <asp:Lis ...

  9. 基本的Web控件二

    ListBox控件 ListBox控件用于创建多选的列表框,而可选项是通过ListItem元素来定义的. ListBox控件常用的属性: 1.Count:表示列表框中条目的总数. 2.Items:表示 ...

随机推荐

  1. hdoj 1166 敌兵布阵(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...

  2. poj 2051 Argus(优先队列)

    题目链接: http://poj.org/problem?id=2051 思路分析: 优先级问题,使用优先队列求解:当执行某个任务后,再增加一个任务到队列中, 该任务的优先级为执行任务的时间加上其时间 ...

  3. java程序获得SqlServer数据表的表结构

    /**   * 取得一张表的结构信息   * 使用DatabaseMetaData与ResultSetMetaData结合的方式获取全部属性   * @param conn   数据连接   * @p ...

  4. python下异常处理

    1.python下异常如何处理: #encoding=utf-8 """ python遇到异常,程序直接运行 try: "判断有可能抛出异常的代码" ...

  5. js如何判断一个对象是不是Array?(转载)

    js如何判断一个对象是不是Array? 在开发中,我们经常需要判断某个对象是否为数组类型,在Js中检测对象类型的常见方法都有哪些呢? typeof 操作符 对于Function, String, Nu ...

  6. 【转】Configuring VM Acceleration on Linux

    Configuring VM Acceleration on Linux Linux-based systems support virtual machine acceleration throug ...

  7. C# Best Practices - Handling Strings

    Features Strings Are Immutable. A String Is a Reference Type Value Type Store their data directly Ex ...

  8. 「OC」 继承

    一.基本用法 1.设计两个类Bird.Dog 1 // Bird的声明 2 @interface Bird : NSObject 3 { 4 @public 5 int weight; 6 } 7 - ...

  9. PHP学习笔记9-生成图片

    用PHP代码在网页上生成图片 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/29 * Time: 2 ...

  10. CSS3-旋转齿轮

    CSS3-旋转齿轮 查看DEMO 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. 先来认识一下css3的animation animat ...