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 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
|
//
// System.Web.UI.WebControls.DataGrid.cs
//
// Authors:
// Jackson Harper (jackson@ximian.com)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2005-2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Web.Util;
using System.Collections;
using System.Globalization;
using System.ComponentModel;
using System.Reflection;
using System.Security.Permissions;
namespace System.Web.UI.WebControls
{
// CAS
[AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
// attributes
[Editor("System.Web.UI.Design.WebControls.DataGridComponentEditor, " + Consts.AssemblySystem_Design, typeof(System.ComponentModel.ComponentEditor))]
[Designer("System.Web.UI.Design.WebControls.DataGridDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
public class DataGrid : BaseDataList, INamingContainer
{
public const string CancelCommandName = "Cancel";
public const string DeleteCommandName = "Delete";
public const string EditCommandName = "Edit";
public const string SelectCommandName = "Select";
public const string SortCommandName = "Sort";
public const string UpdateCommandName = "Update";
public const string PageCommandName = "Page";
public const string NextPageCommandArgument = "Next";
public const string PrevPageCommandArgument = "Prev";
static readonly object CancelCommandEvent = new object ();
static readonly object DeleteCommandEvent = new object ();
static readonly object EditCommandEvent = new object ();
static readonly object ItemCommandEvent = new object ();
static readonly object ItemCreatedEvent = new object ();
static readonly object ItemDataBoundEvent = new object ();
static readonly object PageIndexChangedEvent = new object ();
static readonly object SortCommandEvent = new object ();
static readonly object UpdateCommandEvent = new object ();
TableItemStyle alt_item_style;
TableItemStyle edit_item_style;
TableItemStyle footer_style;
TableItemStyle header_style;
TableItemStyle item_style;
TableItemStyle selected_style;
DataGridPagerStyle pager_style;
ArrayList items_list;
DataGridItemCollection items;
ArrayList columns_list;
DataGridColumnCollection columns;
ArrayList data_source_columns_list;
DataGridColumnCollection data_source_columns;
Table render_table;
DataGridColumn [] render_columns;
PagedDataSource paged_data_source;
IEnumerator data_enumerator;
[DefaultValue(false)]
[WebSysDescription ("")]
[WebCategory ("Paging")]
public virtual bool AllowCustomPaging {
get { return ViewState.GetBool ("AllowCustomPaging", false); }
set { ViewState ["AllowCustomPaging"] = value; }
}
[DefaultValue(false)]
[WebSysDescription ("")]
[WebCategory ("Paging")]
public virtual bool AllowPaging {
get { return ViewState.GetBool ("AllowPaging", false); }
set { ViewState ["AllowPaging"] = value; }
}
[DefaultValue(false)]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
public virtual bool AllowSorting {
get { return ViewState.GetBool ("AllowSorting", false); }
set { ViewState ["AllowSorting"] = value; }
}
[DefaultValue(true)]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
public virtual bool AutoGenerateColumns {
get { return ViewState.GetBool ("AutoGenerateColumns", true); }
set { ViewState ["AutoGenerateColumns"] = value; }
}
[UrlProperty]
[DefaultValue("")]
[Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
public virtual string BackImageUrl {
get { return TableStyle.BackImageUrl; }
set { TableStyle.BackImageUrl = value; }
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
public int CurrentPageIndex {
get { return ViewState.GetInt ("CurrentPageIndex", 0); }
set {
if (value < 0)
throw new ArgumentOutOfRangeException ("value");
ViewState ["CurrentPageIndex"] = value;
}
}
[DefaultValue(-1)]
[WebSysDescription ("")]
[WebCategory ("Misc")]
public virtual int EditItemIndex {
get { return ViewState.GetInt ("EditItemIndex", -1); }
set {
if (value < -1)
throw new ArgumentOutOfRangeException ("value");
ViewState ["EditItemIndex"] = value;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public int PageCount {
get {
if (paged_data_source != null)
return paged_data_source.PageCount;
return ViewState.GetInt ("PageCount", 0);
}
}
[DefaultValue(10)]
[WebSysDescription ("")]
[WebCategory ("Paging")]
public virtual int PageSize {
get { return ViewState.GetInt ("PageSize", 10); }
set {
if (value < 1)
throw new ArgumentOutOfRangeException ("value");
ViewState ["PageSize"] = value;
}
}
void AdjustItemTypes (int prev_select, int new_select)
{
if (items_list == null)
return; // nothing to select
int count = items_list.Count;
if (count == 0)
return; // nothing to select
DataGridItem item;
// Restore item type for the previously selected one.
if (prev_select >= 0 && prev_select < count) {
item = (DataGridItem) items_list [prev_select];
if (item.ItemType == ListItemType.EditItem) {
// nothing to do. This has priority.
} else if ((item.ItemIndex % 2) != 0)
item.SetItemType (ListItemType.AlternatingItem);
else
item.SetItemType (ListItemType.Item);
}
if (new_select == -1 || new_select >= count)
return; // nothing to select
item = (DataGridItem) items_list [new_select];
if (item.ItemType != ListItemType.EditItem) // EditItem takes precedence
item.SetItemType (ListItemType.SelectedItem);
}
[Bindable(true)]
[DefaultValue(-1)]
[WebSysDescription ("")]
[WebCategory ("Paging")]
public virtual int SelectedIndex {
get { return ViewState.GetInt ("SelectedIndex", -1); }
set {
if (value < -1)
throw new ArgumentOutOfRangeException ("value");
int selected_index = ViewState.GetInt ("SelectedIndex", -1);
AdjustItemTypes (selected_index, value);
ViewState ["SelectedIndex"] = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual TableItemStyle AlternatingItemStyle {
get {
if (alt_item_style == null) {
alt_item_style = new TableItemStyle ();
if (IsTrackingViewState)
alt_item_style.TrackViewState ();
}
return alt_item_style;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual TableItemStyle EditItemStyle {
get {
if (edit_item_style == null) {
edit_item_style = new TableItemStyle ();
if (IsTrackingViewState)
edit_item_style.TrackViewState ();
}
return edit_item_style;
}
}
[DefaultValue(null)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual TableItemStyle FooterStyle {
get {
if (footer_style == null) {
footer_style = new TableItemStyle ();
if (IsTrackingViewState)
footer_style.TrackViewState ();
}
return footer_style;
}
}
[DefaultValue(null)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual TableItemStyle HeaderStyle {
get {
if (header_style == null) {
header_style = new TableItemStyle ();
if (IsTrackingViewState)
header_style.TrackViewState ();
}
return header_style;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual TableItemStyle ItemStyle {
get {
if (item_style == null) {
item_style = new TableItemStyle ();
if (IsTrackingViewState)
item_style.TrackViewState ();
}
return item_style;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual TableItemStyle SelectedItemStyle {
get {
if (selected_style == null) {
selected_style = new TableItemStyle ();
if (IsTrackingViewState)
selected_style.TrackViewState ();
}
return selected_style;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual DataGridPagerStyle PagerStyle {
get {
if (pager_style == null) {
pager_style = new DataGridPagerStyle ();
if (IsTrackingViewState)
pager_style.TrackViewState ();
}
return pager_style;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Style")]
public virtual DataGridItemCollection Items {
get {
EnsureChildControls ();
if (items == null) {
if (items_list == null)
items_list = new ArrayList ();
items = new DataGridItemCollection (items_list);
}
return items;
}
}
[DefaultValue (null)]
[Editor ("System.Web.UI.Design.WebControls.DataGridColumnCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
[MergableProperty (false)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("")]
[WebCategory ("Behavior")]
public virtual DataGridColumnCollection Columns {
get {
if (columns == null) {
columns_list = new ArrayList ();
columns = new DataGridColumnCollection (this, columns_list);
if (IsTrackingViewState) {
IStateManager manager = (IStateManager) columns;
manager.TrackViewState ();
}
}
return columns;
}
}
DataGridColumnCollection DataSourceColumns {
get {
if (data_source_columns == null) {
data_source_columns_list = new ArrayList ();
data_source_columns = new DataGridColumnCollection (this, data_source_columns_list);
if (IsTrackingViewState) {
IStateManager manager = (IStateManager) data_source_columns;
manager.TrackViewState ();
}
}
return data_source_columns;
}
}
Table RenderTable {
get {
if (render_table == null) {
render_table = new ChildTable (this);
render_table.AutoID = false;
}
return render_table;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Paging")]
public virtual DataGridItem SelectedItem {
get {
if (SelectedIndex == -1)
return null;
return Items [SelectedIndex];
}
}
[DefaultValue(false)]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
public virtual bool ShowFooter {
get { return ViewState.GetBool ("ShowFooter", false); }
set { ViewState ["ShowFooter"] = value; }
}
[DefaultValue(true)]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
public virtual bool ShowHeader {
get { return ViewState.GetBool ("ShowHeader", true); }
set { ViewState ["ShowHeader"] = value; }
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("")]
[WebCategory ("Appearance")]
public virtual int VirtualItemCount {
get { return ViewState.GetInt ("VirtualItemCount", 0); }
set {
if (value < 0)
throw new ArgumentOutOfRangeException ("value");
ViewState ["VirtualItemCount"] = value;
}
}
protected override HtmlTextWriterTag TagKey {
get { return HtmlTextWriterTag.Table; }
}
TableStyle TableStyle {
get { return (TableStyle) ControlStyle; }
}
static Type [] item_args = new Type [] {typeof (int) };
void AddColumnsFromSource (PagedDataSource data_source)
{
PropertyDescriptorCollection props = null;
Type ptype = null;
bool no_items = false;
// Use plain reflection for the Item property.
// If we use TypeDescriptor, props will hold
// all of the Type properties, which will be listed as columns
Type ds_type = data_source.GetType ();
PropertyInfo pinfo = ds_type.GetProperty ("Item", item_args);
if (pinfo == null) {
IEnumerator items = (data_source.DataSource != null) ? data_source.GetEnumerator () : null;
if (items != null && items.MoveNext ()) {
object data = items.Current;
if ((data is ICustomTypeDescriptor) || (!IsBindableType(data.GetType())))
props = TypeDescriptor.GetProperties (data);
else if (data != null)
ptype = data.GetType ();
data_enumerator = items;
} else
no_items = true;
} else
ptype = pinfo.PropertyType;
if (ptype != null) {
// Found the "Item" property
AddPropertyToColumns ();
} else if (props != null) {
foreach (PropertyDescriptor pd in props)
AddPropertyToColumns (pd, false);
} else if (!no_items) {
// This is not thrown for an empty ArrayList.
string msg = String.Format ("DataGrid '{0}' cannot autogenerate " +
"columns from the given datasource. {1}", ID, ptype);
throw new HttpException (msg);
}
}
protected virtual ArrayList CreateColumnSet (PagedDataSource dataSource, bool useDataSource)
{
ArrayList res = new ArrayList ();
if (columns_list != null)
res.AddRange (columns_list);
if (AutoGenerateColumns) {
if (useDataSource) {
data_enumerator = null;
PropertyDescriptorCollection props = dataSource.GetItemProperties (null);
DataSourceColumns.Clear ();
if (props != null) {
foreach (PropertyDescriptor d in props)
AddPropertyToColumns (d, false);
} else {
AddColumnsFromSource (dataSource);
}
}
if (data_source_columns != null && data_source_columns.Count > 0)
res.AddRange (data_source_columns);
}
return res;
}
void AddPropertyToColumns ()
{
BoundColumn b = new BoundColumn ();
if (IsTrackingViewState) {
IStateManager m = (IStateManager) b;
m.TrackViewState ();
}
b.Set_Owner (this);
b.HeaderText = "Item";
b.SortExpression = "Item";
b.DataField = BoundColumn.thisExpr;
DataSourceColumns.Add (b);
}
void AddPropertyToColumns (PropertyDescriptor prop, bool tothis)
{
BoundColumn b = new BoundColumn ();
b.Set_Owner (this);
if (IsTrackingViewState) {
IStateManager m = (IStateManager) b;
m.TrackViewState ();
}
b.HeaderText = prop.Name;
b.DataField = (tothis ? BoundColumn.thisExpr : prop.Name);
b.SortExpression = prop.Name;
if (String.Compare (DataKeyField, b.DataField, StringComparison.OrdinalIgnoreCase) == 0)
b.ReadOnly = true;
DataSourceColumns.Add (b);
}
protected override void TrackViewState ()
{
base.TrackViewState ();
if (pager_style != null)
pager_style.TrackViewState ();
if (header_style != null)
header_style.TrackViewState ();
if (footer_style != null)
footer_style.TrackViewState ();
if (item_style != null)
item_style.TrackViewState ();
if (alt_item_style != null)
alt_item_style.TrackViewState ();
if (selected_style != null)
selected_style.TrackViewState ();
if (edit_item_style != null)
edit_item_style.TrackViewState ();
if (ControlStyleCreated)
ControlStyle.TrackViewState ();
IStateManager manager = (IStateManager) columns;
if (manager != null)
manager.TrackViewState ();
}
protected override object SaveViewState ()
{
object [] res = new object [11];
res [0] = base.SaveViewState ();
if (columns != null) {
IStateManager cm = (IStateManager) columns;
res [1] = cm.SaveViewState ();
}
if (pager_style != null)
res [2] = pager_style.SaveViewState ();
if (header_style != null)
res [3] = header_style.SaveViewState ();
if (footer_style != null)
res [4] = footer_style.SaveViewState ();
if (item_style != null)
res [5] = item_style.SaveViewState ();
if (alt_item_style != null)
res [6] = alt_item_style.SaveViewState ();
if (selected_style != null)
res [7] = selected_style.SaveViewState ();
if (edit_item_style != null)
res [8] = edit_item_style.SaveViewState ();
if (ControlStyleCreated)
res [9] = ControlStyle.SaveViewState ();
if (data_source_columns != null) {
IStateManager m = (IStateManager) data_source_columns;
res [10] = m.SaveViewState ();
}
return res;
}
protected override void LoadViewState (object savedState)
{
object [] pieces = savedState as object [];
if (pieces == null)
return;
base.LoadViewState (pieces [0]);
if (columns != null) {
IStateManager cm = (IStateManager) columns;
cm.LoadViewState (pieces [1]);
}
if (pieces [2] != null)
PagerStyle.LoadViewState (pieces [2]);
if (pieces [3] != null)
HeaderStyle.LoadViewState (pieces [3]);
if (pieces [4] != null)
FooterStyle.LoadViewState (pieces [4]);
if (pieces [5] != null)
ItemStyle.LoadViewState (pieces [5]);
if (pieces [6] != null)
AlternatingItemStyle.LoadViewState (pieces [6]);
if (pieces [7] != null)
SelectedItemStyle.LoadViewState (pieces [7]);
if (pieces [8] != null)
EditItemStyle.LoadViewState (pieces [8]);
if (pieces [9] != null)
ControlStyle.LoadViewState (pieces [8]);
if (pieces [10] != null) {
// IStateManager manager = (IStateManager) DataSourceColumns;
// manager.LoadViewState (pieces [10]);
object [] cols = (object []) pieces [10];
foreach (object o in cols) {
BoundColumn c = new BoundColumn ();
((IStateManager) c).TrackViewState ();
c.Set_Owner (this);
((IStateManager) c).LoadViewState (o);
DataSourceColumns.Add (c);
}
}
if (pieces [9] != null) {
// IStateManager manager = (IStateManager) DataSourceColumns;
// manager.LoadViewState (pieces [9]);
object [] cols = (object []) pieces [9];
foreach (object o in cols) {
BoundColumn c = new BoundColumn ();
c.Set_Owner (this);
((IStateManager) c).LoadViewState (o);
DataSourceColumns.Add (c);
}
}
}
protected override Style CreateControlStyle ()
{
TableStyle res = new TableStyle ();
res.GridLines = GridLines.Both;
res.CellSpacing = 0;
return res;
}
protected virtual void InitializeItem (DataGridItem item, DataGridColumn [] columns)
{
bool th = UseAccessibleHeader && item.ItemType == ListItemType.Header;
for (int i = 0; i < columns.Length; i++) {
TableCell cell = null;
if (th) {
cell = new TableHeaderCell ();
cell.Attributes["scope"] = "col";
} else
cell = new TableCell ();
columns [i].InitializeCell (cell, i, item.ItemType);
item.Cells.Add (cell);
}
}
protected virtual void InitializePager (DataGridItem item, int columnSpan, PagedDataSource pagedDataSource)
{
TableCell pager_cell;
if (PagerStyle.Mode == PagerMode.NextPrev)
pager_cell = InitializeNextPrevPager (item, columnSpan, pagedDataSource);
else
pager_cell = InitializeNumericPager (item, columnSpan, pagedDataSource);
item.Controls.Add (pager_cell);
}
TableCell InitializeNumericPager (DataGridItem item, int columnSpan, PagedDataSource paged)
{
TableCell res = new TableCell ();
res.ColumnSpan = columnSpan;
int button_count = PagerStyle.PageButtonCount;
int current = paged.CurrentPageIndex;
int start = current - (current % button_count);
int end = start + button_count;
if (end > paged.PageCount)
end = paged.PageCount;
if (start > 0) {
LinkButton link = new LinkButton ();
link.Text = "...";
link.CommandName = PageCommandName;
link.CommandArgument = start.ToString (Helpers.InvariantCulture);
link.CausesValidation = false;
res.Controls.Add (link);
res.Controls.Add (new LiteralControl (" "));
}
for (int i = start; i < end; i++) {
Control number = null;
string page = (i + 1).ToString (Helpers.InvariantCulture);
if (i != paged.CurrentPageIndex) {
LinkButton link = new LinkButton ();
link.Text = page;
link.CommandName = PageCommandName;
link.CommandArgument = page;
link.CausesValidation = false;
number = link;
} else {
Label pageLabel = new Label();
pageLabel.Text = page;
number = pageLabel;
}
res.Controls.Add (number);
if (i < end - 1)
res.Controls.Add (new LiteralControl (" "));
}
if (end < paged.PageCount) {
res.Controls.Add (new LiteralControl (" "));
LinkButton link = new LinkButton ();
link.Text = "...";
link.CommandName = PageCommandName;
link.CommandArgument = (end + 1).ToString (Helpers.InvariantCulture);
link.CausesValidation = false;
res.Controls.Add (link);
}
return res;
}
TableCell InitializeNextPrevPager (DataGridItem item, int columnSpan, PagedDataSource paged)
{
TableCell res = new TableCell ();
res.ColumnSpan = columnSpan;
Control prev;
Control next;
if (paged.IsFirstPage) {
Label l = new Label ();
l.Text = PagerStyle.PrevPageText;
prev = l;
} else {
LinkButton l = new DataControlLinkButton ();
l.Text = PagerStyle.PrevPageText;
l.CommandName = PageCommandName;
l.CommandArgument = PrevPageCommandArgument;
l.CausesValidation = false;
prev = l;
}
if (paged.Count > 0 && !paged.IsLastPage) {
LinkButton l = new DataControlLinkButton ();
l.Text = PagerStyle.NextPageText;
l.CommandName = PageCommandName;
l.CommandArgument = NextPageCommandArgument;
l.CausesValidation = false;
next = l;
} else {
Label l = new Label ();
l.Text = PagerStyle.NextPageText;
next = l;
}
res.Controls.Add (prev);
res.Controls.Add (new LiteralControl (" "));
res.Controls.Add (next);
return res;
}
protected virtual DataGridItem CreateItem (int itemIndex, int dataSourceIndex, ListItemType itemType)
{
DataGridItem res = new DataGridItem (itemIndex, dataSourceIndex, itemType);
return res;
}
DataGridItem CreateItem (int item_index, int data_source_index, ListItemType type, bool data_bind, object data_item, PagedDataSource paged)
{
DataGridItem res = CreateItem (item_index, data_source_index, type);
DataGridItemEventArgs args = new DataGridItemEventArgs (res);
bool no_pager = (type != ListItemType.Pager);
if (no_pager) {
InitializeItem (res, render_columns);
if (data_bind)
res.DataItem = data_item;
OnItemCreated (args);
} else {
InitializePager (res, render_columns.Length, paged);
if (pager_style != null)
res.ApplyStyle (pager_style);
OnItemCreated (args);
}
// Add before the column is bound, so that the
// value is saved in the viewstate
RenderTable.Controls.Add (res);
if (no_pager && data_bind) {
res.DataBind ();
OnItemDataBound (args);
res.DataItem = null;
}
return res;
}
sealed class NCollection : ICollection
{
int n;
public NCollection (int n)
{
this.n = n;
}
public IEnumerator GetEnumerator ()
{
for (int i = 0; i < n; i++)
yield return i;
}
public int Count {
get { return n; }
}
public bool IsSynchronized {
get { return false; }
}
public object SyncRoot {
get { return this; }
}
public void CopyTo (Array array, int index)
{
throw new NotImplementedException ("This should never be called");
}
}
protected override void CreateControlHierarchy (bool useDataSource)
{
Controls.Clear ();
RenderTable.Controls.Clear ();
Controls.Add (RenderTable);
IEnumerable data_source;
ArrayList keys = null;
if (useDataSource) {
if (IsBoundUsingDataSourceID)
data_source = GetData ();
else
data_source = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
if (data_source == null) {
Controls.Clear ();
return;
}
keys = DataKeysArray;
keys.Clear ();
} else {
int nitems = ViewState.GetInt ("Items", 0);
data_source = new NCollection (nitems);
}
paged_data_source = new PagedDataSource ();
PagedDataSource pds = paged_data_source;
pds.AllowPaging = AllowPaging;
pds.AllowCustomPaging = AllowCustomPaging;
pds.DataSource = data_source;
pds.CurrentPageIndex = CurrentPageIndex;
pds.PageSize = PageSize;
pds.VirtualCount = VirtualItemCount;
if ((pds.IsPagingEnabled) && (pds.PageCount < pds.CurrentPageIndex)) {
Controls.Clear ();
throw new HttpException ("Invalid DataGrid PageIndex");
}
ArrayList cList = CreateColumnSet (paged_data_source, useDataSource);
if (cList.Count == 0) {
Controls.Clear ();
return;
}
Page page = this.Page;
if (page != null)
page.RequiresPostBackScript ();
render_columns = new DataGridColumn [cList.Count];
for (int c = 0; c < cList.Count; c++) {
DataGridColumn col = (DataGridColumn) cList [c];
col.Set_Owner (this);
col.Initialize ();
render_columns [c] = col;
}
if (pds.IsPagingEnabled)
CreateItem (-1, -1, ListItemType.Pager, false, null, pds);
CreateItem (-1, -1, ListItemType.Header, useDataSource, null, pds);
// No indexer on PagedDataSource so we have to do
// this silly foreach and index++
if (items_list == null)
items_list = new ArrayList ();
else
items_list.Clear();
bool skip_first = false;
IEnumerator enumerator = null;
if (data_enumerator != null) {
// replaced when creating bound columns
enumerator = data_enumerator;
skip_first = true;
} else if (pds.DataSource != null)
enumerator = pds.GetEnumerator ();
else
enumerator = null;
int index = 0;
bool first = true;
string key = null;
int dataset_index = pds.FirstIndexInPage;
int selected_index = SelectedIndex;
int edit_item_index = EditItemIndex;
while (enumerator != null && (skip_first || enumerator.MoveNext ())) {
// MS does not render <table blah></table> on empty datasource.
if (first) {
first = false;
key = DataKeyField;
skip_first = false;
}
object data = enumerator.Current;
// This will throw if the DataKeyField is not there. As on MS, this
// will not be hit on an empty datasource.
// The values stored here can be used in events so that you can
// get, for example, the row that was clicked
// (data.Rows.Find (ItemsGrid.DataKeys [e.Item.ItemIndex]))
// BaseDataList will keep the array across postbacks.
if (useDataSource && key != "")
keys.Add (DataBinder.GetPropertyValue (data, key));
ListItemType type = ListItemType.Item;
if (index == edit_item_index)
type = ListItemType.EditItem;
else if (index == selected_index)
type = ListItemType.SelectedItem;
else if (index % 2 != 0)
type = ListItemType.AlternatingItem;
items_list.Add (CreateItem (index, dataset_index, type, useDataSource, data, pds));
index++;
dataset_index++;
}
CreateItem (-1, -1, ListItemType.Footer, useDataSource, null, paged_data_source);
if (pds.IsPagingEnabled) {
CreateItem (-1, -1, ListItemType.Pager, false, null, paged_data_source);
if (useDataSource)
ViewState ["Items"] = pds.IsCustomPagingEnabled ? index : pds.DataSourceCount;
} else if (useDataSource) {
ViewState ["Items"] = index;
}
}
void ApplyColumnStyle (TableCellCollection cells, ListItemType type)
{
int ncells = Math.Min (cells.Count, render_columns.Length);
if (ncells <= 0)
return;
for (int i = 0; i < ncells; i++) {
Style style = null;
TableCell cell = cells [i];
DataGridColumn column = render_columns [i];
if (!column.Visible) {
cell.Visible = false;
continue;
}
style = column.GetStyle (type);
if (style != null)
cell.MergeStyle (style);
}
}
protected override void PrepareControlHierarchy ()
{
if (!HasControls () || Controls.Count == 0)
return; // No one called CreateControlHierarchy() with DataSource != null
Table rt = render_table;
rt.CopyBaseAttributes (this);
rt.ApplyStyle (ControlStyle);
rt.Caption = Caption;
rt.CaptionAlign = CaptionAlign;
rt.Enabled = IsEnabled;
bool top_pager = true;
foreach (DataGridItem item in rt.Rows) {
switch (item.ItemType) {
case ListItemType.Item:
ApplyItemStyle (item);
break;
case ListItemType.AlternatingItem:
ApplyItemStyle (item);
break;
case ListItemType.EditItem:
item.MergeStyle (edit_item_style);
ApplyItemStyle (item);
ApplyColumnStyle (item.Cells, ListItemType.EditItem);
break;
case ListItemType.Footer:
if (!ShowFooter) {
item.Visible = false;
break;
}
if (footer_style != null)
item.MergeStyle (footer_style);
ApplyColumnStyle (item.Cells, ListItemType.Footer);
break;
case ListItemType.Header:
if (!ShowHeader) {
item.Visible = false;
break;
}
if (header_style != null)
item.MergeStyle (header_style);
ApplyColumnStyle (item.Cells, ListItemType.Header);
break;
case ListItemType.SelectedItem:
item.MergeStyle (selected_style);
ApplyItemStyle (item);
ApplyColumnStyle (item.Cells, ListItemType.SelectedItem);
break;
case ListItemType.Separator:
ApplyColumnStyle (item.Cells, ListItemType.Separator);
break;
case ListItemType.Pager:
DataGridPagerStyle ps = PagerStyle;
if (ps.Visible == false || !paged_data_source.IsPagingEnabled)
item.Visible = false;
else {
if (top_pager)
item.Visible = (ps.Position != PagerPosition.Bottom);
else
item.Visible = (ps.Position != PagerPosition.Top);
top_pager = false;
}
if (item.Visible)
item.MergeStyle (pager_style);
break;
}
}
}
void ApplyItemStyle (DataGridItem item)
{
if (item.ItemIndex % 2 != 0)
item.MergeStyle (alt_item_style);
item.MergeStyle (item_style);
ApplyColumnStyle (item.Cells, ListItemType.Item);
}
protected override bool OnBubbleEvent (object source, EventArgs e)
{
DataGridCommandEventArgs de = e as DataGridCommandEventArgs;
if (de == null)
return false;
string cn = de.CommandName;
CultureInfo inv = Helpers.InvariantCulture;
OnItemCommand (de);
if (String.Compare (cn, CancelCommandName, true, inv) == 0)
OnCancelCommand (de);
else if (String.Compare (cn, DeleteCommandName, true, inv) == 0)
OnDeleteCommand (de);
else if (String.Compare (cn, EditCommandName, true, inv) == 0)
OnEditCommand (de);
else if (String.Compare (cn, SelectCommandName, true, inv) == 0) {
SelectedIndex = de.Item.ItemIndex;
OnSelectedIndexChanged (de);
} else if (String.Compare (cn, SortCommandName, true, inv) == 0) {
DataGridSortCommandEventArgs se = new DataGridSortCommandEventArgs (de.CommandSource, de);
OnSortCommand (se);
} else if (String.Compare (cn, UpdateCommandName, true, inv) == 0)
OnUpdateCommand (de);
else if (String.Compare (cn, PageCommandName, true, inv) == 0) {
int new_index;
if (String.Compare ((string) de.CommandArgument,
NextPageCommandArgument, true, inv) == 0) {
new_index = CurrentPageIndex + 1;
} else if (String.Compare ((string) de.CommandArgument, PrevPageCommandArgument, true, inv) == 0)
new_index = CurrentPageIndex - 1;
else {
// It seems to just assume it's an int and parses, no
// checks to make sure its valid or anything.
// also it's always one less then specified, not sure
// why that is.
new_index = Int32.Parse ((string) de.CommandArgument, inv) - 1;
}
DataGridPageChangedEventArgs pc = new DataGridPageChangedEventArgs (de.CommandSource, new_index);
OnPageIndexChanged (pc);
}
return true;
}
protected virtual void OnCancelCommand (DataGridCommandEventArgs e)
{
DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [CancelCommandEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnDeleteCommand (DataGridCommandEventArgs e)
{
DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [DeleteCommandEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnEditCommand (DataGridCommandEventArgs e)
{
DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [EditCommandEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnItemCommand (DataGridCommandEventArgs e)
{
DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [ItemCommandEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnItemCreated (DataGridItemEventArgs e)
{
DataGridItemEventHandler handler = (DataGridItemEventHandler) Events [ItemCreatedEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnItemDataBound (DataGridItemEventArgs e)
{
DataGridItemEventHandler handler = (DataGridItemEventHandler) Events [ItemDataBoundEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnPageIndexChanged (DataGridPageChangedEventArgs e)
{
DataGridPageChangedEventHandler handler = (DataGridPageChangedEventHandler) Events [PageIndexChangedEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnSortCommand (DataGridSortCommandEventArgs e)
{
DataGridSortCommandEventHandler handler = (DataGridSortCommandEventHandler) Events [SortCommandEvent];
if (handler != null)
handler (this, e);
}
protected virtual void OnUpdateCommand (DataGridCommandEventArgs e)
{
DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [UpdateCommandEvent];
if (handler != null)
handler (this, e);
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridCommandEventHandler CancelCommand {
add { Events.AddHandler (CancelCommandEvent, value); }
remove { Events.RemoveHandler (CancelCommandEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridCommandEventHandler DeleteCommand {
add { Events.AddHandler (DeleteCommandEvent, value); }
remove { Events.RemoveHandler (DeleteCommandEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridCommandEventHandler EditCommand {
add { Events.AddHandler (EditCommandEvent, value); }
remove { Events.RemoveHandler (EditCommandEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridCommandEventHandler ItemCommand {
add { Events.AddHandler (ItemCommandEvent, value); }
remove { Events.RemoveHandler (ItemCommandEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridItemEventHandler ItemCreated {
add { Events.AddHandler (ItemCreatedEvent, value); }
remove { Events.RemoveHandler (ItemCreatedEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridItemEventHandler ItemDataBound {
add { Events.AddHandler (ItemDataBoundEvent, value); }
remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridPageChangedEventHandler PageIndexChanged {
add { Events.AddHandler (PageIndexChangedEvent, value); }
remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridSortCommandEventHandler SortCommand {
add { Events.AddHandler (SortCommandEvent, value); }
remove { Events.RemoveHandler (SortCommandEvent, value); }
}
[WebSysDescription ("")]
[WebCategory ("Action")]
public event DataGridCommandEventHandler UpdateCommand {
add { Events.AddHandler (UpdateCommandEvent, value); }
remove { Events.AddHandler (UpdateCommandEvent, value); }
}
}
}
|