Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / UI / WebControls / AutoGeneratedField.cs / 1305376 / AutoGeneratedField.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
///
/// Creates a field bounded to a data field in a .
///
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class AutoGeneratedField : BoundField {
private bool _suppressPropertyThrows = false;
private bool _useCheckBox;
private bool _useCheckBoxValid;
///
/// Initializes a new instance of a class.
///
public AutoGeneratedField(string dataField) {
DataField = dataField;
}
///
/// Gets or sets the display format of data in this
/// field.
///
public override string DataFormatString {
get {
return base.DataFormatString;
}
set {
if (!_suppressPropertyThrows) {
throw new NotSupportedException();
}
}
}
///
/// Gets or sets the type of data being displayed by the field.
///
public Type DataType {
get {
object o = ViewState["DataType"];
if (o != null) {
return (Type)o;
}
return typeof(string);
}
set {
ViewState["DataType"] = value;
}
}
///
/// Gets or sets whether the field is visible in Insert mode. Turn off for auto-gen'd db fields
///
public override bool InsertVisible {
get {
return base.InsertVisible;
}
set {
if (!_suppressPropertyThrows) {
throw new NotSupportedException();
}
}
}
///
/// Gets or sets the property that determines whether the BoundField treats empty string as
/// null when the field values are extracted.
///
public override bool ConvertEmptyStringToNull {
get {
return base.ConvertEmptyStringToNull;
}
set {
if (!_suppressPropertyThrows) {
throw new NotSupportedException();
}
}
}
///
/// Determines whether the field should display a text box.
///
private bool UseCheckBox {
get {
if (!_useCheckBoxValid) {
_useCheckBox = (DataType == typeof(bool) || DataType == typeof(bool?));
_useCheckBoxValid = true;
}
return _useCheckBox;
}
}
protected override void CopyProperties(DataControlField newField) {
((AutoGeneratedField)newField).DataType = DataType;
_suppressPropertyThrows = true;
((AutoGeneratedField)newField)._suppressPropertyThrows = true;
base.CopyProperties(newField);
_suppressPropertyThrows = false;
((AutoGeneratedField)newField)._suppressPropertyThrows = false;
}
protected override DataControlField CreateField() {
return new AutoGeneratedField(DataField);
}
///
/// Returns a value to be used for design-time rendering
///
protected override object GetDesignTimeValue() {
if (UseCheckBox) {
return true;
}
return base.GetDesignTimeValue();
}
///
/// Extracts the value of the databound cell and inserts the value into the given dictionary
///
public override void ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly) {
if (UseCheckBox) {
Control childControl = null;
string dataField = DataField;
object value = null;
if (cell.Controls.Count > 0) {
childControl = cell.Controls[0];
CheckBox checkBox = childControl as CheckBox;
if (checkBox != null) {
if (includeReadOnly || checkBox.Enabled) {
value = checkBox.Checked;
}
}
}
if (value != null) {
if (dictionary.Contains(dataField)) {
dictionary[dataField] = value;
}
else {
dictionary.Add(dataField, value);
}
}
}
else {
base.ExtractValuesFromCell(dictionary, cell, rowState, includeReadOnly);
}
}
protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) {
if (UseCheckBox) {
CheckBox childControl = null;
CheckBox boundControl = null;
if (((rowState & DataControlRowState.Edit) != 0 && ReadOnly == false) || (rowState & DataControlRowState.Insert) != 0) {
CheckBox editor = new CheckBox();
childControl = editor;
if (DataField.Length != 0 && (rowState & DataControlRowState.Edit) != 0) {
boundControl = editor;
}
}
else if (DataField.Length != 0) {
CheckBox editor = new CheckBox();
editor.Enabled = false;
childControl = editor;
boundControl = editor;
}
if (childControl != null) {
childControl.ToolTip = HeaderText;
cell.Controls.Add(childControl);
}
if (boundControl != null) {
boundControl.DataBinding += new EventHandler(this.OnDataBindField);
}
}
else {
base.InitializeDataCell(cell, rowState);
}
}
///
///
protected override void OnDataBindField(object sender, EventArgs e) {
if (UseCheckBox) {
Control boundControl = (Control)sender;
Control controlContainer = boundControl.NamingContainer;
object data = GetValue(controlContainer);
if (!(boundControl is CheckBox)) {
throw new HttpException(SR.GetString(SR.CheckBoxField_WrongControlType, DataField));
}
if (DataBinder.IsNull(data)) {
((CheckBox)boundControl).Checked = false;
}
else {
if (data is Boolean) {
((CheckBox)boundControl).Checked = (Boolean)data;
}
else {
try {
((CheckBox)boundControl).Checked = Boolean.Parse(data.ToString());
}
catch (FormatException fe) {
throw new HttpException(SR.GetString(SR.CheckBoxField_CouldntParseAsBoolean, DataField), fe);
}
}
}
}
else {
base.OnDataBindField(sender, e);
}
}
///
/// Override with an empty body if the field's controls all support callback.
/// Otherwise, override and throw a useful error message about why the field can't support callbacks.
///
public override void ValidateSupportsCallback() {
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AppendHelper.cs
- ExtensionQuery.cs
- VisualStyleTypesAndProperties.cs
- MultiSelectRootGridEntry.cs
- PolyBezierSegmentFigureLogic.cs
- XmlCharacterData.cs
- BuiltInExpr.cs
- QueryCursorEventArgs.cs
- AutoGeneratedFieldProperties.cs
- IndexingContentUnit.cs
- SessionIDManager.cs
- MouseCaptureWithinProperty.cs
- String.cs
- CaseInsensitiveHashCodeProvider.cs
- PartialCachingControl.cs
- IChannel.cs
- SoapObjectWriter.cs
- VideoDrawing.cs
- EmitterCache.cs
- BindStream.cs
- InvokeMemberBinder.cs
- ParameterSubsegment.cs
- ConnectionStringSettingsCollection.cs
- InternalControlCollection.cs
- XmlMtomWriter.cs
- storepermission.cs
- Token.cs
- ComponentResourceManager.cs
- CompositeScriptReference.cs
- TeredoHelper.cs
- DataGridViewCellCollection.cs
- ViewGenerator.cs
- Vector3DAnimation.cs
- DataGridCell.cs
- sqlpipe.cs
- SizeConverter.cs
- ColorTransform.cs
- OleDbParameter.cs
- XmlWriterSettings.cs
- EventSetter.cs
- BackEase.cs
- FileDialogPermission.cs
- CodeRegionDirective.cs
- SmtpTransport.cs
- FixedSOMLineCollection.cs
- StringWriter.cs
- EntitySetBaseCollection.cs
- StorageMappingItemCollection.cs
- ViewDesigner.cs
- DefaultAssemblyResolver.cs
- WebPartConnectVerb.cs
- SelectionProviderWrapper.cs
- RecordsAffectedEventArgs.cs
- _CookieModule.cs
- CommandField.cs
- TextSchema.cs
- PointUtil.cs
- XPathPatternBuilder.cs
- NetworkInterface.cs
- ListComponentEditorPage.cs
- XmlDataFileEditor.cs
- DecoderFallbackWithFailureFlag.cs
- QilValidationVisitor.cs
- ObjectNavigationPropertyMapping.cs
- MutexSecurity.cs
- BamlResourceContent.cs
- nulltextcontainer.cs
- AmbientLight.cs
- ResizeBehavior.cs
- tibetanshape.cs
- GridViewRowPresenter.cs
- GeneralTransform3DGroup.cs
- Delegate.cs
- ConnectionPoint.cs
- PartialCachingControl.cs
- RtfNavigator.cs
- LambdaCompiler.Address.cs
- ComponentTray.cs
- DataGridViewTopLeftHeaderCell.cs
- ReflectPropertyDescriptor.cs
- SessionStateUtil.cs
- util.cs
- InvariantComparer.cs
- ClientApiGenerator.cs
- ByteKeyFrameCollection.cs
- EdmMember.cs
- FacetChecker.cs
- XamlParser.cs
- ByteAnimationUsingKeyFrames.cs
- ObjectQueryProvider.cs
- CharUnicodeInfo.cs
- DataListItemEventArgs.cs
- MimeWriter.cs
- DetailsViewRowCollection.cs
- BoundConstants.cs
- NavigateEvent.cs
- TreeViewImageIndexConverter.cs
- XmlSignificantWhitespace.cs
- XmlBinaryWriter.cs
- ZeroOpNode.cs