在ASP .Net 2.0中使用資料對象ObjectDataSource綁定到顯示視圖view中可以很方便的實現資料的從資料庫到顯示層之間的互動,這個比Java的strut雖然是思想一致,但是要簡單很多很多。但是使用ObjectDataSource也有一些問題,ObjectDataSource的參數是直接綁定到控制項或者querystring,無法直接實現對參數的直接手工操作,例如無法實現ObjectDataSource.SelectParameters["ID"].value='1010'。
雖然ObjectDataSource提供了一個Selecting事件,可以在這個事件中隊參數進行控制。
但是要實現這個功能還是需要一些代碼。SelectParameters是一個參數集合,裡面是Parameters對象,而Parameters是什麼呢?它是2.0的新對象。http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.parameter.aspx
在微軟的這個對象說明中,有一個如何擴充參數類。我測試了一下還是可以用的。
步驟:
1:建立一個新類,直接copy微軟代碼就可以。
namespace Samples.AspNet {
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public class StaticParameter : Parameter {
public StaticParameter() {
}
// The StaticParameter(string, object) constructor
// initializes the DataValue property and calls the
// Parameter(string) constructor to initialize the Name property.
public StaticParameter(string name, object value) : base(name) {
DataValue = value;
}
// The StaticParameter(string, TypeCode, object) constructor
// initializes the DataValue property and calls the
// Parameter(string, TypeCode) constructor to initialize the Name and
// Type properties.
public StaticParameter(string name, TypeCode type, object value) : base(name, type) {
DataValue = value;
}
// The StaticParameter copy constructor is provided to ensure that
// the state contained in the DataValue property is copied to new
// instances of the class.
protected StaticParameter(StaticParameter original) : base(original) {
DataValue = original.DataValue;
}
// The Clone method is overridden to call the
// StaticParameter copy constructor, so that the data in
// the DataValue property is correctly transferred to the
// new instance of the StaticParameter.
protected override Parameter Clone() {
return new StaticParameter(this);
}
// The DataValue can be any arbitrary object and is stored in ViewState.
public object DataValue {
get {
return ViewState["Value"];
}
set {
ViewState["Value"] = value;
}
}
// The Value property is a type safe convenience property
// used when the StaticParameter represents string data.
// It gets the string value of the DataValue property, and
// sets the DataValue property directly.
public string Value {
get {
object o = DataValue;
if (o == null || !(o is string))
return String.Empty;
return (string)o;
}
set {
DataValue = value;
OnParameterChanged();
}
}
// The Evaluate method is overridden to return the
// DataValue property instead of the DefaultValue.
protected override object Evaluate(HttpContext context, Control control) {
if (context.Request == null)
return null;
return DataValue;
}
}
}
2:在Selecting中編寫代碼
protected void ObjectDataSourceMtnItem_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
StaticParameter pCode = new StaticParameter();
Label codeLabel = (Label)FormView1.FindControl("codeLabel");
if (codeLabel==null)
pCode.Value = Request.QueryString["ItemNo"];
else
pCode.Value = codeLabel.Text;
pCode.Name = "strID";
pCode.Type = TypeCode.String;
ObjectDataSourceMtnItem.SelectParameters["strID"] = pCode;
}
其中strID是原來的參數名稱。