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
|
//------------------------------------------------------------------------------
// <copyright file="RepeatInfo.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.Util;
/// <devdoc>
/// <para>Defines the information used to render a list of items using
/// a <see cref='System.Web.UI.WebControls.Repeater'/>.</para>
/// </devdoc>
public sealed class RepeatInfo {
private RepeatDirection repeatDirection;
private RepeatLayout repeatLayout;
private int repeatColumns;
private string caption;
private TableCaptionAlign captionAlign;
private bool useAccessibleHeader;
private bool outerTableImplied;
private bool enableLegacyRendering;
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.RepeatInfo'/> class. This class is not
/// inheritable.</para>
/// </devdoc>
public RepeatInfo() {
repeatDirection = RepeatDirection.Vertical;
repeatLayout = RepeatLayout.Table;
repeatColumns = 0;
outerTableImplied = false;
}
public string Caption {
get {
return (caption == null) ? String.Empty : caption;
}
set {
caption = value;
}
}
public TableCaptionAlign CaptionAlign {
get {
return captionAlign;
}
set {
if ((value < TableCaptionAlign.NotSet) ||
(value > TableCaptionAlign.Right)) {
throw new ArgumentOutOfRangeException("value");
}
captionAlign = value;
}
}
// DevDiv 33149: A backward compat. switch for Everett rendering
internal bool EnableLegacyRendering {
get {
return enableLegacyRendering;
}
set {
enableLegacyRendering = value;
}
}
private bool IsListLayout {
get {
return
(RepeatLayout == RepeatLayout.UnorderedList) ||
(RepeatLayout == RepeatLayout.OrderedList);
}
}
/// <devdoc>
/// Indicates whether an outer table is implied
/// for the items.
/// </devdoc>
public bool OuterTableImplied {
get {
return outerTableImplied;
}
set {
outerTableImplied = value;
}
}
/// <devdoc>
/// <para> Indicates the column count of items.</para>
/// </devdoc>
public int RepeatColumns {
get {
return repeatColumns;
}
set {
repeatColumns = value;
}
}
/// <devdoc>
/// <para>Indicates the direction of flow of items.</para>
/// </devdoc>
public RepeatDirection RepeatDirection {
get {
return repeatDirection;
}
set {
if (value < RepeatDirection.Horizontal || value > RepeatDirection.Vertical) {
throw new ArgumentOutOfRangeException("value");
}
repeatDirection = value;
}
}
/// <devdoc>
/// Indicates the layout of items.
/// </devdoc>
public RepeatLayout RepeatLayout {
get {
return repeatLayout;
}
set {
EnumerationRangeValidationUtil.ValidateRepeatLayout(value);
repeatLayout = value;
}
}
public bool UseAccessibleHeader {
get {
return useAccessibleHeader;
}
set {
useAccessibleHeader = value;
}
}
/// <devdoc>
/// </devdoc>
private void RenderHorizontalRepeater(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl) {
Debug.Assert(outerTableImplied == false, "Cannot use outer implied table with Horizontal layout");
int itemCount = user.RepeatedItemCount;
int totalColumns = repeatColumns;
int currentColumn = 0;
if (totalColumns == 0) {
// 0 implies a complete horizontal repetition without any
// column count constraints
totalColumns = itemCount;
}
WebControl outerControl = null;
bool tableLayout = false;
switch (repeatLayout) {
case RepeatLayout.Table:
outerControl = new Table();
if (Caption.Length != 0) {
((Table)outerControl).Caption = Caption;
((Table)outerControl).CaptionAlign = CaptionAlign;
}
tableLayout = true;
break;
case RepeatLayout.Flow:
outerControl = new WebControl(HtmlTextWriterTag.Span);
break;
}
bool separators = user.HasSeparators;
// use ClientID (and not ID) since we want to render out the fully qualified client id
// even though this outer control will not be parented to the control hierarchy
outerControl.ID = baseControl.ClientID;
outerControl.CopyBaseAttributes(baseControl);
outerControl.ApplyStyle(controlStyle);
outerControl.RenderBeginTag(writer);
if (user.HasHeader) {
if (tableLayout) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// add attributes to render for TD/TH
if ((totalColumns != 1) || separators) {
int columnSpan = totalColumns;
if (separators)
columnSpan += totalColumns;
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, columnSpan.ToString(NumberFormatInfo.InvariantInfo));
}
if (useAccessibleHeader) {
writer.AddAttribute(HtmlTextWriterAttribute.Scope, "col");
}
// add style attributes to render for TD/TH
Style style = user.GetItemStyle(ListItemType.Header, -1);
if (style != null) {
style.AddAttributesToRender(writer);
}
// render begin tag
if (useAccessibleHeader) {
writer.RenderBeginTag(HtmlTextWriterTag.Th);
}
else {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
}
user.RenderItem(ListItemType.Header, -1, this, writer);
if (tableLayout) {
// render end tags TD/TH and TR
writer.RenderEndTag();
writer.RenderEndTag();
}
else {
if (totalColumns < itemCount) {
// we have multiple rows, so have a break between the header and first row.
if (EnableLegacyRendering) {
writer.WriteObsoleteBreak();
}
else {
writer.WriteBreak();
}
}
}
}
for (int i = 0; i < itemCount; i++) {
if (tableLayout && (currentColumn == 0)) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
if (tableLayout) {
// add style attributes to render for TD
Style style = user.GetItemStyle(ListItemType.Item, i);
if (style != null)
style.AddAttributesToRender(writer);
// render begin tag for TD
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
user.RenderItem(ListItemType.Item, i, this, writer);
if (tableLayout) {
// render end tag for TD
writer.RenderEndTag();
}
if (separators && (i != (itemCount - 1))) {
if (tableLayout) {
Style style = user.GetItemStyle(ListItemType.Separator, i);
if (style != null)
style.AddAttributesToRender(writer);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
user.RenderItem(ListItemType.Separator, i, this, writer);
if (tableLayout)
writer.RenderEndTag();
}
currentColumn++;
// on the last line, fill in the rest of the empty spots with <td/>s.
// If there were separators, we need twice as many plus one to accomodate for
// the last item not having a separator after it.
if (tableLayout && i == itemCount - 1) {
int unfilledColumns = totalColumns - currentColumn;
if (separators == true) {
int unfilledColumnsWithSeparators = (unfilledColumns * 2) + 1;
if (unfilledColumnsWithSeparators > unfilledColumns) {
unfilledColumns = unfilledColumnsWithSeparators;
}
}
for (int k = 0; k < unfilledColumns; k++) {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.RenderEndTag();
}
}
if ((currentColumn == totalColumns) || (i == itemCount - 1)) {
if (tableLayout) {
// End tag for TR
writer.RenderEndTag();
}
else {
// write out the <br> after rows when there are multiple rows
if (totalColumns < itemCount) {
if (EnableLegacyRendering) {
writer.WriteObsoleteBreak();
}
else {
writer.WriteBreak();
}
}
}
currentColumn = 0;
}
}
if (user.HasFooter) {
if (tableLayout) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
if ((totalColumns != 1) || separators) {
// add attributes to render for TD
int columnSpan = totalColumns;
if (separators)
columnSpan += totalColumns;
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, columnSpan.ToString(NumberFormatInfo.InvariantInfo));
}
// add style attributes to render for TD
Style style = user.GetItemStyle(ListItemType.Footer, -1);
if (style != null)
style.AddAttributesToRender(writer);
// render begin tag for TD
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
user.RenderItem(ListItemType.Footer, -1, this, writer);
if (tableLayout) {
// render end tag for TR and TD
writer.RenderEndTag();
writer.RenderEndTag();
}
}
outerControl.RenderEndTag(writer);
}
/// <devdoc>
/// <para>Renders the Repeater with the specified
/// information.</para>
/// </devdoc>
public void RenderRepeater(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl) {
if (IsListLayout) {
if (user.HasFooter || user.HasHeader || user.HasSeparators) {
throw new InvalidOperationException(SR.GetString(SR.RepeatInfo_ListLayoutDoesNotSupportHeaderFooterSeparator));
}
if (RepeatDirection != RepeatDirection.Vertical) {
throw new InvalidOperationException(SR.GetString(SR.RepeatInfo_ListLayoutOnlySupportsVerticalLayout));
}
if ((RepeatColumns != 0) && (RepeatColumns != 1)) {
throw new InvalidOperationException(SR.GetString(SR.RepeatInfo_ListLayoutDoesNotSupportMultipleColumn));
}
if (OuterTableImplied) {
throw new InvalidOperationException(SR.GetString(SR.RepeatInfo_ListLayoutDoesNotSupportImpliedOuterTable));
}
}
if (repeatDirection == RepeatDirection.Vertical) {
RenderVerticalRepeater(writer, user, controlStyle, baseControl);
}
else {
RenderHorizontalRepeater(writer, user, controlStyle, baseControl);
}
}
/// <devdoc>
/// </devdoc>
private void RenderVerticalRepeater(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl) {
int itemCount = user.RepeatedItemCount;
int totalColumns;
int totalRows;
int filledColumns;
// List Layout Constraint --> Columns = 0 or 1
if ((repeatColumns == 0) || (repeatColumns == 1)) {
// A RepeatColumns of 0 implies a completely vertical repetition in
// a single column. This is same as repeatColumns of 1.
totalColumns = 1;
filledColumns = 1;
totalRows = itemCount;
}
else {
totalColumns = repeatColumns;
totalRows = (int)((itemCount + repeatColumns - 1) / repeatColumns);
if ((totalRows == 0) && (itemCount != 0)) {
// if repeatColumns is a huge number like Int32.MaxValue, then the
// calculation above essentially reduces down to 0
totalRows = 1;
}
filledColumns = itemCount % totalColumns;
if (filledColumns == 0) {
filledColumns = totalColumns;
}
}
WebControl outerControl = null;
bool tableLayout = false;
// List Layout Constraint --> OuterTableImplied = false
// List Layout Constraint --> tableLayout = false
if (!outerTableImplied) {
switch (repeatLayout) {
case RepeatLayout.Table:
outerControl = new Table();
if (Caption.Length != 0) {
((Table)outerControl).Caption = Caption;
((Table)outerControl).CaptionAlign = CaptionAlign;
}
tableLayout = true;
break;
case RepeatLayout.Flow:
outerControl = new WebControl(HtmlTextWriterTag.Span);
break;
case RepeatLayout.UnorderedList:
outerControl = new WebControl(HtmlTextWriterTag.Ul);
break;
case RepeatLayout.OrderedList:
outerControl = new WebControl(HtmlTextWriterTag.Ol);
break;
}
}
bool separators = user.HasSeparators;
// List Layout Constraint --> separators = false
if (outerControl != null) {
// use ClientID (and not ID) since we want to render out the fully qualified client id
// even though this outer control will not be parented to the control hierarchy
outerControl.ID = baseControl.ClientID;
outerControl.CopyBaseAttributes(baseControl);
outerControl.ApplyStyle(controlStyle);
outerControl.RenderBeginTag(writer);
}
// List Layout Constraint --> HasHeader = false
if (user.HasHeader) {
if (tableLayout) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// add attributes to render for TH
if (totalColumns != 1) {
int columnSpan = totalColumns;
if (separators)
columnSpan += totalColumns;
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, columnSpan.ToString(NumberFormatInfo.InvariantInfo));
}
if (useAccessibleHeader) {
writer.AddAttribute(HtmlTextWriterAttribute.Scope, "col");
}
// add style attributes to render for TD/TH
Style style = user.GetItemStyle(ListItemType.Header, -1);
if (style != null) {
style.AddAttributesToRender(writer);
}
// render begin tag for TD/TH
if (useAccessibleHeader) {
writer.RenderBeginTag(HtmlTextWriterTag.Th);
}
else {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
}
user.RenderItem(ListItemType.Header, -1, this, writer);
if (tableLayout) {
// render end tags TD/TH and TR
writer.RenderEndTag();
writer.RenderEndTag();
}
else if (!outerTableImplied) {
if (EnableLegacyRendering) {
writer.WriteObsoleteBreak();
}
else {
writer.WriteBreak();
}
}
}
int itemCounter = 0;
for (int currentRow = 0; currentRow < totalRows; currentRow++) {
// List Layout Constraint --> tableLayout = false
if (tableLayout) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
int itemIndex = currentRow;
for (int currentCol = 0; currentCol < totalColumns; currentCol++) {
if (itemCounter >= itemCount) {
// done rendering all items, so break out of the loop now...
// we might end up here, in unfilled columns attempting to re-render items that
// have already been rendered on the next column in a prior row.
break;
}
if (currentCol != 0) {
itemIndex += totalRows;
// if the previous column (currentColumn - 1) was not a filled column, i.e.,
// it had one less item (the maximum possible), then subtract 1 from the item index.
if ((currentCol - 1) >= filledColumns) {
itemIndex--;
}
}
if (itemIndex >= itemCount)
continue;
itemCounter++;
// List Layout Constraint --> tableLayout = false
if (tableLayout) {
// add style attributes to render for TD
Style style = user.GetItemStyle(ListItemType.Item, itemIndex);
if (style != null)
style.AddAttributesToRender(writer);
// render begin tag for TD
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
if (IsListLayout) {
writer.RenderBeginTag(HtmlTextWriterTag.Li);
}
user.RenderItem(ListItemType.Item, itemIndex, this, writer);
if (IsListLayout) {
writer.RenderEndTag();
writer.WriteLine();
}
// List Layout Constraint --> tableLayout = false
if (tableLayout) {
// render end tag for TD
writer.RenderEndTag();
}
// List Layout Constraint --> separators = false
if (separators) {
if (itemIndex != (itemCount - 1)) {
if (totalColumns == 1) {
if (tableLayout) {
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
}
else {
if (!outerTableImplied) {
if (EnableLegacyRendering) {
writer.WriteObsoleteBreak();
}
else {
writer.WriteBreak();
}
}
}
}
if (tableLayout) {
Style style = user.GetItemStyle(ListItemType.Separator, itemIndex);
if (style != null)
style.AddAttributesToRender(writer);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
if (itemIndex < itemCount)
user.RenderItem(ListItemType.Separator, itemIndex, this, writer);
if (tableLayout)
writer.RenderEndTag();
}
else {
// if we're on the last filled line and separators are specified, add another <td/>
// to accomodate for the lack of a separator on the last item. If there's only one
// column, though, separators will get their own row anyways.
if (tableLayout && totalColumns > 1) {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.RenderEndTag();
}
}
}
}
// on the last line, fill in the remaining empty slots with <td/>s. We need twice as many
// if there were separators.
// List Layout Constraint --> tableLayout = false
if (tableLayout) {
if (currentRow == totalRows - 1) {
int unfilledColumns = totalColumns - filledColumns;
if (separators) {
int unfilledColumnsWithSeparators = unfilledColumns * 2;
if (unfilledColumnsWithSeparators >= unfilledColumns) {
unfilledColumns = unfilledColumnsWithSeparators;
}
}
if (unfilledColumns != 0) {
for (int i = 0; i < unfilledColumns; i++) {
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.RenderEndTag();
}
}
}
writer.RenderEndTag();
}
else {
if (((currentRow != totalRows - 1) || user.HasFooter) &&
(!outerTableImplied) && (!IsListLayout)) {
if (EnableLegacyRendering) {
writer.WriteObsoleteBreak();
}
else {
writer.WriteBreak();
}
}
}
}
// List Layout Constraint --> HasFooter = false
if (user.HasFooter) {
if (tableLayout) {
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// add attributes to render for TD
if (totalColumns != 1) {
int columnSpan = totalColumns;
if (separators)
columnSpan += totalColumns;
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, columnSpan.ToString(NumberFormatInfo.InvariantInfo));
}
// add style attributes to render for TD
Style style = user.GetItemStyle(ListItemType.Footer, -1);
if (style != null)
style.AddAttributesToRender(writer);
// render begin tag for TD
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
user.RenderItem(ListItemType.Footer, -1, this, writer);
if (tableLayout) {
// render end tag for TR and TD
writer.RenderEndTag();
writer.RenderEndTag();
}
}
if (outerControl != null)
outerControl.RenderEndTag(writer);
}
}
}
|