国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

通過RadioButton對DataList控件進行單選實例說明

2024-07-10 13:18:11
字體:
來源:轉載
供稿:網友
本例實現通過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模版內。

復制代碼 代碼如下:


<asp:DataList runat="server" CellPadding="0" CellSpacing="0">
<ItemStyle BorderWidth="1" />
<ItemTemplate>
<table>
<tr>
<td>
<asp:RadioButton runat="server" /></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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丁青县| 丽江市| 左云县| 台安县| 黄浦区| 杭锦后旗| 东明县| 扎兰屯市| 中卫市| 东乡县| 嘉义县| 西峡县| 张家界市| 墨玉县| 泾源县| 南澳县| 安庆市| 龙岩市| 高青县| 诏安县| 宁化县| 天津市| 阿克苏市| 白水县| 含山县| 精河县| 金堂县| 军事| 左权县| 汪清县| 和顺县| 武定县| 阳春市| 海丰县| 赤峰市| 奉节县| 乐都县| 文化| 肃北| 申扎县| 临沧市|