晕哦,我刚写的没了。。。。重写
看到很多人用了好多办法写这个功能,我好久没用了,也忘了怎么写的,不过看着挺麻烦的,有个老外重写了RadioButton, 我这个类似重写DataGrid, 其实不管radiobutton还是radiobuttonlist都是让它实现单选并显示在datagird的列中,前者是在单选上写JS控制它的name,后者是在显示上写JS让它显示在DataGrid上,我选了后者,有兴趣的可以把我这个重写DataGrid。有篇类似文章,不过代码不全没看明白:http://blog.csdn.net/j99616/archive/2003/09/02/19247.aspx
有用的朋友可以copy-paste一下,感觉我的比较简单一点,找了半天没找到好用的。。。。。。
aspx上:
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > Untitled Page </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:RadioButtonList ID ="rbl" runat ="server" AutoPostBack ="True" OnSelectedIndexChanged ="rbl_SelectedIndexChanged" >
</ asp:RadioButtonList >
< asp:DataGrid ID ="DataGrid1" runat ="server" OnItemCreated ="DataGrid1_ItemCreated" >
</ asp:DataGrid >
</ form >
< script > ...
var dg=document.getElementById("<%=DataGrid1.ClientID %>");
var rb=document.getElementById("<%=rbl.ClientID %>");
for(var i=1;i<=dg.rows.length-1;i++)
...{
dg.rows[i].cells[0].innerHTML=rb.rows[i-1].cells[0].innerHTML;
rb.rows[i-1].cells[0].innerHTML="";
}
</ script >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > Untitled Page </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:RadioButtonList ID ="rbl" runat ="server" AutoPostBack ="True" OnSelectedIndexChanged ="rbl_SelectedIndexChanged" >
</ asp:RadioButtonList >
< asp:DataGrid ID ="DataGrid1" runat ="server" OnItemCreated ="DataGrid1_ItemCreated" >
</ asp:DataGrid >
</ form >
< script > ...
var dg=document.getElementById("<%=DataGrid1.ClientID %>");
var rb=document.getElementById("<%=rbl.ClientID %>");
for(var i=1;i<=dg.rows.length-1;i++)
...{
dg.rows[i].cells[0].innerHTML=rb.rows[i-1].cells[0].innerHTML;
rb.rows[i-1].cells[0].innerHTML="";
}
</ script >
</ body >
</ html >
cs文件:
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
bind();
}
protected void bind()
...{
string[] s =...{ "1", "2", "3" };
DataGrid1.DataSource = s;
DataGrid1.DataBind();
}
protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
...{
TableCell tc = new TableCell();
tc.Width = Unit.Parse("40");
e.Item.Cells.AddAt(0, tc);
if (e.Item.ItemIndex != -1 && !IsPostBack)
...{
ListItem li = new ListItem("", e.Item.ItemIndex.ToString());
rbl.Items.Add(li);
}
}
protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
...{
Response.Write(rbl.SelectedValue);//这里就是radiobuttionlist选中项的值,目前是行标,你可以在ItemCreated的时候赋其他值
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
bind();
}
protected void bind()
...{
string[] s =...{ "1", "2", "3" };
DataGrid1.DataSource = s;
DataGrid1.DataBind();
}
protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
...{
TableCell tc = new TableCell();
tc.Width = Unit.Parse("40");
e.Item.Cells.AddAt(0, tc);
if (e.Item.ItemIndex != -1 && !IsPostBack)
...{
ListItem li = new ListItem("", e.Item.ItemIndex.ToString());
rbl.Items.Add(li);
}
}
protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
...{
Response.Write(rbl.SelectedValue);//这里就是radiobuttionlist选中项的值,目前是行标,你可以在ItemCreated的时候赋其他值
}
}
代码还是用自已写的比较好。。。。。。。。
-------------------------------------------------------------------------
http://www.dreamingcatstudio.com