RadioButton置于DataList实现单选

本例实现通过RadioButton对DataList控件进行单选。你可以参考下面演示。


准备好一个星座对象,并定义好一个泛型List来存储每一个星座名称。

复制代码
Constelltion.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Constellation
/// </summary>
namespace Insus.NET
{
    public class Constellation
    {
        private int _ID;
        private string _Name;

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public Constellation()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Constellation(int id, string name)
        {
            this._ID = id;
            this._Name = name;
        }

        public List<Constellation> GetConstellation()
        {
            List<Constellation> constellation = new List<Constellation>();

            Constellation c = new Constellation(1, " 白羊座");
            constellation.Add(c);

            c = new Constellation(2, "金牛座");
            constellation.Add(c);

            c = new Constellation(3, "双子座");
            constellation.Add(c);

            c = new Constellation(4, "巨蟹座");
            constellation.Add(c);

            c = new Constellation(5, "狮子座");
            constellation.Add(c);

            c = new Constellation(6, "处女座");
            constellation.Add(c);

            c = new Constellation(7, "天秤座 ");
            constellation.Add(c);

            c = new Constellation(8, "天蝎座");
            constellation.Add(c);

            c = new Constellation(9, "射手座");
            constellation.Add(c);

            c = new Constellation(10, "摩羯座");
            constellation.Add(c);

            c = new Constellation(11, "水瓶座");
            constellation.Add(c);

            c = new Constellation(12, "双鱼座");
            constellation.Add(c);

            return constellation;
        }
    }
}
复制代码


在.aspx拉一个DataList控件,把RadioButton置于DataList的ItemTemplate模版内。

复制代码
View Code
 <asp:DataList ID="DataListConstellation" runat="server" Width="100" CellPadding="0" CellSpacing="0">
                <ItemStyle BorderWidth="1" />
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <asp:RadioButton ID="RadioButtonSelect" runat="server" onclick="SelectedRadio(this);" /></td>
                            <td><%# Eval("ID") %></td>
                            <td><%# Eval("Name") %></td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
复制代码


在.aspx.cs内为DataList控件绑定数据:

复制代码
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class _Default : System.Web.UI.Page
{
    Constellation objConstellation = new Constellation();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Data_Binding();
    }

    private void Data_Binding()
    {
        this.DataListConstellation.DataSource = objConstellation.GetConstellation();
        this.DataListConstellation.DataBind();
    }
}
复制代码


最后,我们写一段Javascript来实现onclick 事件。

复制代码
View Code
 <script type="text/javascript">
        function SelectedRadio(rb) {
            var gv = document.getElementById("<%=DataListConstellation.ClientID%>");
            var rbs = gv.getElementsByTagName("input");

            var row = rb.parentNode.parentNode;
            for (var i = 0; i < rbs.length; i++) {
                if (rbs[i].type == "radio") {
                    if (rbs[i].checked && rbs[i] != rb) {
                        rbs[i].checked = false;
                        break;
                    }
                }
            }
        }
    </script>
复制代码


 

posted @   Insus.NET  阅读(1045)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2012-01-20 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突。
2012-01-20 sp_executesql 使用复杂的 Unicode 表达式
2012-01-20 OBJECTPROPERTY与sp_rename更改对象名称
2010-01-20 Generics and Linq demo
2010-01-20 Two ListBox selector demo
点击右上角即可分享
微信分享提示