答复“博问”中网友对 ObjectDataSource 处理某问题的示例请求
原“问”地址 gridview中dropdownlist如何动态获取值?
答应给一个例子,不拖拉为好:)
将 DemoObjectQuery 放至 App_Code 某文件中:
DemoObjectQuery
using System;
using System.Collections.Generic;
using System.Text;
[System.ComponentModel.DataObject]
public static class DemoObjectQuery {
private static System.Data.DataTable _prototypeTable = new System.Data.DataTable();
static DemoObjectQuery() {
_prototypeTable.Columns.Add("Text");
_prototypeTable.Columns.Add("Value");
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static System.Data.DataTable SelectByNumber(int number) {
System.Data.DataTable dt = _prototypeTable.Clone();
for (int i = 1; i <= number; i++) {
dt.Rows.Add("(" + i + ")", i);
}
return dt;
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static System.Data.DataTable Select() {
System.Data.DataTable dt = _prototypeTable.Clone();
for (int i = 10; i <= 30; i++) {
dt.Rows.Add("[" + i + "]", i);
}
return dt;
}
}
using System;
using System.Collections.Generic;
using System.Text;
[System.ComponentModel.DataObject]
public static class DemoObjectQuery {
private static System.Data.DataTable _prototypeTable = new System.Data.DataTable();
static DemoObjectQuery() {
_prototypeTable.Columns.Add("Text");
_prototypeTable.Columns.Add("Value");
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static System.Data.DataTable SelectByNumber(int number) {
System.Data.DataTable dt = _prototypeTable.Clone();
for (int i = 1; i <= number; i++) {
dt.Rows.Add("(" + i + ")", i);
}
return dt;
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static System.Data.DataTable Select() {
System.Data.DataTable dt = _prototypeTable.Clone();
for (int i = 10; i <= 30; i++) {
dt.Rows.Add("[" + i + "]", i);
}
return dt;
}
}
创建如下 Test.aspx 示例文件:
Test.aspx
Test.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
}
c#的一个重要特征是“优雅”,事实上我也在平常的工作追求这种“无用”的所谓美观。当然,从技术服务于业务的角度说,无论怎样的实现方法,只要稳定、高效、安全就最好,表现方式或许不那么重要;只是在后期维护的过程中,后来人(也许还是开发者自己)将会十分受益!