通过RadioButton对DataList控件进行单选实例说明
网络编程 2021-07-04 22:40www.168986.cn编程入门
本例实现通过RadioButton对DataList控件进行单选,aspx拉一个DataList控件,把RadioButton置于DataList的ItemTemplate模版内;在.aspx.cs内为DataList控件绑定数据,很实用的功能,感兴趣的朋友可以了解下啊
本例实现通过RadioButton对DataList控件进行单选。你可以参考狼蚁网站SEO优化演示。
准备好一个星座对象,并定义好一个泛型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>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程