//private static Dictionary<Type, Action<object, object>> map; //static DataBindingHelper() //{ // map = new Dictionary<Type, Action<object, object>>(); // map.Add( // typeof(DataBoundControl), // (control, data) => // { // ((DataBoundControl)control).DataSource = data; // ((DataBoundControl)control).DataBind(); // }); // map.Add(typeof(BaseDataList), // (control, data) => // { // ((BaseDataList)control).DataSource = data; // ((BaseDataList)control).DataBind(); // }); //} public static void SetDataSourceAndBind(object control, object dataSource) { if (control is DataBoundControl) { DataBoundControl bindable = (DataBoundControl)control; bindable.DataSource = dataSource; bindable.DataBind(); } else if (control is BaseDataList) { BaseDataList bindable = (BaseDataList)control; bindable.DataSource = dataSource; bindable.DataBind(); } else if (control is Repeater) { Repeater bindable = (Repeater)control; bindable.DataSource = dataSource; bindable.DataBind(); } else { throw new NotSupportedException(control.GetType().Name + " is not a supported data control"); } //foreach (var type in map.Keys) //{ // if (type.IsAssignableFrom(control.GetType())) // { // map[type](control, dataSource); // return; // } //} //throw new NotSupportedException(control.GetType().Name + " is not a supported data control"); }
public static void SetDataSourceAndBind(object control, object dataSource) { if (control is DataBoundControl) { DataBoundControl bindable = (DataBoundControl)control; bindable.DataSource = dataSource; bindable.DataBind(); } else if (control is BaseDataList) { BaseDataList bindable = (BaseDataList)control; bindable.DataSource = dataSource; bindable.DataBind(); } else if (control is Repeater) { Repeater bindable = (Repeater)control; bindable.DataSource = dataSource; bindable.DataBind(); } else { throw new NotSupportedException(control.GetType().Name + " is not a supported data control"); } }
private static void Fill <U, T>(U dataControl, T data) where U : Control where T : class { if (dataControl.IsNull()) { return; } if (data.IsNull()) { Clear <U>(dataControl); return; } if (SObject.IsTypeNotInList <U>(typeof(BaseDataList), typeof(Repeater), typeof(BaseDataBoundControl))) { throw new InvalidCastException("Control parameter is not valid, you are only alowed to use: BaseDataList, Repeater and BaseDataBoundControl"); } if (SObject.IsTypeNotInList <T>(typeof(IListSource), typeof(IEnumerable), typeof(IDataSource))) { throw new InvalidCastException("Data parameter is not valid, you are only alowed to use: IDataSource, IEnumerable and IListSource"); } try { dataControl.GetType().GetProperty("DataSource").SetValue(dataControl, data); dataControl.GetType().GetMethod("DataBind").Invoke(dataControl, null); } catch (Exception) { if (dataControl is BaseDataList) { BaseDataList dl = dataControl as BaseDataList; dl.DataSource = data; dl.DataBind(); } else if (dataControl is Repeater) { Repeater rptr = dataControl as Repeater; rptr.DataSource = data; rptr.DataBind(); } else if (dataControl is BaseDataBoundControl) { BaseDataBoundControl grv = dataControl as BaseDataBoundControl; grv.DataSource = data; grv.DataBind(); } } if (data is DbDataReader) { data.GetType().GetMethod("Close").Invoke(data, null); } }
private void TestCellSpacing(BaseDataList ctl, GHTSubTest SubTest, int CellSpacing) { try { base.GHTActiveSubTest = SubTest; ctl.DataSource = GHTDataListBase.GHTGetSampleDataSource(); ctl.DataBind(); ctl.CellSpacing = CellSpacing; } catch (Exception exception2) { this.GHTSubTestUnexpectedExceptionCaught(exception2); } }
/// <summary> /// Процедура заполнения любого списочного контрола. /// </summary> /// <param name="list">Название списочного контрола</param> /// <param name="cmd">SQL-команда заполнения списка</param> /// <param name="parameters">Набор SQL-параметров</param> public static void BindList(Control list, SqlCommand cmd, params SqlParameter[] parameters) { if (cmd.Connection == null) { cmd.Connection = CreateConnection(); } cmd.Parameters.AddRange(parameters); using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable table = new DataTable(); sda.Fill(table); if (list is ListControl) { ListControl listControl = list as ListControl; listControl.DataSource = table; listControl.DataValueField = "OptionValue"; listControl.DataTextField = "OptionText"; listControl.DataBind(); } else if (list is BaseDataList) { BaseDataList baseDataList = list as BaseDataList; baseDataList.DataSource = table; baseDataList.DataBind(); } else if (list is Repeater) { Repeater repeater = list as Repeater; repeater.DataSource = table; repeater.DataBind(); } else if (list is System.Web.UI.HtmlControls.HtmlSelect) { System.Web.UI.HtmlControls.HtmlSelect listControl = list as System.Web.UI.HtmlControls.HtmlSelect; listControl.DataSource = table; listControl.DataValueField = "OptionValue"; listControl.DataTextField = "OptionText"; listControl.DataBind(); } } }
private void TestCellSpacingError(BaseDataList ctl, GHTSubTest SubTest, int CellSpacing) { try { base.GHTActiveSubTest = SubTest; ctl.DataBind(); ctl.DataSource = GHTDataListBase.GHTGetSampleDataSource(); ctl.CellSpacing = CellSpacing; this.GHTSubTestExpectedExceptionNotCaught("ArgumentException"); } catch (ArgumentException exception3) { this.GHTSubTestExpectedExceptionCaught(exception3); } catch (Exception exception4) { this.GHTSubTestUnexpectedExceptionCaught(exception4); } }
private void Test(Type CtlType, BaseDataList_ctor_.BuildDataListControl CtlBuilder) { try { this.GHTSubTestBegin("BaseDataList_" + CtlType.Name + "_DataBind1"); BaseDataList list1 = (BaseDataList)this.GHTElementClone(CtlType); base.GHTActiveSubTest.Controls.Add(list1); CtlBuilder(list1); list1.DataSource = GHTDataListBase.GHTGetSampleDataSource(); list1.DataBind(); list1.Caption = "Caption Text"; list1.CaptionAlign = TableCaptionAlign.Right; list1.UseAccessibleHeader = true; } catch (Exception exception2) { this.GHTSubTestUnexpectedExceptionCaught(exception2); } }
private static void Clear <T>(T dataControl) where T : Control { if (SObject.IsTypeNotInList <T>(typeof(BaseDataList), typeof(Repeater), typeof(BaseDataBoundControl))) { throw new InvalidCastException("Control parameter is not valid, you are only alowed to use: BaseDataList, Repeater and BaseDataBoundControl"); } object data = null; try { dataControl.GetType().GetProperty("DataSource").SetValue(dataControl, data); dataControl.GetType().GetMethod("DataBind").Invoke(dataControl, null); } catch (Exception) { if (dataControl is BaseDataList) { BaseDataList dl = dataControl as BaseDataList; dl.DataSource = data; dl.DataBind(); } else if (dataControl is Repeater) { Repeater rptr = dataControl as Repeater; rptr.DataSource = data; rptr.DataBind(); } else if (dataControl is BaseDataBoundControl) { BaseDataBoundControl grv = dataControl as BaseDataBoundControl; grv.DataSource = data; grv.DataBind(); } } }
private void BindData() { // 确保控件存在且为列表控件 _controlToPaginate = Page.FindControl(DataContainer); if (_controlToPaginate == null) { return; } if (!(_controlToPaginate is BaseDataList || _controlToPaginate is ListControl || _controlToPaginate is Repeater)) { return; } // 确保具有足够的连接信息并指定查询 if (ConnString == "") { return; } // 获取数据 FetchPageData(); // 将数据绑定到合作者控件 BaseDataList baseDataListControl = null; ListControl listControl = null; Repeater repeaterControl = null; if (CustomParameters) { if (this._pagedDataSource == null) { this._pagedDataSource = new PagedDataSource(); } this._pagedDataSource.PageSize = PageSize; TotalPage = this._pagedDataSource.PageCount; _pagedDataSource.CurrentPageIndex = CurPage; _pagedDataSource.VirtualCount = RecordCount; _pagedDataSource.DataSource = _dataSource.DefaultView; if (_controlToPaginate is BaseDataList) { baseDataListControl = (BaseDataList)_controlToPaginate; baseDataListControl.DataSource = _pagedDataSource; baseDataListControl.DataBind(); return; } if (_controlToPaginate is ListControl) { listControl = (ListControl)_controlToPaginate; listControl.Items.Clear(); listControl.DataSource = _pagedDataSource; listControl.DataBind(); return; } if (_controlToPaginate is Repeater) { repeaterControl = (Repeater)_controlToPaginate; repeaterControl.DataSource = _pagedDataSource; repeaterControl.DataBind(); return; } } else { if (_controlToPaginate is BaseDataList) { baseDataListControl = (BaseDataList)_controlToPaginate; baseDataListControl.DataSource = _dataSource.DefaultView; baseDataListControl.DataBind(); return; } if (_controlToPaginate is ListControl) { listControl = (ListControl)_controlToPaginate; listControl.Items.Clear(); listControl.DataSource = _dataSource.DefaultView; listControl.DataBind(); return; } if (_controlToPaginate is Repeater) { repeaterControl = (Repeater)_controlToPaginate; repeaterControl.DataSource = _dataSource.DefaultView; repeaterControl.DataBind(); return; } } }
public static void DataShow(this BaseDataList bdl, object datasource) { bdl.DataSource = datasource; bdl.DataBind(); }
/// <summary> /// 数据列表绑定 /// </summary> /// <param name="dli">列表对象</param> public void Bind(BaseDataList dli) { dli.DataSource = this.EnumList; dli.DataBind(); }
/// <summary> /// Bind data to DataList Control /// </summary> /// <param name="dataControl"></param> /// <param name="dataSource"></param> public void BindDataControl(BaseDataList dataControl, object dataSource) { dataControl.DataSource = dataSource; dataControl.DataBind(); }