我在后台绑定数据源,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCheckBoxList();
}
}
#region BindCheckBoxList
public class TestClass
{
private string _id;
public string Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public TestClass(string id, string name)
{
_id = id;
_name = name;
}
}
private void BindCheckBoxList()
{
List<TestClass> myList = new List<TestClass>();
myList.Add(new TestClass("1", "数据绑定值 1"));
myList.Add(new TestClass("2", "数据绑定值 2"));
myList.Add(new TestClass("3", "数据绑定值 3"));
myList.Add(new TestClass("4", "数据绑定值 4"));
myList.Add(new TestClass("5", "数据绑定值 5"));
myList.Add(new TestClass("6", "数据绑定值 6"));
myList.Add(new TestClass("7", "数据绑定值 7"));
CheckBoxList2.DataTextField = "Name";
CheckBoxList2.DataValueField = "Id";
CheckBoxList2.DataSource = myList;
CheckBoxList2.DataBind();
//!!!注意:这里我不有选中任何项,项目执行后,我选中几项,但当我遍历读取选中项时,获得的值为空;但是如果加上下面的这句话再选择,就可以 得到值,哪位高手帮我分析一下,不胜感激!!
//CheckBoxList2.SelectedValueArray = new string[] { "1", "3" };
}
|