public void DetailsViewRowCollection_Properties () { DetailsViewRowCollection coll = new DetailsViewRowCollection (myArr); Assert.AreEqual (6, coll.Count, "CountProeprty"); Assert.AreEqual (false, coll.IsReadOnly, "IsReadOnlyProperty"); Assert.AreEqual (false, coll.IsSynchronized, "IsSynchronizedProperty"); Assert.AreEqual (typeof (DetailsViewRowCollection), coll.SyncRoot.GetType() , "SyncRootProperty"); Assert.AreEqual (coll, coll.SyncRoot, "SyncRoot2"); }
internal static void ExtractRowValues(object[] fields, DetailsViewRowCollection rows, string[] dataKeyNames, IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includeKeys) { int cellIndex; // Field and row count should match, but if there was no data, or if the user removed some rows, // these may no longer match. Make sure we don't exceed the bounds. for (int i = 0; i < fields.Length && i < rows.Count; i++) { // If the row isn't a DataRow then skip it if (rows[i].RowType != DataControlRowType.DataRow) { continue; } cellIndex = 0; if (((DataControlField)fields[i]).ShowHeader) { cellIndex = 1; } if (!((DataControlField)fields[i]).Visible) { continue; } OrderedDictionary newValues = new OrderedDictionary(); ((DataControlField)fields[i]).ExtractValuesFromCell(newValues, rows[i].Cells[cellIndex] as DataControlFieldCell, rows[i].RowState, includeReadOnlyFields); foreach (DictionaryEntry entry in newValues) { if (includeKeys || (Array.IndexOf(dataKeyNames, entry.Key) == -1)) { fieldValues[entry.Key] = entry.Value; } } } }
/// <devdoc> /// <para>Creates the control hierarchy that is used to render the DetailsView. /// This is called whenever a control hierarchy is needed and the /// ChildControlsCreated property is false. /// The implementation assumes that all the children in the controls /// collection have already been cleared.</para> /// </devdoc> protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) { PagedDataSource pagedDataSource = null; int itemIndex = PageIndex; bool allowPaging = AllowPaging; int itemCount = 0; DetailsViewMode mode = Mode; // if we're in design mode, PageIndex doesn't return -1 if (DesignMode && mode == DetailsViewMode.Insert) { itemIndex = -1; } if (dataBinding) { DataSourceView view = GetData(); DataSourceSelectArguments arguments = SelectArguments; if (view == null) { throw new HttpException(SR.GetString(SR.DataBoundControl_NullView, ID)); } if (mode != DetailsViewMode.Insert) { if (allowPaging && !view.CanPage) { if (dataSource != null && !(dataSource is ICollection)) { arguments.StartRowIndex = itemIndex; arguments.MaximumRows = 1; // This should throw an exception saying the data source can't page. // We do this because the data source can provide a better error message than we can. view.Select(arguments, SelectCallback); } } if (_useServerPaging) { if (view.CanRetrieveTotalRowCount) { pagedDataSource = CreateServerPagedDataSource(arguments.TotalRowCount); } else { ICollection dataSourceCollection = dataSource as ICollection; if (dataSourceCollection == null) { throw new HttpException(SR.GetString(SR.DataBoundControl_NeedICollectionOrTotalRowCount, GetType().Name)); } pagedDataSource = CreateServerPagedDataSource(checked(PageIndex + dataSourceCollection.Count)); } } else { pagedDataSource = CreatePagedDataSource(); } } } else { pagedDataSource = CreatePagedDataSource(); } if (mode != DetailsViewMode.Insert) { pagedDataSource.DataSource = dataSource; } IEnumerator dataSourceEnumerator = null; OrderedDictionary keyTable = KeyTable; _rowsArray = new ArrayList(); _rowsCollection = null; if (dataBinding == false) { dataSourceEnumerator = dataSource.GetEnumerator(); ICollection collection = dataSource as ICollection; if (collection == null) { throw new HttpException(SR.GetString(SR.DataControls_DataSourceMustBeCollectionWhenNotDataBinding)); } itemCount = collection.Count; } else { keyTable.Clear(); if (dataSource != null) { if (mode != DetailsViewMode.Insert) { ICollection collection = dataSource as ICollection; if ((collection == null) && (pagedDataSource.IsPagingEnabled && !pagedDataSource.IsServerPagingEnabled)) { throw new HttpException(SR.GetString(SR.DetailsView_DataSourceMustBeCollection, ID)); } if (pagedDataSource.IsPagingEnabled) { itemCount = pagedDataSource.DataSourceCount; } else if (collection != null) { itemCount = collection.Count; } } dataSourceEnumerator = dataSource.GetEnumerator(); } } Table table = CreateTable(); TableRowCollection rows = table.Rows; bool moveNextSucceeded = false; object lastItem = null; Controls.Add(table); if (dataSourceEnumerator != null) { moveNextSucceeded = dataSourceEnumerator.MoveNext(); // goto the first item } // if there are no items, only add the tablerow if there's a null template or null text if (!moveNextSucceeded && mode != DetailsViewMode.Insert) { // if we're in insert mode and we're not autogenerating rows, render the rows in insert mode if (itemIndex >= 0 || AutoGenerateRows) { if (EmptyDataText.Length > 0 || _emptyDataTemplate != null) { _rowsArray.Add(CreateRow(0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal, null, rows, null)); } itemCount = 0; } } else { int currentItemIndex = 0; if (!_useServerPaging) { // skip over the first records that are before the page we're showing for (; currentItemIndex < itemIndex; currentItemIndex++) { lastItem = dataSourceEnumerator.Current; moveNextSucceeded = dataSourceEnumerator.MoveNext(); if (!moveNextSucceeded) { _pageIndex = currentItemIndex; pagedDataSource.CurrentPageIndex = currentItemIndex; itemIndex = currentItemIndex; break; // never throw if the PageIndex is out of range: just fix up the current page and goto the last item. } } } if (moveNextSucceeded) { _dataItem = dataSourceEnumerator.Current; } else { _dataItem = lastItem; // if we broke out of the above loop, the current item will be invalid } // If we're not using server paging and this isn't a collection, or server paging doesn't return a page count, our _pageCount isn't accurate. // Loop through the rest of the enumeration to figure out how many items are in it. if ((!_useServerPaging && !(dataSource is ICollection)) || (_useServerPaging && itemCount < 0)) { itemCount = currentItemIndex; while (moveNextSucceeded) { itemCount++; moveNextSucceeded = dataSourceEnumerator.MoveNext(); } } _dataItemIndex = currentItemIndex; bool singlePage = itemCount <= 1 && !_useServerPaging; // hide pagers if there's only one item if (allowPaging && PagerSettings.Visible && _pagerSettings.IsPagerOnTop && !singlePage && mode != DetailsViewMode.Insert) { // top pager _topPagerRow = CreateRow(-1, DataControlRowType.Pager, DataControlRowState.Normal, null, rows, pagedDataSource); } _headerRow = CreateRow(-1, DataControlRowType.Header, DataControlRowState.Normal, null, rows, null); if (_headerTemplate == null && HeaderText.Length == 0) { _headerRow.Visible = false; } _rowsArray.AddRange(CreateDataRows(dataBinding, rows, _dataItem)); if (itemIndex >= 0) { string[] keyFields = DataKeyNamesInternal; if (dataBinding && (keyFields.Length != 0)) { foreach (string keyName in keyFields) { object keyValue = DataBinder.GetPropertyValue(_dataItem, keyName); keyTable.Add(keyName, keyValue); } _dataKey = new DataKey(keyTable); } } _footerRow = CreateRow(-1, DataControlRowType.Footer, DataControlRowState.Normal, null, rows, null); if (_footerTemplate == null && FooterText.Length == 0) { _footerRow.Visible = false; } if (allowPaging && PagerSettings.Visible && _pagerSettings.IsPagerOnBottom && !singlePage && mode != DetailsViewMode.Insert) { // bottom pager _bottomPagerRow = CreateRow(-1, DataControlRowType.Pager, DataControlRowState.Normal, null, rows, pagedDataSource); } } _pageCount = itemCount; OnItemCreated(EventArgs.Empty); if (dataBinding) { DataBind(false); } return itemCount; }
public void DetailsViewRowCollection_CopyTo () { DetailsViewRow[] rows = new DetailsViewRow[6]; ArrayList myRows = new ArrayList (); myRows.Add (new DetailsViewRow (0, DataControlRowType.DataRow, DataControlRowState.Insert)); myRows.Add (new DetailsViewRow (1, DataControlRowType.Footer, DataControlRowState.Edit)); myRows.Add (new DetailsViewRow (2, DataControlRowType.Header, DataControlRowState.Normal)); DetailsViewRowCollection coll = new DetailsViewRowCollection (myRows); coll.CopyTo(rows,0); Assert.AreEqual (6, rows.Length, "CopyTo1"); Assert.AreEqual (0, rows[0].RowIndex, "CopyTo2"); Assert.AreEqual (DataControlRowType.Footer , rows[1].RowType , "CopyTo3"); Assert.AreEqual (DataControlRowState.Normal, rows[2].RowState, "CopyTo4"); Assert.AreEqual (2, rows[2].RowIndex, "CopyTo5"); Assert.AreEqual (null, rows[3], "CopyTo6"); }
public void DetailsViewRowCollection_GetEnumerator () { DetailsViewRowCollection coll = new DetailsViewRowCollection (myArr); IEnumerator e=coll.GetEnumerator (); e.MoveNext (); Assert.AreEqual ("Item1", e.Current, "GetEnumerator1"); Assert.AreEqual ("True", e.MoveNext ().ToString (), "GetEnumerator2"); Assert.AreEqual ("Item2", e.Current, "GetEnumerator3"); }
protected override int CreateChildControls (IEnumerable data, bool dataBinding) { PagedDataSource dataSource; if (dataBinding) { DataSourceView view = GetData (); dataSource = new PagedDataSource (); dataSource.DataSource = data; if (AllowPaging) { dataSource.AllowPaging = true; dataSource.PageSize = 1; dataSource.CurrentPageIndex = PageIndex; if (view.CanPage) { dataSource.AllowServerPaging = true; if (view.CanRetrieveTotalRowCount) dataSource.VirtualCount = SelectArguments.TotalRowCount; else { dataSource.DataSourceView = view; dataSource.DataSourceSelectArguments = SelectArguments; dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount); } } } pageCount = dataSource.PageCount; } else { dataSource = new PagedDataSource (); dataSource.DataSource = data; if (AllowPaging) { dataSource.AllowPaging = true; dataSource.PageSize = 1; dataSource.CurrentPageIndex = PageIndex; } } bool showPager = AllowPaging && (PageCount > 1); Controls.Clear (); table = CreateTable (); Controls.Add (table); ArrayList list = new ArrayList (); if (!Page.IsPostBack) currentMode = DefaultMode; // Gets the current data item IEnumerator e = dataSource.GetEnumerator (); if (e.MoveNext ()) dataItem = e.Current; else dataItem = null; // Creates the set of fields to show ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding); DataControlField[] fields = new DataControlField [fieldCollection.Count]; fieldCollection.CopyTo (fields, 0); foreach (DataControlField field in fields) { field.Initialize (false, this); if (EnablePagingCallbacks) field.ValidateSupportsCallback (); } // Main table creation if (HeaderText.Length != 0 || headerTemplate != null) { headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal); DataControlFieldCell cell = new DataControlFieldCell (null); cell.ColumnSpan = 2; if (headerTemplate != null) headerTemplate.InstantiateIn (cell); else cell.Text = HeaderText; headerRow.Cells.Add (cell); table.Rows.Add (headerRow); } if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) { topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (topPagerRow, dataSource); table.Rows.Add (topPagerRow); } if (dataSource.Count > 0) { foreach (DataControlField field in fields) { DataControlRowState rstate = GetRowState (list.Count); DetailsViewRow row = CreateRow (list.Count, DataControlRowType.DataRow, rstate); InitializeRow (row, field); table.Rows.Add (row); list.Add (row); if (commandField == field) commandRow = row; } if (!dataBinding) { if (CurrentMode == DetailsViewMode.Edit) oldEditValues = new DataKey (new OrderedDictionary ()); key = new DataKey (new OrderedDictionary (), DataKeyNames); } } else { table.Rows.Add (CreateEmptyrRow ()); } rows = new DetailsViewRowCollection (list); if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) { bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (bottomPagerRow, dataSource); table.Rows.Add (bottomPagerRow); } if (FooterText.Length != 0 || footerTemplate != null) { footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal); DataControlFieldCell cell = new DataControlFieldCell (null); cell.ColumnSpan = 2; if (footerTemplate != null) footerTemplate.InstantiateIn (cell); else cell.Text = FooterText; footerRow.Cells.Add (cell); table.Rows.Add (footerRow); } if (dataBinding) DataBind (false); return dataSource.DataSourceCount; }
public DetailsView () { rows = new DetailsViewRowCollection (new ArrayList ()); }
protected override int CreateChildControls (IEnumerable data, bool dataBinding) { PagedDataSource dataSource = new PagedDataSource (); dataSource.DataSource = CurrentMode != DetailsViewMode.Insert ? data : null; dataSource.AllowPaging = AllowPaging; dataSource.PageSize = 1; dataSource.CurrentPageIndex = PageIndex; if (dataBinding && CurrentMode != DetailsViewMode.Insert) { DataSourceView view = GetData (); if (view != null && view.CanPage) { dataSource.AllowServerPaging = true; if (SelectArguments.RetrieveTotalRowCount) dataSource.VirtualCount = SelectArguments.TotalRowCount; } } bool showPager = AllowPaging && (dataSource.PageCount > 1); Controls.Clear (); table = CreateTable (); Controls.Add (table); headerRow = null; footerRow = null; topPagerRow = null; bottomPagerRow = null; ArrayList list = new ArrayList (); // Gets the current data item if (AllowPaging) { PageCount = dataSource.DataSourceCount; if (PageIndex >= PageCount && PageCount > 0) { pageIndex = dataSource.CurrentPageIndex = PageCount - 1; } if (dataSource.DataSource != null) { IEnumerator e = dataSource.GetEnumerator (); if (e.MoveNext ()) dataItem = e.Current; } } else { int page = 0; object lastItem = null; if (dataSource.DataSource != null) { IEnumerator e = dataSource.GetEnumerator (); for (; e.MoveNext (); page++) { lastItem = e.Current; if (page == PageIndex) dataItem = e.Current; } } PageCount = page; if (PageIndex >= PageCount && PageCount > 0) { pageIndex = PageCount - 1; dataItem = lastItem; } } if (PageCount == 0 && CurrentMode != DetailsViewMode.Insert) { DetailsViewRow row = CreateEmptyRow (); if (row != null) { table.Rows.Add (row); list.Add (row); } } else { // Creates the set of fields to show ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding && dataItem != null); DataControlField [] fields = new DataControlField [fieldCollection.Count]; fieldCollection.CopyTo (fields, 0); foreach (DataControlField field in fields) { field.Initialize (false, this); if (EnablePagingCallbacks) field.ValidateSupportsCallback (); } // Main table creation headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal); DataControlFieldCell headerCell = new DataControlFieldCell (null); headerCell.ColumnSpan = 2; if (headerTemplate != null) headerTemplate.InstantiateIn (headerCell); else if (!String.IsNullOrEmpty (HeaderText)) headerCell.Text = HeaderText; else headerRow.Visible = false; headerRow.Cells.Add (headerCell); table.Rows.Add (headerRow); if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) { topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (topPagerRow, dataSource); table.Rows.Add (topPagerRow); } foreach (DataControlField field in fields) { DataControlRowState rstate = GetRowState (list.Count); DetailsViewRow row = CreateRow (PageIndex, DataControlRowType.DataRow, rstate); InitializeRow (row, field); table.Rows.Add (row); list.Add (row); } footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal); DataControlFieldCell footerCell = new DataControlFieldCell (null); footerCell.ColumnSpan = 2; if (footerTemplate != null) footerTemplate.InstantiateIn (footerCell); else if (!String.IsNullOrEmpty (FooterText)) footerCell.Text = FooterText; else footerRow.Visible = false; footerRow.Cells.Add (footerCell); table.Rows.Add (footerRow); if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) { bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal); InitializePager (bottomPagerRow, dataSource); table.Rows.Add (bottomPagerRow); } } rows = new DetailsViewRowCollection (list); if (dataBinding) DataBind (false); OnItemCreated (EventArgs.Empty); return PageCount; }
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) { PagedDataSource pagedDataSource = null; int pageIndex = this.PageIndex; bool allowPaging = this.AllowPaging; int count = 0; DetailsViewMode mode = this.Mode; if (base.DesignMode && (mode == DetailsViewMode.Insert)) { pageIndex = -1; } if (dataBinding) { DataSourceView data = this.GetData(); DataSourceSelectArguments selectArguments = base.SelectArguments; if (data == null) { throw new HttpException(System.Web.SR.GetString("DataBoundControl_NullView", new object[] { this.ID })); } if (mode != DetailsViewMode.Insert) { if ((allowPaging && !data.CanPage) && ((dataSource != null) && !(dataSource is ICollection))) { selectArguments.StartRowIndex = pageIndex; selectArguments.MaximumRows = 1; data.Select(selectArguments, new DataSourceViewSelectCallback(this.SelectCallback)); } if (this._useServerPaging) { if (data.CanRetrieveTotalRowCount) { pagedDataSource = this.CreateServerPagedDataSource(selectArguments.TotalRowCount); } else { ICollection is2 = dataSource as ICollection; if (is2 == null) { throw new HttpException(System.Web.SR.GetString("DataBoundControl_NeedICollectionOrTotalRowCount", new object[] { base.GetType().Name })); } pagedDataSource = this.CreateServerPagedDataSource(this.PageIndex + is2.Count); } } else { pagedDataSource = this.CreatePagedDataSource(); } } } else { pagedDataSource = this.CreatePagedDataSource(); } if (mode != DetailsViewMode.Insert) { pagedDataSource.DataSource = dataSource; } IEnumerator enumerator = null; OrderedDictionary keyTable = this.KeyTable; this._rowsArray = new ArrayList(); this._rowsCollection = null; if (!dataBinding) { enumerator = dataSource.GetEnumerator(); ICollection is3 = dataSource as ICollection; if (is3 == null) { throw new HttpException(System.Web.SR.GetString("DataControls_DataSourceMustBeCollectionWhenNotDataBinding")); } count = is3.Count; } else { keyTable.Clear(); if (dataSource != null) { if (mode != DetailsViewMode.Insert) { ICollection is4 = dataSource as ICollection; if (((is4 == null) && pagedDataSource.IsPagingEnabled) && !pagedDataSource.IsServerPagingEnabled) { throw new HttpException(System.Web.SR.GetString("DetailsView_DataSourceMustBeCollection", new object[] { this.ID })); } if (pagedDataSource.IsPagingEnabled) { count = pagedDataSource.DataSourceCount; } else if (is4 != null) { count = is4.Count; } } enumerator = dataSource.GetEnumerator(); } } Table child = this.CreateTable(); TableRowCollection rows = child.Rows; bool flag2 = false; object current = null; this.Controls.Add(child); if (enumerator != null) { flag2 = enumerator.MoveNext(); } if (!flag2 && (mode != DetailsViewMode.Insert)) { if ((pageIndex >= 0) || this.AutoGenerateRows) { if ((this.EmptyDataText.Length > 0) || (this._emptyDataTemplate != null)) { this._rowsArray.Add(this.CreateRow(0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal, null, rows, null)); } count = 0; } } else { int num3 = 0; if (!this._useServerPaging) { while (num3 < pageIndex) { current = enumerator.Current; flag2 = enumerator.MoveNext(); if (!flag2) { this._pageIndex = num3; pagedDataSource.CurrentPageIndex = num3; pageIndex = num3; break; } num3++; } } if (flag2) { this._dataItem = enumerator.Current; } else { this._dataItem = current; } if ((!this._useServerPaging && !(dataSource is ICollection)) || (this._useServerPaging && (count < 0))) { count = num3; while (flag2) { count++; flag2 = enumerator.MoveNext(); } } this._dataItemIndex = num3; bool flag3 = (count <= 1) && !this._useServerPaging; if (((allowPaging && this.PagerSettings.Visible) && (this._pagerSettings.IsPagerOnTop && !flag3)) && (mode != DetailsViewMode.Insert)) { this._topPagerRow = this.CreateRow(-1, DataControlRowType.Pager, DataControlRowState.Normal, null, rows, pagedDataSource); } this._headerRow = this.CreateRow(-1, DataControlRowType.Header, DataControlRowState.Normal, null, rows, null); if ((this._headerTemplate == null) && (this.HeaderText.Length == 0)) { this._headerRow.Visible = false; } this._rowsArray.AddRange(this.CreateDataRows(dataBinding, rows, this._dataItem)); if (pageIndex >= 0) { string[] dataKeyNamesInternal = this.DataKeyNamesInternal; if (dataBinding && (dataKeyNamesInternal.Length != 0)) { foreach (string str in dataKeyNamesInternal) { object propertyValue = DataBinder.GetPropertyValue(this._dataItem, str); keyTable.Add(str, propertyValue); } this._dataKey = new System.Web.UI.WebControls.DataKey(keyTable); } } this._footerRow = this.CreateRow(-1, DataControlRowType.Footer, DataControlRowState.Normal, null, rows, null); if ((this._footerTemplate == null) && (this.FooterText.Length == 0)) { this._footerRow.Visible = false; } if (((allowPaging && this.PagerSettings.Visible) && (this._pagerSettings.IsPagerOnBottom && !flag3)) && (mode != DetailsViewMode.Insert)) { this._bottomPagerRow = this.CreateRow(-1, DataControlRowType.Pager, DataControlRowState.Normal, null, rows, pagedDataSource); } } this._pageCount = count; this.OnItemCreated(EventArgs.Empty); if (dataBinding) { this.DataBind(false); } return count; }
internal static void ExtractRowValues(object[] fields, DetailsViewRowCollection rows, string[] dataKeyNames, IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includeKeys) { for (int i = 0; (i < fields.Length) && (i < rows.Count); i++) { if (rows[i].RowType == DataControlRowType.DataRow) { int num = 0; if (((DataControlField) fields[i]).ShowHeader) { num = 1; } if (((DataControlField) fields[i]).Visible) { OrderedDictionary dictionary = new OrderedDictionary(); ((DataControlField) fields[i]).ExtractValuesFromCell(dictionary, rows[i].Cells[num] as DataControlFieldCell, rows[i].RowState, includeReadOnlyFields); foreach (DictionaryEntry entry in dictionary) { if (includeKeys || (Array.IndexOf<object>(dataKeyNames, entry.Key) == -1)) { fieldValues[entry.Key] = entry.Value; } } } } } }