<asp:TemplateColumn HeaderText="结 果" ItemStyle-Width = "70">
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
<asp:RadioButtonList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格</asp:ListItem>
<asp:ListItem>不合格</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
</asp:TemplateColumn>嵌入datagrid中某列的RadioButtonList
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
<asp:RadioButtonList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格</asp:ListItem>
<asp:ListItem>不合格</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
</asp:TemplateColumn>嵌入datagrid中某列的RadioButtonList
或者寫成checkbox用js在客戶端進行單選控制
<head runat="server">
<title>无标题页</title>
<script language="javascript">
window.onload = function(){
var obj = null;
var chks = document.getElementById("<%=ItemCheckResult.ClientID %>").getElementsByTagName("input");
for(var i=0;i<chks.length;i++){
chks[i].onclick=function(){
if(this.checked){
if(obj!=null && obj!=this)obj.checked=false;
obj = this;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格 </asp:ListItem>
<asp:ListItem>不合格 </asp:ListItem>
<asp:ListItem>优秀 </asp:ListItem>
</asp:CheckBoxList> </form>
</body>
</html>
<asp:DataGrid ID="DG_Data">
<Columns>
<asp:TemplateColumn HeaderText="结 果">
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格 </asp:ListItem>
<asp:ListItem>不合格 </asp:ListItem>
<asp:ListItem>优秀 </asp:ListItem>
</asp:CheckBoxList> </ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<ItemStyle Width="70px" />
</asp:TemplateColumn></Columns>
</asp:DataGrid>
<head runat="server">
<title>无标题页</title>
<script language="javascript">
function eforCheck(gridID,checkListID){
var obj = null;
var grid = document.getElementById(gridID);
if(grid==null) return;
var tables = grid.getElementsByTagName("table");
for(var i=0;i<tables.length;i++){
if(tables[i].id.indexOf(checkListID)!=-1){
var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
for(var j=0;j<chks.length;j++){
chks[j].onclick=function(){
if(this.checked){
if(obj!=null && obj!=this)obj.checked=false;
obj = this;
}
};
}
}
}
}
window.onload=function(){
eforCheck("<%=DG_Data.ClientID%>","ItemCheckResult");
};
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="DG_Data" runat="server" OnLoad="DG_Data_Load">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格 </asp:ListItem>
<asp:ListItem>不合格 </asp:ListItem>
<asp:ListItem>优秀 </asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
<script>
function singleChecked()
{
var p=event.srcElement.parentNode;
var input=p.getElementsByTagName('input');
for(var i=0;i<input.length;i++)
{
if((input[i].type='checkbox')&&(input[i].id!=event.srcElement.id))
document.getElementById(input[i].id).checked=false;
}
}
</script><asp:GridView ID="gv" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ckbOne" Text="合格" runat="server" onclick="singleChecked();" />
<asp:CheckBox ID="ckbTwo" Text="不合格" runat="server" onclick="singleChecked();" />
<asp:CheckBox ID="ckbThree" Text="优秀" runat="server" onclick="singleChecked();" /> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript">
function eforCheck(gridID,checkListID){
var obj = null;
var grid = document.getElementById(gridID);
if(grid==null) return;
var tables = grid.getElementsByTagName("table");
for(var i=0;i<tables.length;i++){
if(tables[i].id.indexOf(checkListID)!=-1){
var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
for(var j=0;j<chks.length;j++){
chks[j].onclick=function(){
if(this.checked){
if(obj!=null && obj!=this)obj.checked=false;
obj = this;
}
};
}
}
}
}
window.onload=function(){
eforCheck("<%=DG_Data.ClientID%>","ItemCheckResult");
};
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="DG_Data" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格 </asp:ListItem>
<asp:ListItem>不合格 </asp:ListItem>
<asp:ListItem>优秀 </asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>cs代码
using System.Text;
using System.Data.SqlClient;public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//string connectionString = "server = (192.168.0.199);database = pratice;user id = sa;pwd = "; //dbconn.GetConnPara(); string connectionString = "server=192.168.0.199;database=XuanwuSupervision;uid=sa;pwd=sa"; SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "select CheckCode,CheckItem,CheckContent,IsKeyProject,Re from DMBusinessCheck"; myCommand.Connection = myConnection;
myAdapter.SelectCommand = myCommand; DataTable choosedt = new DataTable(); myAdapter.Fill(choosedt); DG_Data.DataSource = choosedt;// fillDT();
DG_Data.DataBind();
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript">
function eforCheck(gridID,checkListID){
var obj = null;
var grid = document.getElementById(gridID);
if(grid==null) return;
var tables = grid.getElementsByTagName("table");
for(var i=0;i<tables.length;i++){
if(tables[i].id.indexOf(checkListID)!=-1){
var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
for(var j=0;j<chks.length;j++){
chks[j].onclick=function(){
if(this.checked){
if(obj!=null && obj!=this)obj.checked=false;
obj = this;
}
};
}
}
}
}
window.onload=function(){
eforCheck("<%=DG_Data.ClientID%>","ItemCheckResult");
};
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="DG_Data" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" />
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt"
Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格 </asp:ListItem>
<asp:ListItem>不合格 </asp:ListItem>
<asp:ListItem>优秀 </asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html> protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("a", typeof(string)));
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
DG_Data.DataSource = dt;
DG_Data.DataBind();
}
function eforCheck(gridID,checkListID){
var objs = {};
var grid = document.getElementById(gridID);
if(grid==null) return;
var tables = grid.getElementsByTagName("table");
for(var i=0;i<tables.length;i++){
if(tables[i].id.indexOf(checkListID)!=-1){
objs[tables[i].id] = null;
var chks = document.getElementById(tables[i].id).getElementsByTagName("input");
for(var j=0;j<chks.length;j++){
chks[j].onclick=function(){
if(this.checked){
var pid = this.parentNode.parentNode.parentNode.parentNode.id;
if(objs[pid]!=null && objs[pid]!=this){
objs[pid].checked=false;
objs[pid] = null;
}
objs[pid] = this;
}
};
}
}
}
}
var pid = this.parentNode.parentNode.parentNode.parentNode.id;一定要这样表示?
谢谢!
<script>
function singleChecked(obj)
{
var input=obj.getElementsByTagName('input');
for(var i=0;i<input.length;i++)
{
if((input[i].type='checkbox')&&(input[i].id!=event.srcElement.id))
document.getElementById(input[i].id).checked=false;
}
}
function addClickEvent(id,colIndex)
{
var table=document.getElementById(id);
if(table)
{
var tempID;
for(var i=1;i<table.rows.length;i++)
{
var input=table.rows[i].cells[colIndex].getElementsByTagName('input');
tempID=table.rows[i].cells[colIndex].childNodes[0].id;
for(var j=0;j<input.length;j++)
{
if(input[j].type='checkbox')
{
(function(){
var tb=document.getElementById(tempID);//每个闭包使用不同的变量
input[j].onclick = function(){singleChecked(tb);};
})();
}
}
}
}
else
return;
}
</script>
使用的时候<script>
addClickEvent('<%=gv.ClientID %>',0);
</script>把这段放在gridview的html代码下面 就行, gv 是girdview 的ID ,0 是表示 gridview 的第一列
有一个地方需要注意一下
tempID=table.rows[i].cells[colIndex].childNodes[0].id;
childNodes[0] 中的 0 是硬编码,意思是获取td的第一个子元素.你可以根据需要改成参数配置的形式,像列现在的写法就可以了。
有一个地方需要注意一下 <asp:DataGrid ID="DG_Data" runat="server" AllowSorting="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Font-Size="10pt" GridLines="Vertical" PageSize="1000" Width="780px" >
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" Mode="NumericPages" />
<AlternatingItemStyle BackColor="Gainsboro" />
<ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundColumn DataField="CheckCode" HeaderText="编号"></asp:BoundColumn>
<asp:BoundColumn DataField="CheckItem" HeaderText="检查项目">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="结 果" ItemStyle-Width = "70">
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格</asp:ListItem>
<asp:ListItem>不合格</asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:BoundColumn DataField="CheckCode" HeaderText="编号"> </asp:BoundColumn>
<asp:BoundColumn DataField="CheckItem" HeaderText="检查项目">
<ItemStyle Width="80px" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="结 果" ItemStyle-Width = "70">
<ItemTemplate>
<input id="SelectedID" runat="server" type="hidden" value=' <%# DataBinder.Eval(Container.DataItem, "CheckCode")%>' />
<asp:CheckBoxList ID="ItemCheckResult" runat="server" Font-Names="宋体" Font-Size="10pt" Height="1px" RepeatDirection="Vertical" >
<asp:ListItem>合格 </asp:ListItem>
<asp:ListItem>不合格 </asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="50px" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
</asp:TemplateColumn>
</Columns> 你这个Columns 中 就包含了3列,那列号就为2
你的DataGrid 的 id为DG_Data
你把<script>
addClickEvent('<%=DG_Data.ClientID %>',0);
</script>放在DataGrid 的下面 就可以了