我有一个ListView
与EditItemTemplate
调用方法onItemEditing
。ListView包含CheckBoxList - 选择的项目不显示为检查
在我的ListView
我有一个CheckBoxList
绑定使用LINQ
。
在我的onItemEditing
方法中,我试图检查某些CheckBoxes
,如果它们出现在链接用户与扇区的查找表中。
然而,当我加载EditItemTemplate
没有CheckBoxes
,尽管我已经将它们设置为在onItemEditing
方法选择被选中。
这里的方法:
protected void onItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
ListView1.DataBind();
int regId = Convert.ToInt32(((Label)ListView1.Items[e.NewEditIndex].FindControl("LblRegId")).Text);
CheckBoxList cbl = (CheckBoxList) ListView1.Items[e.NewEditIndex].FindControl("chkLstSectors");
//test to see if forcing first check box to be selected works - doesn't work
cbl.Items[0].Selected = true;
SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DaresburyConnectionString"].ToString());
SqlCommand objCmd = new SqlCommand("select * from register_sectors where register_id= " + regId, objConn);
objConn.Open();
SqlDataReader objReader = objCmd.ExecuteReader();
if (objReader != null)
{
while (objReader.Read())
{
ListItem currentCheckBox = cbl.Items.FindByValue(objReader["sector_id"].ToString());
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
}
}
}
任何想法如何解决这个问题?
你在哪里创建控件?在加载时,在init? – 2009-07-30 03:46:42