【发布时间】:2009-07-28 09:37:19
【问题描述】:
我有一个ListView 和一个EditItemTemplate,它调用了一个方法onItemEditing。
在我的ListView 中,我有一个使用LINQ 绑定的CheckBoxList。
在我的onItemEditing 方法中,我尝试检查某些CheckBoxes 是否存在于将用户与部门链接的查找表中。
但是,当我加载 EditItemTemplate 时,即使我在 onItemEditing 方法中将它们设置为选中,也不会检查任何 CheckBoxes。
方法如下:
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;
}
}
}
}
任何想法如何解决这个问题?
【问题讨论】:
-
您在哪里创建了控件?加载时,初始化时?
标签: c# asp.net linq listview checkboxlist