【发布时间】:2010-02-03 13:50:48
【问题描述】:
我在 aspx 页面中使用带有 sqldatasource 的详细信息视图。 我正在尝试对某些字段进行一些预处理和后处理 - 基本上是将 html 列表转换为换行符分隔的列表以进行编辑并返回 html 以存储在数据库中。
ItemUpdating 中的后处理很简单,但 DataBound 中的前处理比较麻烦……
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.Rows.Count > 2)
{
string s =((DataRowView)DetailsView1.DataItem).Row.ItemArray[2].ToString();
TextBox box1 = (TextBox) DetailsView1.FindControl("textbox1");
if (box1 != null)
{
box1.Text = preprocess(s);
}
}
}
它的脆弱性
string s=((DataRowView)DetailsView1.DataItem).Row.ItemArray[2].ToString();
这让我很不爽。我确定我遗漏了一些明显的东西(不止一件事)!
我想我希望做一些更像我的 ItemUpdating...
e.NewValues["threeline"] = postprocess(e.NewValues["threeline"].ToString());
【问题讨论】:
-
我猜你已经在 10 年内找到了解决方案。但是请考虑将您需要的字段添加到 DataKeys 属性中,以便它可以直接从控件中使用,而不是深入生成的控件树。
标签: c# data-binding detailsview