读书人

搞.net开发一年半了碰到一个很棘手的

发布时间: 2012-01-29 21:39:32 作者: rapoo

搞.net开发一年半了,碰到一个很棘手的问题(RadioButtonList被选择后怎么取消选择?)
我有一个radiobuttonlist
<asp:RadioButtonList ID= "rblist " Runat= "server " RepeatDirection= "Horizontal ">
<asp:ListItem Value= "1 "> Translator </asp:ListItem>
<asp:ListItem Value= "2 "> Done </asp:ListItem>
</asp:RadioButtonList>

我的问题是:一旦选中了某项,然后再单击就可以取消(使两项都不选),能否用JAVASCRIPT做到?望高手赐教。。。。。。

[解决办法]
如果是静态页面可以的了
===========================================
回复人:saeba(浮躁是失败的最初原因) ( ) 信誉:1002007-01-04 21:19:43得分:0
radiobuttonlist 我不怎么用,不过我想javascript 可以实现

var obj = document.getElementById( "rblist ");
obj.option[0].checked = false;

没试,大概就是这样的
==============================================
如果是aspx页面应为服务器控件都有ViewState的值,javascript可以改变html控件的值,但是改变不了视图的值了。javascript看似取消了,其实是一个假象了。
[解决办法]
<asp:RadioButtonList ID= "RadioButtonList1 " runat= "server ">
<asp:ListItem onclick= "set(this); "> a </asp:ListItem>
<asp:ListItem onclick= "set(this); "> b </asp:ListItem>
</asp:RadioButtonList> </div>

<script>
var id = " ";
function set(c)
{

if(c.id == id)
{
c.checked = false;
id = " ";
}
else
id = c.id;
}
</script>
[解决办法]
我试了一下可以cancel,不过觉得意义不大啊
<form id= "Form1 " method= "post " runat= "server ">
<asp:RadioButtonList id= "radioBtnList " style= "Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 136px "
runat= "server ">
<asp:ListItem Value= "111 "> 111 </asp:ListItem>
<asp:ListItem Value= "2222 "> 2222 </asp:ListItem>
<asp:ListItem Value= "3333 "> 3333 </asp:ListItem>
</asp:RadioButtonList>
<asp:Button id= "btnCancel " style= "Z-INDEX: 102; LEFT: 408px; POSITION: absolute; TOP: 168px "
runat= "server " Text= "取消 "> </asp:Button>
</form>
private void btnCancel_Click(object sender, System.EventArgs e)
{
bool bFlag = false;
this.radioBtnList.SelectedItem.Selected = bFlag;
}
[解决办法]
Sorry,下班回家晚了点,测试通过:
前台:
<%@ 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> 无标题页 </title>
<script language= "javascript ">
<!--
var a = new Array();

function myClick(e)
{
for(var h=0;h <document.all.length;h++)
{
if(document.all(h).tagName == "INPUT " && document.all(h).type == "radio ")
{
if(document.all(h).id == e.id)


{
if(a[h] == 1)
e.checked = false;
else
e.checked = true;
}
}
}
for(var i=0;i <document.all.length;i++)
{
if(document.all(i).tagName == "INPUT " && document.all(i).type == "radio ")
{
if(document.all(i).id == e.id)
{
a[i] = 1;
}
else
{
a[i] = 0;
}
}
}

}
//-->
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:RadioButtonList ID= "RadioButtonList1 " runat= "server ">
<asp:ListItem> a </asp:ListItem>
<asp:ListItem> b </asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>


后台:
//......
protected void Page_Load(object sender, EventArgs e)
{
for(int i = 0; i < this.RadioButtonList1.Items.Count; i++)
{
this.RadioButtonList1.Items[i].Attributes.Add( "onclick ", "myClick(this); ");
}
}

读书人网 >asp.net

热点推荐