• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    通过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 ListConstellation> GetConstellation()
    {
    ListConstellation> constellation = new ListConstellation>();
    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模版内。
    复制代码 代码如下:

    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控件绑定数据
    复制代码 代码如下:

    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事件
    复制代码 代码如下:

    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>
    您可能感兴趣的文章:
    • 获取DataList控件的主键和索引实用图解
    • 动态加载用户控件至DataList并为用户控件赋值实例演示
    • Datalist控件使用存储过程来分页实现代码
    • asp.net datalist 用法
    • DataList中TextBox onfocus调用后台void静态方法及获取相应行数
    上一篇:GridView的CheckBox列选择及多参数传递三步搞定
    下一篇:gridview checkbox从服务器端和客户端两个方面实现全选和反选
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    通过RadioButton对DataList控件进行单选实例说明 通过,RadioButton,对,DataList,