2010-02-03 50 views
1

我在aspx頁面中使用帶有sqldatasource的detailsview。 我想對一些字段進行一些預處理和後處理 - 基本上是將html列表轉換爲換行符分隔的列表以進行編輯並返回到html以存儲在數據庫中。如何修改DetailsView Databound事件中的數據

在ItemUpdating後處理是很容易的,但在數據綁定前處理是凌亂...

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());

回答

0

切換到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);