通过RadioButton对DataList控件进行单选实例说明
程序员文章站
2024-03-04 15:02:29
本例实现通过radiobutton对datalist控件进行单选。你可以参考下面演示。 准备好一个星座对象,并定义好一个泛型list来存储每一个星座名称。 复制...
本例实现通过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>