问题描述
我在aspx页面中使用了带sqldatasource的detailsview.我正在尝试对某些字段进行一些预处理和后期处理-基本上是将html列表转换为换行符分隔的列表以进行编辑,然后再返回html以存储在数据库中.
I'm using a detailsview with an sqldatasource in the aspx page.I'm trying to do some pre and post processing on some of the fields - basically to convert a html list to a newline separated list for editing and back to html to store in the database.
ItemUpdating中的后处理很容易,但是DataBound中的前处理却很杂乱...
The post-processing in ItemUpdating is easy enough but the pre-processing in DataBound is messy...
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);
}
}
}
它的脆弱性
字符串s =(((DataRowView)DetailsView1.DataItem).Row.ItemArray [2] .ToString();
这让我不高兴.我确定我缺少明显的东西(不止一件事)!
that upsets me. I'm sure I am missing something (more than one thing) obvious!
我想我希望做更多像我的ItemUpdating ...
I guess I was hoping to do something more like my ItemUpdating...
e.NewValues ["threeline"] =后处理(e.NewValues ["threeline"].ToString());
推荐答案
切换到Asp.Net 4.0+并使用 ObjectDataSource
.
Switch to Asp.Net 4.0+ and use ObjectDataSource
.
将 ObjectDataSource.TypeName
设置为数据访问对象 Type.FullName
.
将 ObjectDataSource.DataObjectTypeName
设置为DTO Type.FullName
.
将 ObjectDataSource.SelectMethod
设置为获取 IQueryable< MyDto>
的数据访问对象方法.将 DetailsView1.DataSourceID
设置为 ObjectDataSource.ID
.
将 DetailsView1.ItemType
设置为DTO Type.FullName
.
Set ObjectDataSource.TypeName
to the data access object Type.FullName
.
Set ObjectDataSource.DataObjectTypeName
to the DTO Type.FullName
.
Set ObjectDataSource.SelectMethod
to the data access object method that get a IQueryable<MyDto>
.Set DetailsView1.DataSourceID
to ObjectDataSource.ID
.
Set DetailsView1.ItemType
to the 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);
这篇关于如何在DetailsView数据绑定事件中修改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!