datagrid中的radiobuttonlist 列选中某向后,怎么取消(即一项都不选中)

terrence1106 2009-03-17 05:04:44
<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
...全文
232 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 terrence1106 的回复:]
谢谢!18楼的问题可以解决么?我不太明白这句话:

把这段放在gridview的html代码下面 就行, gv 是girdview 的ID ,0 是表示 gridview 的第一列
有一个地方需要注意一下


<asp:DataGrid ID="DG_Data" runat="server" AllowSorting="True" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPaddin…
[/Quote]


<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>

你这个Columns 中 就包含了3列,那列号就为2
你的DataGrid 的 id为DG_Data
你把

<script>
addClickEvent('<%=DG_Data.ClientID %>',0);
</script>

放在DataGrid 的下面 就可以了
terrence1106 2009-03-23
  • 打赏
  • 举报
回复
谢谢!18楼的问题可以解决么?我不太明白这句话:

把这段放在gridview的html代码下面 就行, gv 是girdview 的ID ,0 是表示 gridview 的第一列
有一个地方需要注意一下



<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>
阿非 2009-03-21
  • 打赏
  • 举报
回复

<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的第一个子元素.

你可以根据需要改成参数配置的形式,像列现在的写法就可以了。

阿非 2009-03-19
  • 打赏
  • 举报
回复

var pid = this.parentNode.parentNode.parentNode.parentNode.id;

一定要这样表示?
terrence1106 2009-03-19
  • 打赏
  • 举报
回复
可以了 多谢多谢!
CutBug 2009-03-19
  • 打赏
  • 举报
回复
这个函数改一下
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;
}
};
}
}
}
}
terrence1106 2009-03-19
  • 打赏
  • 举报
回复
CutBug 你好

13楼的代码还是整个datagrid只能只能选择一个“合格、不合格、优秀”,我要求的是每一行数据中最多只能选择一个“合格、不合格、优秀” ,我的是 。net 2005 ,ie6
CutBug 2009-03-19
  • 打赏
  • 举报
回复
下面是我的完整测试数据,IE6.0没有问题,你自己新建一个页面把代码拷进去看看
<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();
}
terrence1106 2009-03-19
  • 打赏
  • 举报
回复
CutBug ,不行呀,我测试怎么通不过呀

aspx页面

<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" 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,Remark from DMBusinessCheck";

myCommand.Connection = myConnection;
myAdapter.SelectCommand = myCommand;

DataTable choosedt = new DataTable();

myAdapter.Fill(choosedt);

DG_Data.DataSource = choosedt;// fillDT();
DG_Data.DataBind();



}
}
}
terrence1106 2009-03-19
  • 打赏
  • 举报
回复
谢谢二位!

CutBug ,我先还有个小问题,15楼的方法可以了,但是进入该页面有2个口,一个新建、一个修改,前者可以,后者修改(预先选择了部分合格or不合格),对于预先选择了的,可以同时选择合格and不合格,也就是系统没有看到预先选择的。不知我说清楚没有?
谢谢!
阿非 2009-03-18
  • 打赏
  • 举报
回复

<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>
CutBug 2009-03-18
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
路人乙e 2009-03-18
  • 打赏
  • 举报
回复
最简单的做法就是加个选项 none
terrence1106 2009-03-18
  • 打赏
  • 举报
回复
谢谢 cutbug,我的 CheckBoxList 是放在datagrid中的一列,编译的时候报错误如下:

错误 33 当前上下文中不存在名称“ItemCheckResult”


<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>
阿非 2009-03-18
  • 打赏
  • 举报
回复
哪你就用我的那个
CutBug 2009-03-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 CutBug 的回复:]
HTML code<html xmlns="http://www.w3.org/1999/xhtml" >
<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++){

[/Quote]
这个是每行里面一个checkboxlist的代码,测试过的
terrence1106 2009-03-18
  • 打赏
  • 举报
回复
你好cutbug, 你的方法整个datagrid只能输入一个合格或者不合格,我的要求是没条记录 可以为 合格、不合格、不填写
liuyeede 2009-03-17
  • 打赏
  • 举报
回复
在客户端控制的结果是提交到服务端仍然还是操作以前的结果
CutBug 2009-03-17
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
terrence1106 2009-03-17
  • 打赏
  • 举报
回复
寫成checkbox用js在客戶端進行單選控制

这个怎么做?
加载更多回复(1)

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧