1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
//------------------------------------------------------------------------------
// <copyright file="BaseDataList.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web;
using System.Web.UI;
using System.Web.Util;
/// <devdoc>
/// <para>Serves as the abstract base class for the <see cref='System.Web.UI.WebControls.DataList'/> and <see cref='System.Web.UI.WebControls.DataGrid'/>
/// controls and implements the selection semantics which are common to both
/// controls.</para>
/// </devdoc>
[
DefaultEvent("SelectedIndexChanged"),
DefaultProperty("DataSource"),
Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + AssemblyRef.SystemDesign)
]
public abstract class BaseDataList : WebControl {
private static readonly object EventSelectedIndexChanged = new object();
internal const string ItemCountViewStateKey = "_!ItemCount";
private object dataSource;
private DataKeyCollection dataKeysCollection;
private bool _requiresDataBinding;
private bool _inited;
private bool _throwOnDataPropertyChange;
private DataSourceView _currentView;
private bool _currentViewIsFromDataSourceID;
private bool _currentViewValid;
private DataSourceSelectArguments _arguments;
private bool _pagePreLoadFired;
[
DefaultValue(""),
Localizable(true),
WebCategory("Accessibility"),
WebSysDescription(SR.DataControls_Caption)
]
public virtual string Caption {
get {
string s = (string)ViewState["Caption"];
return (s != null) ? s : String.Empty;
}
set {
ViewState["Caption"] = value;
}
}
[
DefaultValue(TableCaptionAlign.NotSet),
WebCategory("Accessibility"),
WebSysDescription(SR.WebControl_CaptionAlign)
]
public virtual TableCaptionAlign CaptionAlign {
get {
object o = ViewState["CaptionAlign"];
return (o != null) ? (TableCaptionAlign)o : TableCaptionAlign.NotSet;
}
set {
if ((value < TableCaptionAlign.NotSet) ||
(value > TableCaptionAlign.Right)) {
throw new ArgumentOutOfRangeException("value");
}
ViewState["CaptionAlign"] = value;
}
}
/// <devdoc>
/// <para>Indicates the amount of space between cells.</para>
/// </devdoc>
[
WebCategory("Layout"),
DefaultValue(-1),
WebSysDescription(SR.BaseDataList_CellPadding)
]
public virtual int CellPadding {
get {
if (ControlStyleCreated == false) {
return -1;
}
return ((TableStyle)ControlStyle).CellPadding;
}
set {
((TableStyle)ControlStyle).CellPadding = value;
}
}
/// <devdoc>
/// <para>Gets or sets the amount of space between the contents of
/// a cell and the cell's border.</para>
/// </devdoc>
[
WebCategory("Layout"),
DefaultValue(0),
WebSysDescription(SR.BaseDataList_CellSpacing)
]
public virtual int CellSpacing {
get {
if (ControlStyleCreated == false) {
return 0;
}
return ((TableStyle)ControlStyle).CellSpacing;
}
set {
((TableStyle)ControlStyle).CellSpacing = value;
}
}
public override ControlCollection Controls {
get {
EnsureChildControls();
return base.Controls;
}
}
/// <devdoc>
/// </devdoc>
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
WebSysDescription(SR.BaseDataList_DataKeys)
]
public DataKeyCollection DataKeys {
get {
if (dataKeysCollection == null) {
dataKeysCollection = new DataKeyCollection(this.DataKeysArray);
}
return dataKeysCollection;
}
}
/// <devdoc>
/// </devdoc>
protected ArrayList DataKeysArray {
get {
object o = ViewState["DataKeys"];
if (o == null) {
o = new ArrayList();
ViewState["DataKeys"] = o;
}
return(ArrayList)o;
}
}
/// <devdoc>
/// <para>Indicatesthe primary key field in the data source referenced by <see cref='System.Web.UI.WebControls.BaseDataList.DataSource'/>.</para>
/// </devdoc>
[
DefaultValue(""),
Themeable(false),
WebCategory("Data"),
WebSysDescription(SR.BaseDataList_DataKeyField)
]
public virtual string DataKeyField {
get {
object o = ViewState["DataKeyField"];
if (o != null)
return(string)o;
return String.Empty;
}
set {
ViewState["DataKeyField"] = value;
}
}
/// <devdoc>
/// </devdoc>
[
DefaultValue(""),
Themeable(false),
WebCategory("Data"),
WebSysDescription(SR.BaseDataList_DataMember)
]
public string DataMember {
get {
object o = ViewState["DataMember"];
if (o != null)
return (string)o;
return String.Empty;
}
set {
ViewState["DataMember"] = value;
OnDataPropertyChanged();
}
}
/// <devdoc>
/// <para>Gets
/// or sets the source to a list of values used to populate
/// the items within the control.</para>
/// </devdoc>
[
Bindable(true),
DefaultValue(null),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Themeable(false),
WebCategory("Data"),
WebSysDescription(SR.BaseDataBoundControl_DataSource),
]
public virtual object DataSource {
get {
return dataSource;
}
set {
if ((value == null) || (value is IListSource) || (value is IEnumerable)) {
dataSource = value;
OnDataPropertyChanged();
}
else {
throw new ArgumentException(SR.GetString(SR.Invalid_DataSource_Type, ID));
}
}
}
/// <summary>
/// The ID of the DataControl that this control should use to retrieve
/// its data source. When the control is bound to a DataControl, it
/// can retrieve a data source instance on-demand, and thereby attempt
/// to work in auto-DataBind mode.
/// </summary>
[
DefaultValue(""),
IDReferenceProperty(typeof(DataSourceControl)),
Themeable(false),
WebCategory("Data"),
WebSysDescription(SR.BaseDataBoundControl_DataSourceID)
]
public virtual string DataSourceID {
get {
object o = ViewState["DataSourceID"];
if (o != null) {
return (string)o;
}
return String.Empty;
}
set {
ViewState["DataSourceID"] = value;
OnDataPropertyChanged();
}
}
/// <devdoc>
/// <para>Gets or sets a value that specifies the grid line style.</para>
/// </devdoc>
[
WebCategory("Appearance"),
DefaultValue(GridLines.Both),
WebSysDescription(SR.DataControls_GridLines)
]
public virtual GridLines GridLines {
get {
if (ControlStyleCreated == false) {
return GridLines.Both;
}
return ((TableStyle)ControlStyle).GridLines;
}
set {
((TableStyle)ControlStyle).GridLines = value;
}
}
/// <devdoc>
/// <para>Gets or sets a value that specifies the alignment of a rows with respect
/// surrounding text.</para>
/// </devdoc>
[
Category("Layout"),
DefaultValue(HorizontalAlign.NotSet),
WebSysDescription(SR.WebControl_HorizontalAlign)
]
public virtual HorizontalAlign HorizontalAlign {
get {
if (ControlStyleCreated == false) {
return HorizontalAlign.NotSet;
}
return ((TableStyle)ControlStyle).HorizontalAlign;
}
set {
((TableStyle)ControlStyle).HorizontalAlign = value;
}
}
protected bool Initialized {
get {
return _inited;
}
}
protected bool IsBoundUsingDataSourceID {
get {
return (DataSourceID.Length > 0);
}
}
public override bool SupportsDisabledAttribute {
get {
return RenderingCompatibility < VersionUtil.Framework40;
}
}
protected bool RequiresDataBinding {
get {
return _requiresDataBinding;
}
set {
_requiresDataBinding = value;
}
}
protected DataSourceSelectArguments SelectArguments {
get {
if (_arguments == null) {
_arguments = CreateDataSourceSelectArguments();
}
return _arguments;
}
}
[
DefaultValue(false),
WebCategory("Accessibility"),
WebSysDescription(SR.Table_UseAccessibleHeader)
]
public virtual bool UseAccessibleHeader {
get {
object b = ViewState["UseAccessibleHeader"];
return (b != null) ? (bool)b : false;
}
set {
ViewState["UseAccessibleHeader"] = value;
}
}
/// <devdoc>
/// <para>Occurs when an item on the list is selected.</para>
/// </devdoc>
[
WebCategory("Action"),
WebSysDescription(SR.BaseDataList_OnSelectedIndexChanged)
]
public event EventHandler SelectedIndexChanged {
add {
Events.AddHandler(EventSelectedIndexChanged, value);
}
remove {
Events.RemoveHandler(EventSelectedIndexChanged, value);
}
}
/// <devdoc>
/// <para> Not coded yet.</para>
/// </devdoc>
protected override void AddParsedSubObject(object obj) {
return;
}
/// <devdoc>
/// Connects this data bound control to the appropriate DataSourceView
/// and hooks up the appropriate event listener for the
/// DataSourceViewChanged event. The return value is the new view (if
/// any) that was connected to. An exception is thrown if there is
/// a problem finding the requested view or data source.
/// </devdoc>
private DataSourceView ConnectToDataSourceView() {
if (_currentViewValid && !DesignMode) {
// If the current view is correct, there is no need to reconnect
return _currentView;
}
// Disconnect from old view, if necessary
if ((_currentView != null) && (_currentViewIsFromDataSourceID)) {
// We only care about this event if we are bound through the DataSourceID property
_currentView.DataSourceViewChanged -= new EventHandler(OnDataSourceViewChanged);
}
// Connect to new view
IDataSource ds = null;
string dataSourceID = DataSourceID;
if (dataSourceID.Length != 0) {
// Try to find a DataSource control with the ID specified in DataSourceID
Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
if (control == null) {
throw new HttpException(SR.GetString(SR.DataControl_DataSourceDoesntExist, ID, dataSourceID));
}
ds = control as IDataSource;
if (ds == null) {
throw new HttpException(SR.GetString(SR.DataControl_DataSourceIDMustBeDataControl, ID, dataSourceID));
}
}
if (ds == null) {
// DataSource control was not found, construct a temporary data source to wrap the data
ds = new ReadOnlyDataSource(DataSource, DataMember);
}
else {
// Ensure that both DataSourceID as well as DataSource are not set at the same time
if (DataSource != null) {
throw new InvalidOperationException(SR.GetString(SR.DataControl_MultipleDataSources, ID));
}
}
// IDataSource was found, extract the appropriate view and return it
DataSourceView newView = ds.GetView(DataMember);
if (newView == null) {
throw new InvalidOperationException(SR.GetString(SR.DataControl_ViewNotFound, ID));
}
_currentViewIsFromDataSourceID = IsBoundUsingDataSourceID;
_currentView = newView;
if ((_currentView != null) && (_currentViewIsFromDataSourceID)) {
// We only care about this event if we are bound through the DataSourceID property
_currentView.DataSourceViewChanged += new EventHandler(OnDataSourceViewChanged);
}
_currentViewValid = true;
return _currentView;
}
/// <internalonly/>
/// <devdoc>
/// <para>Creates a child control using the view state.</para>
/// </devdoc>
protected internal override void CreateChildControls() {
Controls.Clear();
if (ViewState[ItemCountViewStateKey] == null) {
if (RequiresDataBinding) {
EnsureDataBound();
}
}
else {
// create the control hierarchy using the view state (and
// not the datasource)
CreateControlHierarchy(false);
ClearChildViewState();
}
}
protected abstract void CreateControlHierarchy(bool useDataSource);
public override void DataBind() {
// Don't databind to a data source control when the control is in the designer but not top-level
if (IsBoundUsingDataSourceID && DesignMode && (Site == null)) {
return;
}
// do our own databinding
RequiresDataBinding = false;
OnDataBinding(EventArgs.Empty);
}
protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments() {
return DataSourceSelectArguments.Empty;
}
protected void EnsureDataBound() {
try {
_throwOnDataPropertyChange = true;
if (RequiresDataBinding && DataSourceID.Length > 0) {
DataBind();
}
}
finally {
_throwOnDataPropertyChange = false;
}
}
/// <devdoc>
/// Returns an IEnumerable that is the DataSource, which either came
/// from the DataSource property or from the control bound via the
/// DataSourceID property.
/// </devdoc>
protected virtual IEnumerable GetData() {
ConnectToDataSourceView();
Debug.Assert(_currentViewValid);
if (_currentView != null) {
return _currentView.ExecuteSelect(SelectArguments);
}
return null;
}
/// <devdoc>
/// <para>Determines if the specified data type can be bound to.</para>
/// </devdoc>
public static bool IsBindableType(Type type) {
return(type.IsPrimitive ||
(type == typeof(string)) ||
(type == typeof(DateTime)) ||
(type == typeof(Decimal)));
}
/// <internalonly/>
/// <devdoc>
/// <para>Raises the <see langword='DataBinding '/>event of a <see cref='System.Web.UI.WebControls.BaseDataList'/>
/// .</para>
/// </devdoc>
protected override void OnDataBinding(EventArgs e) {
base.OnDataBinding(e);
// reset the control state
Controls.Clear();
ClearChildViewState();
// and create the control hierarchy using the datasource
dataKeysCollection = null;
CreateControlHierarchy(true);
ChildControlsCreated = true;
TrackViewState();
}
/// <devdoc>
/// This method is called when DataMember, DataSource, or DataSourceID is changed.
/// </devdoc>
protected virtual void OnDataPropertyChanged() {
if (_throwOnDataPropertyChange) {
throw new HttpException(SR.GetString(SR.DataBoundControl_InvalidDataPropertyChange, ID));
}
if (_inited) {
RequiresDataBinding = true;
}
_currentViewValid = false;
}
protected virtual void OnDataSourceViewChanged(object sender, EventArgs e) {
RequiresDataBinding = true;
}
protected internal override void OnInit(EventArgs e) {
base.OnInit(e);
if (Page != null) {
Page.PreLoad += new EventHandler(this.OnPagePreLoad);
if (!IsViewStateEnabled && Page.IsPostBack) {
RequiresDataBinding = true;
}
}
}
protected internal override void OnLoad(EventArgs e) {
_inited = true; // just in case we were added to the page after PreLoad
ConnectToDataSourceView();
if (Page != null && !_pagePreLoadFired && ViewState[ItemCountViewStateKey] == null) {
// If the control was added after PagePreLoad, we still need to databind it because it missed its
// first change in PagePreLoad. If this control was created by a call to a parent control's DataBind
// in Page_Load (with is relatively common), this control will already have been databound even
// though pagePreLoad never fired and the page isn't a postback.
if (!Page.IsPostBack) {
RequiresDataBinding = true;
}
// If the control was added to the page after page.PreLoad, we'll never get the event and we'll
// never databind the control. So if we're catching up and Load happens but PreLoad never happened,
// call DataBind. This may make the control get databound twice if the user called DataBind on the control
// directly in Page.OnLoad, but better to bind twice than never to bind at all.
else if (IsViewStateEnabled) {
RequiresDataBinding = true;
}
}
base.OnLoad(e);
}
private void OnPagePreLoad(object sender, EventArgs e) {
_inited = true;
if (Page != null) {
Page.PreLoad -= new EventHandler(this.OnPagePreLoad);
// Setting RequiresDataBinding to true in OnLoad is too late because the OnLoad page event
// happens before the control.OnLoad method gets called. So a page_load handler on the page
// that calls DataBind won't prevent DataBind from getting called again in PreRender.
if (!Page.IsPostBack) {
RequiresDataBinding = true;
}
// If this is a postback and viewstate is enabled, but we have never bound the control
// before, it is probably because its visibility was changed in the postback. In this
// case, we need to bind the control or it will never appear. This is a common scenario
// for Wizard and MultiView.
if (Page.IsPostBack && IsViewStateEnabled && ViewState[ItemCountViewStateKey] == null) {
RequiresDataBinding = true;
}
}
_pagePreLoadFired = true;
}
protected internal override void OnPreRender(EventArgs e) {
EnsureDataBound();
base.OnPreRender(e);
}
/// <devdoc>
/// <para>Raises the <see cref='System.Web.UI.WebControls.BaseDataList.SelectedIndexChanged'/>event of a <see cref='System.Web.UI.WebControls.BaseDataList'/>.</para>
/// </devdoc>
protected virtual void OnSelectedIndexChanged(EventArgs e) {
EventHandler handler = (EventHandler)Events[EventSelectedIndexChanged];
if (handler != null) handler(this, e);
}
protected internal abstract void PrepareControlHierarchy();
/// <internalonly/>
/// <devdoc>
/// <para>Displays the control on the client.</para>
/// </devdoc>
protected internal override void Render(HtmlTextWriter writer) {
PrepareControlHierarchy();
RenderContents(writer);
}
}
}
|