protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("Data source=localhost;Initial catalog=webvotedb;Persist security info=true;user id=sa");
string sqlstr = "select * from votes";
SqlDataAdapter sda = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "votes");
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = "item";
CheckBoxList1.DataValueField = "voteid";
CheckBoxList1.DataBind();
}
}
这样就可以了,
应为页面回发服务器以后又会执行一次代码,加载事件的代码再次被执行,你之前选的就会消失,下面判断的时候就会一个都没选中,就这样了