通过RadioButton对DataList控件进行单选实例说明
更新时间:2021年9月22日 10:17 点击:1688
本例实现通过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模版内。
<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>
准备好一个星座对象,并定义好一个泛型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模版内。
复制代码 代码如下:
<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>
相关文章
- 这篇文章主要为大家详细介绍了vue实现div单选多选功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-07-16
ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22asp.net实现DataList与Repeater嵌套绑定的方法
这篇文章主要介绍了asp.net实现DataList与Repeater嵌套绑定的方法,结合实例形式分析了DataList与Repeater的步骤与相关实现技巧,需要的朋友可以参考下...2021-09-22ASP.NET中利用DataList实现图片无缝滚动 实例分享
这个问题之前也困扰我,后来解决了,拿出来分享下,以后用也方便,代码很容易看懂,不多说什么了...2021-09-22在ASP.NET 2.0中操作数据之三十三:基于DataList和Repeater使用DropDownList过滤的主/从报表
前面已经介绍过使用DropDownList过滤的主/从报表,不过当时是基于GridView,本文算是复习一下,基于DataList和Repeater再次实现一下相同的功能。...2021-09-22- 用CheckBoxList实现单选的原因是我觉得CheckBoxList控件页面展示效果要好看一些,需要的朋友可以参考下...2021-09-22
asp.net datalist绑定数据后可以上移下移实现示例
这篇文章主要介绍了asp.net datalist绑定数据后可以上移下移的示例代码,需要的朋友可以参考下...2021-09-22- 这篇文章主要介绍了asp.net控件DataList分页用法,实例分析了asp.net使用DataList控件实现分页功能的设置与数据操作技巧,需要的朋友可以参考下...2021-09-22
- 本文借用使用通用的新闻例子演示动态加载用户控件至DataList并为用户控件赋值,感兴趣的朋友可以了解下...2021-09-22
ASP.NET GridView中加入RadioButton不能单选的解决方案
这篇文章主要介绍了ASP.NET GridView中加入RadioButton不能单选的解决方案,希望大家阅读完本文有所收获。...2021-09-22在ASP.NET 2.0中操作数据之三十五:使用Repeater和DataList单页面实现主/从报表
前面已经介绍了ASP.NET 2.0中如何使用两个页面实现主/从报表,本文主要讲解,如何使用一个单独页面实现主/从报表。...2021-09-22Gridview使用CheckBox全选与单选采用js实现同时高亮显示选择行
Gridview使用CheckBox单选与全选功能再次进行简单演示,选中的行,使用高亮显示,让用户一目了然看到哪一行被选择了,在项目中很实用的,开发中的朋友们可要考虑一下哦...2021-09-22- 这篇文章主要为大家详细介绍了小程序实现自定义多层级单选和多选,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-21
Asp.Net如何将多个RadioButton指定在一个组中
将多个RadioButton指定在一个组中,实现其实很简单,一句代码即可,具体如下,希望对大家有所帮助...2021-09-22DataList中TextBox onfocus调用后台void静态方法及获取相应行数
DataList中我放了一个TextBox 现在的问题是当我光标放到TextBox上的时候。如果让onfocus调用后台某一个void静态方法并且在静态方法里边获取光标相应的DataList的相应行数,本文介绍如何实现,感兴趣的朋友可以了解下...2021-09-22datalist,Repeater和Gridview的区别分析
datalist,Repeater和Gridview的区别分析,需要的朋友可以参考一下...2021-09-22- 这篇文章主要为大家详细介绍了MVC5下拉框绑定,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
在ASP.NET 2.0中操作数据之四十四:DataList和Repeater数据排序(三)
上篇已经完成了自定义分页,这一节我们继续完善排序功能。...2021-09-22- 这篇文章主要介绍了C#实现DataList里面嵌套DataList的折叠菜单,以实例形式详细分析了DataList嵌套实现折叠菜单所涉及的JavaScript、HTML与C#相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25