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

解决方案 »

  1.   

    你可以在 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);
      

  2.   

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