通過RadioButton對DataList控件進行單選實例說明
更新時間:2013年01月20日 10:45:47 作者:
本例實現(xiàn)通過RadioButton對DataList控件進行單選,aspx拉一個DataList控件,把RadioButton置于DataList的ItemTemplate模版內(nèi);在.aspx.cs內(nèi)為DataList控件綁定數(shù)據(jù),很實用的功能,感興趣的朋友可以了解下啊
本例實現(xiàn)通過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模版內(nèi)。
<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內(nèi)為DataList控件綁定數(shù)據(jù):
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來實現(xiàn)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來存儲每一個星座名稱。
復(fù)制代碼 代碼如下:
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模版內(nèi)。
復(fù)制代碼 代碼如下:
<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內(nèi)為DataList控件綁定數(shù)據(jù):
復(fù)制代碼 代碼如下:
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來實現(xiàn)onclick事件
復(fù)制代碼 代碼如下:
<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>
相關(guān)文章
asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
這幾天在看到一個網(wǎng)站的注冊的時候,就只允許輸入規(guī)定的字符和數(shù)字。我就好奇的寫了一個校驗的代碼。呵呵 不知道對大家有沒有用。如果有用的話可以保存。沒有用就當是看看以下了。2010-10-10.NET?Core配置連接字符串和獲取數(shù)據(jù)庫上下文實例
這篇文章介紹了.NET?Core配置連接字符串和獲取數(shù)據(jù)庫上下文實例的方法,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-01-01Visual Studio Debug實戰(zhàn)教程之斷點操作
眾所周知斷點對于Visual Studio調(diào)試過程是十分重要的,斷點的設(shè)置也是為了更好的進行調(diào)試。下面這篇文章主要給大家介紹了關(guān)于Visual Studio Debug實戰(zhàn)教程之斷點操作的相關(guān)資料,需要的朋友可以參考下2018-09-09