本来用DropDownList做的,现在用CheckBoxList做,问下该设置怎样的属性?使多个选项只允许选择一个

解决方案 »

  1.   

    没有true或false这样可以用一个属性来设置的.但可以写javascript来实现,当点击一个CheckBox时,把自己加上勾,别一个去掉勾,如:
    <script language="javascript">
    function checkyes()
    {
    Form1.checkYes.checked = true;
    Form1.checkNo.checked = false;
    }function checkno()
    {
    Form1.checkNo.checked = true;
    Form1.checkYes.checked = false;
    }
    </script>
      

  2.   

    如果你有很多CheckBox,原理同上,但得在语句构造方面稍作处理,比如用循环来进行判断.
      

  3.   

    每次选择的时候遍历所有的checkbox,让其他的取消选择.
      

  4.   

    用JS呗!...用JS判断或者用程序也是可行的。自己看着办
      

  5.   

    需要自己写JavaScript实现,下面是我自己写的一个。请指教。
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;namespace ICS.CommModule
    {
    /// <summary>
    /// ECheckBoxList 的摘要说明。
    /// </summary>
    public class ECheckBoxList:System.Web.UI.WebControls.CheckBoxList
    {
    private bool _IsSelectAll;
    private bool _IsRadio;
    private string _CheckValue;
    private string _AddCheckValue;
    private string _DelCheckValue; #region 自定义的属性
    /// <summary>
    /// 是否为单选框,默认为多选。true单选
    /// </summary>
    public bool IsRadio
    {
    get
    {
    return _IsRadio;
    }
    set
    {
    _IsRadio = value;
    } }
    /// <summary>
    /// 默认选定的CheckBox值,多个用“,”号隔开
    /// </summary>
    public string SetCheckedValue
    {
    get
    {
    return (String)ViewState["_CheckedValue"];
    }
    set
    {
    ViewState["_CheckedValue"] = value;
    SetCheckBox();
    }
    } /// <summary>
    /// 是否显示全选项目,True显示,默认不显示
    /// </summary>
    public bool IsShowSelectAll
    {
    get
    {
    return _IsSelectAll;
    }
    set
    {
    _IsSelectAll = value;
    }
    }
    /// <summary>
    /// 当前选定的CheckedBox,中间用“,”号隔开。
    /// </summary>
    public string GetCheckedValue
    {
    get
    {
    if(_CheckValue == "")
    {
    GetCheckedBoxValue();
    }
    return _CheckValue;
    }
    }
    /// <summary>
    /// 当前新增选定的CheckBox,中间用“,”号隔开。
    /// </summary>
    public string GetAddCheckedValue
    {
    get
    {
    if(_AddCheckValue == "")
    {
    GetCheckedBoxDiffValue();
    }
    return _AddCheckValue;
    }
    }
    /// <summary>
    /// 当前取消选定的CheckBox,中间用“,”号隔开。
    /// </summary>
    public string GetDelCheckedValue
    {
    get
    {
    if(_DelCheckValue == "")
    {
    GetCheckedBoxDiffValue();
    }
    return _DelCheckValue;
    }
    }

    #endregion
    public ECheckBoxList()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    _IsSelectAll = false;
    _IsRadio = false;
    SetCheckedValue = "";
    _AddCheckValue = "";
    _DelCheckValue = "";
    _CheckValue = "";
    this.Load += new EventHandler(ECheckBoxList_Load);
    }
    private void ECheckBoxList_Load(object objer,System.EventArgs e)
    {
    if(_IsSelectAll)
    {
    ListItem Litem = new ListItem();
    Litem.Text = "全选";
    Litem.Value = "全选";
    Litem.Selected = false;
    Litem.Attributes.Add("onclick","SelectAll"+this.ID+"(this)");
    this.Items.Insert(0,Litem);
    RegisterSelectAll();
    }
    if(_IsRadio)
    {
    for(int i = 0;i< this.Items.Count;i++)
    {
    this.Items[i].Attributes.Add("onclick","SelectRadio"+this.ID+"(this)");
    }
    RegisterSelectRadio();
    }
    } protected override void Render(HtmlTextWriter writer) 
    { if(_IsSelectAll || _IsRadio)
    {
    RenderWride(writer);
    }
    else
    {
    base.Render(writer);
    }
    }
    private void RenderWride(HtmlTextWriter writer)
    {
    StringBuilder sb = new StringBuilder();
    base.Render(new HtmlTextWriter(new System.IO.StringWriter(sb)));
    string s = sb.ToString();
    int start = 0;
    for(int i = 0;i< this.Items.Count;i++)
    {
    start = s.IndexOf("<input",start,s.Length-start);
    StringBuilder sbItemAttribute = new StringBuilder();
    this.Items[i].Attributes.Render(new HtmlTextWriter(new System.IO.StringWriter(sbItemAttribute)));
    s = s.Insert(start+7,sbItemAttribute.ToString()+" ");
    start = s.IndexOf("/>",start,s.Length-start);
    }
    writer.Write(s);
    } public override void DataBind()
    {
    base.DataBind();

    }

    //打开时选定CheckBox
    private void SetCheckBox()
    {
    if(SetCheckedValue=="")
    {
    return;
    }
    string m_selectvalue = ","+SetCheckedValue+",";
    string m_check;
    for (int i=0;i<this.Items.Count;i++)
    {
    m_check=","+this.Items[i].Value.ToString()+",";
    if(m_selectvalue.IndexOf(m_check)>=0)
    {
    this.Items[i].Selected=true;
    }
    else
    {
    this.Items[i].Selected=false;
    }
    } }
    //登记全选javascript代码
    private void RegisterSelectAll()
    {
    if(!Page.IsClientScriptBlockRegistered("SelectAll"))
    {
    string m_script;
    m_script = "<Script language = 'javascript'>function SelectAll"+this.ID+"(obj) {"+
    "for(i=1;i<"+this.Items.Count+";i++){document.all['"+this.ID+"_'+i].checked=obj.checked;}}</script>";
    Page.RegisterClientScriptBlock("SelectAll"+this.ID,m_script);
    }
    }
    //登记单选javascript代码
    private void RegisterSelectRadio()
    {
    if(!Page.IsClientScriptBlockRegistered("SelectRadio"))
    {
    string m_script;
    m_script = "<Script language = 'javascript'>function SelectRadio"+this.ID+"(obj) {"+
    "if(!obj.checked) {obj.checked=true;return}; "+
    "for(i=0;i<"+this.Items.Count+";i++){if(obj.id != '"+this.ID+"_'+i){document.all['"+this.ID+"_'+i].checked=false;}}}</script>";
    Page.RegisterClientScriptBlock("SelectRadio"+this.ID,m_script);
    }
    } //当前选定的Checkbox
    private void GetCheckedBoxValue()
    { _CheckValue = "";
    foreach(ListItem m_item in this.Items)
    {
    if(m_item.Value != "全选" && m_item.Selected)
    {
    if(_CheckValue == "")
    {
    _CheckValue = m_item.Value;
    }
    else
    {
    _CheckValue = _CheckValue + "," + m_item.Value;
    }
    }
    }
    } //跟原始原定的Checkbox差异
    private void GetCheckedBoxDiffValue()
    {
    int m_find = 0;
    _AddCheckValue = "";
    _DelCheckValue = "";
    string m_CheckValueNow = "";
    string m_CheckValueOld = "";
    string m_item; if(GetCheckedValue != "")
    {
    if(SetCheckedValue != "")
    {
    //新增选定的checked
    m_CheckValueOld = ","+ SetCheckedValue +",";
    foreach(string item in GetCheckedValue.Split(','))
    {
    if(item != "")
    {
    m_item = "," + item + ",";
    m_find = 0;
    m_find = m_CheckValueOld.IndexOf(m_item);
    if(m_find == -1)
    {
    if(_AddCheckValue == "")
    {
    _AddCheckValue = item;
    }
    else
    {
    _AddCheckValue = _AddCheckValue + "," + item;
    }
    }
    }
    }
    //取消选定的checkbox
    m_CheckValueNow = ","+ GetCheckedValue +",";
    foreach(string item in SetCheckedValue.Split(','))
    {
    if(item != "")
    {
    m_item = "," + item + ",";
    m_find = 0;
    m_find = m_CheckValueNow.IndexOf(m_item);
    if(m_find == -1)
    {
    if(_DelCheckValue == "")
    {
    _DelCheckValue = item;
    }
    else
    {
    _DelCheckValue = _DelCheckValue + "," + item;
    }
    }
    } }
    }
    else
    {
    _AddCheckValue = GetCheckedValue;
    _DelCheckValue = "" ;
    }
    }
    else
    {
    _AddCheckValue = "";
    _DelCheckValue = SetCheckedValue;
    }
    }
    /// <summary>
    /// 根据值得到多选框的文件
    /// </summary>
    /// <param name="ItemValue"></param>
    /// <returns></returns>
    public string GetItemText(string ItemValue)
    {
    string m_result = "";
    foreach(ListItem m_item in this.Items)
    {
    if(m_item.Value == ItemValue)
    {
    m_result = m_item.Text;
    break;
    }
    }
    return m_result;
    } }
    }
      

  6.   

    如果只单选,怎么不用RadioButtonList