DataBoundLiteralControl.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // System.Web.UI.DataBoundLiteralCOntrol.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. using System.Text;
  13. namespace System.Web.UI {
  14. [ToolboxItem(false)]
  15. public sealed class DataBoundLiteralControl : Control
  16. {
  17. private string [] staticLiterals;
  18. private string [] dataBoundLiterals;
  19. public DataBoundLiteralControl (int staticLiteralsCount,
  20. int dataBoundLiteralCount)
  21. {
  22. staticLiterals = new string [staticLiteralsCount];
  23. dataBoundLiterals = new string [dataBoundLiteralCount];
  24. PreventAutoID ();
  25. }
  26. public string Text {
  27. get {
  28. StringBuilder text = new StringBuilder ();
  29. int stLength = staticLiterals.Length;
  30. int dbLength = dataBoundLiterals.Length;
  31. int max = (stLength > dbLength) ? stLength : dbLength;
  32. for (int i = 0; i < max; i++){
  33. if (i < stLength)
  34. text.Append (staticLiterals [i]);
  35. if (i < dbLength)
  36. text.Append (dataBoundLiterals [i]);
  37. }
  38. return text.ToString ();
  39. }
  40. }
  41. protected override ControlCollection CreateControlCollection ()
  42. {
  43. return new EmptyControlCollection (this);
  44. }
  45. protected override void LoadViewState (object savedState)
  46. {
  47. if (savedState != null) {
  48. Array source = (Array) savedState;
  49. if (source.Length == dataBoundLiterals.Length)
  50. source.CopyTo (dataBoundLiterals, 0);
  51. }
  52. }
  53. protected override void Render (HtmlTextWriter output)
  54. {
  55. output.Write (Text);
  56. }
  57. protected override object SaveViewState ()
  58. {
  59. if (dataBoundLiterals.Length == 0)
  60. return null;
  61. return dataBoundLiterals;
  62. }
  63. public void SetDataBoundString (int index, string s)
  64. {
  65. dataBoundLiterals [index] = s;
  66. }
  67. public void SetStaticString (int index, string s)
  68. {
  69. staticLiterals [index] = s;
  70. }
  71. }
  72. }