【问题标题】:how to modify data in a DetailsView Databound event如何在 DetailsView 数据绑定事件中修改数据
【发布时间】: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


【解决方案1】:

切换到 Asp.Net 4.0+ 并使用 ObjectDataSource

ObjectDataSource.TypeName设置为数据访问对象Type.FullName
ObjectDataSource.DataObjectTypeName 设置为 DTO Type.FullName
ObjectDataSource.SelectMethod 设置为获取IQueryable<MyDto> 的数据访问对象方法。 将DetailsView1.DataSourceID 设置为ObjectDataSource.ID
DetailsView1.ItemType 设置为 DTO Type.FullName

然后做这样的事情:

var item = DetailsView1.DataItem as MyDTO;
if(item == null)
    return;

var box1 = (TextBox) DetailsView1.FindControl("textbox1");
if (box1 == null)
    return;

box1.Text = preprocess(item.PropertyName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多