FineUI 官方论坛
标题:
扩展Grid.DataSourceID以支持ObjectDataSource
[打印本页]
作者:
大石头
时间:
2012-4-2 13:17
标题:
扩展Grid.DataSourceID以支持ObjectDataSource
本帖最后由 大石头 于 2012-4-2 13:19 编辑
给Grid增加DataSourceID属性,用于指定ObjectDataSource的ID。
然后在OnPreRender阶段执行绑定DataBind,无需手工编写获取数据的代码,Grid可以直接从ObjectDataSource获取数据,用法与GridView完全兼容。
目前已测试通过多条件高级查询、千万数据分页、排序、删除等操作,其中高级查询、分页、排序可以混合进行。
主要修改点如下:
#region DataSourceID
/// <summary>数据源控件</summary>
public String DataSourceID
{
get
{
object obj = XState["DataSourceID"];
return obj == null ? null : (String)obj;
}
set { XState["DataSourceID"] = value; }
}
private DataSourceSelectArguments _SelectArguments;
/// <summary>查询参数</summary>
public DataSourceSelectArguments SelectArguments { get { return _SelectArguments ?? (_SelectArguments = CreateDataSourceSelectArguments()); } }
/// <summary>从关联的数据源中检索数据。</summary>
protected void PerformSelect()
{
if (String.IsNullOrEmpty(DataSourceID)) return;
var view = GetData();
_SelectArguments = CreateDataSourceSelectArguments();
view.Select(_SelectArguments, data =>
{
if (SelectArguments.RetrieveTotalRowCount)
{
RecordCount = SelectArguments.TotalRowCount;
IsDatabasePaging = true;
}
DataBindToEnumerable(data);
});
}
/// <summary>创建 <see cref="T:System.Web.UI.DataSourceSelectArguments" /> 对象,该对象包含传递到数据源进行处理的参数。</summary>
/// <returns>一个 <see cref="T:System.Web.UI.DataSourceSelectArguments" />,包含传递到数据源进行处理的参数。</returns>
protected DataSourceSelectArguments CreateDataSourceSelectArguments()
{
var arg = new DataSourceSelectArguments();
var view = GetData();
// 准备排序表达式
if (view.CanSort)
{
string sortExp = null;
if (SortColumnIndex >= 0 && SortColumnIndex < Columns.Count) sortExp = Columns[SortColumnIndex].SortField;
if (!String.IsNullOrEmpty(sortExp))
{
if (String.Equals(SortDirection, "DESC", StringComparison.OrdinalIgnoreCase)) sortExp += " DESC";
arg.SortExpression = sortExp;
}
}
// 准备分页相关
if (AllowPaging && view.CanPage)
{
if (view.CanRetrieveTotalRowCount)
{
arg.RetrieveTotalRowCount = true;
arg.MaximumRows = PageSize;
}
else
arg.MaximumRows = -1;
arg.StartRowIndex = PageSize * PageIndex;
}
return arg;
}
/// <summary>检索数据绑定控件用于执行数据操作的 <see cref="T:System.Web.UI.DataSourceView" /> 对象。</summary>
/// <returns>数据绑定控件用于执行数据操作的 <see cref="T:System.Web.UI.DataSourceView" />。如果设置了 <see cref="P:System.Web.UI.WebControls.DataBoundControl.DataMember" /> 属性,则返回特定的已命名的 <see cref="T:System.Web.UI.DataSourceView" />;否则,返回默认的 <see cref="T:System.Web.UI.DataSourceView" />。</returns>
/// <exception cref="T:System.InvalidOperationException">同时设置了 <see cref="P:System.Web.UI.WebControls.BaseDataBoundControl.DataSource" /> 和 <see cref="P:System.Web.UI.WebControls.BaseDataBoundControl.DataSourceID" /> 属性。- 或 -设置了 <see cref="P:System.Web.UI.WebControls.DataBoundControl.DataMember" /> 属性,但不存在具有该名称的 <see cref="T:System.Web.UI.DataSourceView" /> 对象。</exception>
protected DataSourceView GetData()
{
string dataSourceID = DataSourceID;
if (String.IsNullOrEmpty(dataSourceID)) return null;
var ds = FindControl(this, dataSourceID) as IDataSource;
if (ds == null || ds.GetViewNames().Count < 1) return null;
foreach (String item in ds.GetViewNames())
{
var view = ds.GetView(item);
if (view != null) return view;
}
return null;
}
public static Control FindControl(Control control, string controlID)
{
Control namingContainer = control;
Control control3 = null;
if (control != control.Page)
{
while (control3 == null && namingContainer != control.Page)
{
namingContainer = namingContainer.NamingContainer;
if (namingContainer == null) return null;
control3 = namingContainer.FindControl(controlID);
}
return control3;
}
return control.FindControl(controlID);
}
#endregion
#region 删除功能
private void HandleDelete(GridCommandEventArgs e)
{
if (String.IsNullOrEmpty(DataSourceID)) return;
var view = this.GetData();
if (view == null) return;
var dic = new Dictionary<Object, Object>();
if (DataKeys.Count > e.RowIndex)
{
var vs = DataKeys[e.RowIndex];
for (int i = 0; i < DataKeyNames.Length && i < vs.Length; i++)
{
dic.Add(DataKeyNames[i], vs[i]);
}
}
view.Delete(dic, null, (rows, ex) =>
{
if (ex == null)
{
//Alert.Show("删除成功!");
RequiresDataBinding = true;
return true;
}
else
{
Alert.Show("删除失败!" + ex.Message, MessageBoxIcon.Error);
//return false;
// 返回false会导致抛出异常
return true;
}
});
}
#endregion
复制代码
作者:
大石头
时间:
2012-4-2 13:18
我吐血!
贴的代码那么乱……
作者:
大石头
时间:
2012-4-2 13:20
private Boolean RequiresDataBinding = true;
protected override void OnPreRender(EventArgs e)
{
if (RequiresDataBinding) DataBind();
base.OnPreRender(e);
}
protected virtual void OnRowCommand(GridCommandEventArgs e)
{
EventHandler<GridCommandEventArgs> handler = Events[_rowCommandHandlerKey] as EventHandler<GridCommandEventArgs>;
if (handler != null)
{
handler(this, e);
}
if (String.Equals(e.CommandName, "Delete")) HandleDelete(e);
}
复制代码
欢迎光临 FineUI 官方论坛 (https://fineui.com/bbs/)
Powered by Discuz! X3.4