当前位置: 代码迷 >> ASP.NET >> 和checkbox干上了,该如何处理
  详细解决方案

和checkbox干上了,该如何处理

热度:9433   发布时间:2013-02-25 00:00:00.0
和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的选中状态。
  相关解决方案