首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

和checkbox干上了,该如何处理

2012-01-13 
和checkbox干上了datalist中的checkbox选中其中一个时把其他选中的都取消选中状态,结果是只有一个框被选中

和checkbox干上了
datalist中的checkbox   选中其中一个时把其他选中的都取消选中状态,结果是只有一个框被选中

[解决办法]
你可以在 page_PreRender 事件处理程序中写:

string functionName = "fun " + (Guid.NewGuid().ToString( "N "));
string scp = "function " + functionName + "(){ ";
foreach (DataGridItem item in yourDataGrid.Items)
{
CheckBox chk = (CheckBox)item.FindControl( "yourCheckBoxID ");
scp += "document.all( ' " + chk.ClientID + " ').checked=false; ";
chk.Attributes[ "onclick "] = "if(this.checked){ " + functionName +
"();this.checked=true;} ";
}
scp += "} ";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "initCheckbox ", scp, true);


[解决办法]
随机产生脚本函数名是为了避免与其它函数名重名。这个函数可以将 yourDataGrid 中所有id为 yourCheckBoxID 的 CheckBox 的选中状态取消。同时,chk.Attributes[ "onclick "] 写入的脚本是:当CheckBox被选中时,调用这个函数(取消所有选中状态),然后再设置本CheckBox的选中状态。

热点排行