Hacking.FAQ 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. Q: How do I get a question/answer added to this file?
  2. A: Just ask.
  3. Q: How do I write that 'style' attribute?
  4. A: The 'style' attribute of html tags is rendered in WebControl derived classes
  5. by using ControlStyle. This property is usually of type
  6. System.Web.UI.WebControls.Style. It has several properties to get/set colors,
  7. font, sizes... And also a few methods like AddAttributesToRender (which is the
  8. one you should call in your Render method to add the "style='blah'" after
  9. BeginTag.
  10. Q: Which method calls AdCreated event?
  11. A: As for any other events, you can just create a small test, attach
  12. your own method to the event and throw an exception there. The
  13. resulting page will have the stack trace either visible or in a
  14. HTML comment at the bottom.
  15. Q: Misc: using HtmlTextWriter
  16. A: Attributes added using AddAttribute will be applied to the next
  17. RenderBeginTag called. So if you want <a href="lalala">...</a>, do:
  18. writer.AddAttribute (HtmlTextWriterAttribute.Href, "lalala");
  19. writer.RenderBeginTag (HtmlTextWriterTag.A);
  20. ...
  21. writer.RenderEndTag ();
  22. Use HtmlTextWriterAttribute and HtmlTextWriterTag unless there's no value in
  23. them for the attribute/tag you're writing, in which case you can just use a
  24. string.
  25. Q: What attributes do I need on my control?
  26. A: AFAIR, there are only 2 attributes that affect how controls are parsed:
  27. * ControlBuilderAttribute: specialized parsing.
  28. * ParseChildrenAttribute:
  29. [ParseChildren (false|true)]
  30. This tells the parser to consider tags inside the ones of this control
  31. as properties (true) or as child controls (false).
  32. When set to true, there's an optional second parameter that
  33. tells the name of the property that will get the new controls
  34. added. This is useful, for example, for Table, whose children
  35. are added to Rows.
  36. * ValidationPropertyAttribute: it can take a property name, which will
  37. be the property checked when Page.Validate() is called.
  38. the design time attributes are not needed by now.
  39. For control properties, one attribute that is used sometimes is
  40. TypeConverterAttribute. If a property needs this attribute and its
  41. missing, you'll probably get a compilation error.
  42. Basically you need to put:
  43. [TypeConverter (typeof (YourConverter))]
  44. Look at Unit.cs, UnitConveter.cs for an example
  45. Q: How do I find out the attribute values?
  46. A:
  47. The easiest way to do this is to use the master info files from
  48. corcompre. Open up:
  49. http://mono.ximian.com/masterinfos/masterinfos-1.1.tar.gz
  50. And go to the System.Web file. Grep for your class; the attributes and
  51. their values will be there.
  52. Q: How do I get the source C# generated for a page/control?
  53. A:
  54. rm -rf /tmp/$USER-temp-aspnet
  55. MONO_ASPNET_NODELETE=1 xsp
  56. The file(s) will be in /tmp/$USER-temp-aspnet/XXXXX/*.cs.
  57. Q: How do I know if an attribute value is stored in ViewState? How do I know the
  58. key to be used when storing a value in ViewState?
  59. A: ViewState is a protected property, so you need to create a class deriving
  60. from the control you're testing/writing and have a method there that you can
  61. call before and after setting a property value. That way you can find out the
  62. key that should be used, the type of the value, the value... Check out the tests
  63. in Test/standalone/adrotator/adrotator-defaults.aspx for an example.
  64. Q: My control has a bunch of properties, but most of them are never set. Any
  65. trick to improve speed when getting the default value from them?
  66. A: You can use an int, [flags] enum, ... to keep track of which
  67. properties have been set so that if someone calls get_Prop, you do
  68. something like:
  69. if (has_this_property_been_set)
  70. return (type) ViewState [key];
  71. return DEFAULT;
  72. where 'type' is the property type and DEFAULT is the default value
  73. for the property.
  74. Q: How do I test my code?
  75. A: Update the System.Web_test.dll.sources with your new tests, and also
  76. add the tests to the `files-to-copy' file.
  77. Then run this:
  78. CVS=/cvs
  79. sh update-old $CVS/mcs
  80. cd $CVS/mcs
  81. make run-test-local
  82. Q: My control will be handling data coming from a postback to trigger an event.
  83. Do I have to do anything?
  84. A: Yes. You'll have to register your control for this. There's a method in Page
  85. called RegisterRequiresPostBack. You need to call that from OnPreRender. Don't
  86. call it if the control is disabled or the Page property has not been
  87. initialized. You probably want to read the next Q/A too.
  88. Q: My control implements IPostBackDataHandler.LoadPostData and I get
  89. controlname.x and controlname.y in the postback collection. How do I make the
  90. Page raise a postback event?
  91. A: You have to register your control for this, as the page will not be able to
  92. map from that controlname.x/controlname.y to an actual control. To do that,
  93. just run:
  94. Page.RegisterRequiresRaiseEvent (yourcontrolhere);
  95. In the stage where postback events are run, the page will call
  96. IPostBackEventHandler.RaisePostBackEvent. Here you'll have to validate the page
  97. (if appropiate) and raise any event based on the data you got in LoadPostData.
  98. Q: How do I compare arrays with NUnit?
  99. A: If the order of items in the array is predictable, Assert.AreEqual (array, array, string)
  100. can be used. Otherwise, you can use the following fragment in your code:
  101. --- snip --- snip --- snip --- snip ---
  102. private bool IsEqual(object[] a1, object[] a2, string assertion) {
  103. int matches;
  104. bool[] notfound;
  105. if (a1.Length != a2.Length) {
  106. if (assertion != null) {
  107. Assert.Fail(assertion + "( different length )");
  108. }
  109. return false;
  110. }
  111. matches = 0;
  112. notfound = new bool[a1.Length];
  113. for (int i = 0; i < a1.Length; i++) {
  114. for (int j = 0; j < a2.Length; j++) {
  115. if (a1[i].Equals(a2[j])) {
  116. matches++;
  117. break;
  118. }
  119. }
  120. if ((assertion != null) && (matches != i+1)) {
  121. Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
  122. }
  123. }
  124. return matches == a1.Length;
  125. }
  126. --- snip --- snip --- snip --- snip ---
  127. Q: Why are controls inside my control not being rendered?
  128. A: Your control probably has a [ParseChildren (false)], meaning that its
  129. children will not be parsed as properties. In that case, the controls are added
  130. to the Controls collection of your control. Text will be transformed into a
  131. LiteralControl. When rendering, you'll have to check if you have any children
  132. (Control.HasControls ()) and if so, either render the controls by yourself or
  133. let the base class do it. If your control has no children, just render it as you
  134. would normally do.
  135. Q: My control has a public event. Do I have to do anything special?
  136. A: Yes. System.Web.UI.Control has a 'Events' property that you have to use like
  137. this:
  138. static object event_name_blah = new object ();
  139. ....
  140. public event EventType EventName {
  141. add { Events.AddHandler (event_name_blah, value); }
  142. remove { Events.RemoveHandler (event_name_blah, value); }
  143. }
  144. ....
  145. If your control has a OnEventName that invokes EventName, you have to do:
  146. EventType deleg = (EventType) Events [event_name_blah];
  147. if (deleg != null)
  148. delege (arguments);
  149. Why? Ben said that if you don't do this, every event, even if not
  150. used, will take 4 bytes (on a 32 bit box), which is a total of 32 bits
  151. (4*8). And there are lots of events there that are not always used.
  152. Q: I hate all those casts when I use ViewState. How can I avoid those.
  153. A:
  154. Ben added nice helper methods:
  155. internal bool GetBool (string key, bool def);
  156. internal int GetInt (string key, int def);
  157. internal string GetString (string key, string def);
  158. If you have enumerations, you will have to cast them to integers to
  159. take advantage of these methods. Casting also has the advantage of
  160. using the integer view state form, which is more compact than the
  161. enumeration one.
  162. Q: ViewState does not seem to have all the values I expected. How do I get them?
  163. A: By default, IsTrackingViewState property is set to false. That might be
  164. preventing your control from storing some/all of the values. Use
  165. TrackViewState() (protected) to enable that.
  166. Q: How do I enable client side validation for validators?
  167. A: a few things are required:
  168. 1. In your machine.config, add the following line to your <httpHandlers>
  169. <add verb="*" path="WebResource.axd" type="System.Web.Handlers.AssemblyResourceLoader, System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  170. 2. Apply the patch file 'browscap.ini.diff' to your browscap.ini (this patch only
  171. works if you're using firefox, IE6, or Safari. I didn't bother with any others.):
  172. Once these two steps are completed, just going to be a page with
  173. validators that have EnableClientScript="true" (the default) should be enough.
  174. Q: My control can get a URL in one of its attributes. The value can be something
  175. like "~/blah" or "../bleh" or... How do I translate that into a URL that is
  176. includes my application directory?
  177. A: Use Control.ResolveUrl ().
  178. Q: What should i do in AddParsedSubObject
  179. A: For control like Label or Hyperlink where there is a text property,
  180. you need to handle both plain text and child controls. See the code in
  181. Label for a correct impl of this method