我遇到特殊情况的麻烦。我dropdownlist
中的值如下:101, 102, 103
,但在任何时候,该用户都可以在下拉列表中添加或停用项目:101, 104, 106
。
我将此dropdownlist
嵌入到listview编辑项目模板中。因此,如果用户添加值为102
的记录,则稍后会删除此值,因此无法编辑此值,因为我收到了上述错误。
所以我想弄清楚的是我如何处理这条消息,让用户知道他们无法编辑记录。到目前为止我所拥有的是ListView_ItemEditing
事件处理程序:
protected void LV_Equipment_ItemEditing(object sender, ListViewEditEventArgs e)
{
LV_Equipment.EditIndex = e.NewEditIndex;
e.Cancel = true;
try
{
LV_Equipment.DataBind();
}
catch (Exception ex)
{
//This is telling the user they cannot edit the record.
AlertMessage("you cannot edit this record.");
}
DropDownList UnitIDDDL = (DropDownList)(LV_Equipment.EditItem.FindControl("DropDownList1"));
DropDownList DriverIDDDL = (DropDownList)(LV_Equipment.EditItem.FindControl("DDL_Insert_Drivers"));
//We need to get the driver for the selected unit in the listview.
int ID = DatabaseInteraction.GetUnitDriver(UnitIDDDL.SelectedValue);
//Now that we have the id we can set the ddl.
DriverIDDDL.SelectedValue = ID.ToString();
DriverIDDDL.DataBind();
}
因此,如果用户尝试编辑有效项目,则没有问题。但是,如果他们尝试从列表视图中编辑已停用的项目,则LV_Equipment.DataBind()
方法将失败,并且其余代码将抛出错误,因为UnitIDDDL
和DriverIDDDL
设置为null。
有关如何使此解决方法生效的任何想法?
答案 0 :(得分:0)
好的,这就是你需要做的事情。
您的数据绑定应来自某种SQL查询。让我们假设它看起来像这样:
string query = "SELECT ItemNumber FROM TableName";
SqlConnection conn;
SqlDataAdapter da = new SqlDataAdapter(query,conn);
DataTable sqlTable = new DataTable("Test");
da.Fill(sqlTable);
LV_Equipment.DataSource = sqlTable;
您要做的是更改查询,使其看起来像这样: string query =" SELECT ItemNumber FROM TableName WHERE Active =' True'";
这应该可以解决你的问题。
向catch块添加一个返回:
try
{
LV_Equipment.DataBind();
}
catch (Exception ex)
{
//This is telling the user they cannot edit the record.
AlertMessage("you cannot edit this record.");
return; // don't continue to process
}
击> <击> 撞击>