本来用DropDownList做的,现在用CheckBoxList做,问下该设置怎样的属性?使多个选项只允许选择一个
解决方案 »
- datalist分页
- 关于MVC2.0的问题,在线等啊!
- 彈出窗口被阻止
- 一条关于连接的SQL语句
- DataGrid中时间的比较
- 页面传值问题。
- 怎样改变DataGrid编辑模式下编辑框的宽度? ..........在线等!
- 为什么大家都不理我的贴子呀?关于BS系统框架的问题!!
- 在线急等!Datagrid点击hyperlink弹出新页面时不显示IE的地址栏,菜单栏?
- 【UI问题】:ASP.Net中如何在父窗口和子窗口中进行Server控件的信息交互?(不刷新父窗口)
- ¥¥¥¥¥ 点击页面的某按钮后,如何用jscript实现:打开一个含有textbox的小窗口,用户输完值后,将此值传回页面
- TextBox显示特定字符的问题
<script language="javascript">
function checkyes()
{
Form1.checkYes.checked = true;
Form1.checkNo.checked = false;
}function checkno()
{
Form1.checkNo.checked = true;
Form1.checkYes.checked = false;
}
</script>
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;
} }
}