SampleTextBox.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections.Generic;
  11. namespace Samples.CS
  12. {
  13. public class SampleTextBox : TextBox, IScriptControl
  14. {
  15. private string _highlightCssClass;
  16. private string _noHighlightCssClass;
  17. private ScriptManager sm;
  18. public string HighlightCssClass
  19. {
  20. get { return _highlightCssClass; }
  21. set { _highlightCssClass = value; }
  22. }
  23. public string NoHighlightCssClass
  24. {
  25. get { return _noHighlightCssClass; }
  26. set { _noHighlightCssClass = value; }
  27. }
  28. protected override void OnPreRender(EventArgs e)
  29. {
  30. if (!this.DesignMode)
  31. {
  32. // Test for ScriptManager and register if it exists
  33. sm = ScriptManager.GetCurrent(Page);
  34. if (sm == null)
  35. throw new HttpException("A ScriptManager control must exist on the current page.");
  36. sm.RegisterScriptControl(this);
  37. }
  38. base.OnPreRender(e);
  39. }
  40. protected override void Render(HtmlTextWriter writer)
  41. {
  42. if (!this.DesignMode)
  43. sm.RegisterScriptDescriptors(this);
  44. base.Render(writer);
  45. }
  46. protected virtual IEnumerable<ScriptReference> GetScriptReferences()
  47. {
  48. ScriptReference reference = new ScriptReference();
  49. reference.Path = ResolveClientUrl("SampleTextBox.js");
  50. return new ScriptReference[] { reference };
  51. }
  52. protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
  53. {
  54. ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Samples.SampleTextBox", this.ClientID);
  55. descriptor.AddProperty("highlightCssClass", this.HighlightCssClass);
  56. descriptor.AddProperty("nohighlightCssClass", this.NoHighlightCssClass);
  57. return new ScriptDescriptor[] { descriptor };
  58. }
  59. IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
  60. {
  61. return GetScriptReferences();
  62. }
  63. IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
  64. {
  65. return GetScriptDescriptors();
  66. }
  67. }
  68. }