• RadioButtonList控件


    在这里只写,绑定数据库数据的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 ·
    复制代码

    这是最终建立的表:

    二:

    前台代码:

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

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

    后台代码(Page_Load):

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

    后台代码(Button1):

     1 /// <summary>
     2 /// Button1按钮的单机事件
     3 /// </summary>
     4 /// <param name="sender"></param>
     5 /// <param name="e"></param>
     6 protected void Button1_Click(object sender, EventArgs e)
     7 {
     8     string s = string.Empty;
     9 
    10     for (int i = 0; i < RadioButtonList1.Items.Count; i++)
    11     {
    12         if (RadioButtonList1.Items[i].Selected)
    13         {
    14             s = RadioButtonList1.Items[i].Text;
    15         }
    16     }
    17 
    18     this.Label1.Text = "你选中的是:" + s;
    19 }

    最终效果:

    以上就是RadioButtonList控件。

  • 相关阅读:
    svn使用方法
    董事、执行董事、总裁、总经理
    华联、联华、世纪华联、百联
    声明式编程和命令式编程
    NodeJS中的require和import
    JSONP跨站访问
    Camera服务之--架构浅析
    HashMap实现原理分析
    设计模式解读之一: 策略模式
    Java6.0中Comparable接口与Comparator接口详解
  • 原文地址:https://www.cnblogs.com/KTblog/p/4273023.html
Copyright © 2020-2023  润新知