| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- Q: How do I get a question/answer added to this file?
-
- A: Just ask.
- Q: How do I write that 'style' attribute?
-
- A: The 'style' attribute of html tags is rendered in WebControl derived classes
- by using ControlStyle. This property is usually of type
- System.Web.UI.WebControls.Style. It has several properties to get/set colors,
- font, sizes... And also a few methods like AddAttributesToRender (which is the
- one you should call in your Render method to add the "style='blah'" after
- BeginTag.
- Q: Which method calls AdCreated event?
-
- A: As for any other events, you can just create a small test, attach
- your own method to the event and throw an exception there. The
- resulting page will have the stack trace either visible or in a
- HTML comment at the bottom.
- Q: Misc: using HtmlTextWriter
-
- A: Attributes added using AddAttribute will be applied to the next
- RenderBeginTag called. So if you want <a href="lalala">...</a>, do:
- writer.AddAttribute (HtmlTextWriterAttribute.Href, "lalala");
- writer.RenderBeginTag (HtmlTextWriterTag.A);
- ...
- writer.RenderEndTag ();
- Use HtmlTextWriterAttribute and HtmlTextWriterTag unless there's no value in
- them for the attribute/tag you're writing, in which case you can just use a
- string.
-
- Q: What attributes do I need on my control?
- A: AFAIR, there are only 2 attributes that affect how controls are parsed:
- * ControlBuilderAttribute: specialized parsing.
- * ParseChildrenAttribute:
- [ParseChildren (false|true)]
- This tells the parser to consider tags inside the ones of this control
- as properties (true) or as child controls (false).
-
- When set to true, there's an optional second parameter that
- tells the name of the property that will get the new controls
- added. This is useful, for example, for Table, whose children
- are added to Rows.
- * ValidationPropertyAttribute: it can take a property name, which will
- be the property checked when Page.Validate() is called.
- the design time attributes are not needed by now.
- For control properties, one attribute that is used sometimes is
- TypeConverterAttribute. If a property needs this attribute and its
- missing, you'll probably get a compilation error.
- Basically you need to put:
- [TypeConverter (typeof (YourConverter))]
- Look at Unit.cs, UnitConveter.cs for an example
- Q: How do I find out the attribute values?
- A:
- The easiest way to do this is to use the master info files from
- corcompre. Open up:
- http://mono.ximian.com/masterinfos/masterinfos-1.1.tar.gz
- And go to the System.Web file. Grep for your class; the attributes and
- their values will be there.
-
- Q: How do I get the source C# generated for a page/control?
- A:
- rm -rf /tmp/$USER-temp-aspnet
- MONO_ASPNET_NODELETE=1 xsp
- The file(s) will be in /tmp/$USER-temp-aspnet/XXXXX/*.cs.
- Q: How do I know if an attribute value is stored in ViewState? How do I know the
- key to be used when storing a value in ViewState?
- A: ViewState is a protected property, so you need to create a class deriving
- from the control you're testing/writing and have a method there that you can
- call before and after setting a property value. That way you can find out the
- key that should be used, the type of the value, the value... Check out the tests
- in Test/standalone/adrotator/adrotator-defaults.aspx for an example.
-
- Q: My control has a bunch of properties, but most of them are never set. Any
- trick to improve speed when getting the default value from them?
- A: You can use an int, [flags] enum, ... to keep track of which
- properties have been set so that if someone calls get_Prop, you do
- something like:
- if (has_this_property_been_set)
- return (type) ViewState [key];
- return DEFAULT;
- where 'type' is the property type and DEFAULT is the default value
- for the property.
- Q: How do I test my code?
- A: Update the System.Web_test.dll.sources with your new tests, and also
- add the tests to the `files-to-copy' file.
- Then run this:
- CVS=/cvs
- sh update-old $CVS/mcs
- cd $CVS/mcs
- make run-test-local
- Q: My control will be handling data coming from a postback to trigger an event.
- Do I have to do anything?
- A: Yes. You'll have to register your control for this. There's a method in Page
- called RegisterRequiresPostBack. You need to call that from OnPreRender. Don't
- call it if the control is disabled or the Page property has not been
- initialized. You probably want to read the next Q/A too.
- Q: My control implements IPostBackDataHandler.LoadPostData and I get
- controlname.x and controlname.y in the postback collection. How do I make the
- Page raise a postback event?
- A: You have to register your control for this, as the page will not be able to
- map from that controlname.x/controlname.y to an actual control. To do that,
- just run:
- Page.RegisterRequiresRaiseEvent (yourcontrolhere);
- In the stage where postback events are run, the page will call
- IPostBackEventHandler.RaisePostBackEvent. Here you'll have to validate the page
- (if appropiate) and raise any event based on the data you got in LoadPostData.
- Q: How do I compare arrays with NUnit?
- A: If the order of items in the array is predictable, Assert.AreEqual (array, array, string)
- can be used. Otherwise, you can use the following fragment in your code:
- --- snip --- snip --- snip --- snip ---
- private bool IsEqual(object[] a1, object[] a2, string assertion) {
- int matches;
- bool[] notfound;
- if (a1.Length != a2.Length) {
- if (assertion != null) {
- Assert.Fail(assertion + "( different length )");
- }
- return false;
- }
- matches = 0;
- notfound = new bool[a1.Length];
- for (int i = 0; i < a1.Length; i++) {
- for (int j = 0; j < a2.Length; j++) {
- if (a1[i].Equals(a2[j])) {
- matches++;
- break;
- }
- }
- if ((assertion != null) && (matches != i+1)) {
- Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
- }
- }
- return matches == a1.Length;
- }
- --- snip --- snip --- snip --- snip ---
- Q: Why are controls inside my control not being rendered?
- A: Your control probably has a [ParseChildren (false)], meaning that its
- children will not be parsed as properties. In that case, the controls are added
- to the Controls collection of your control. Text will be transformed into a
- LiteralControl. When rendering, you'll have to check if you have any children
- (Control.HasControls ()) and if so, either render the controls by yourself or
- let the base class do it. If your control has no children, just render it as you
- would normally do.
- Q: My control has a public event. Do I have to do anything special?
- A: Yes. System.Web.UI.Control has a 'Events' property that you have to use like
- this:
- static object event_name_blah = new object ();
- ....
- public event EventType EventName {
- add { Events.AddHandler (event_name_blah, value); }
- remove { Events.RemoveHandler (event_name_blah, value); }
- }
- ....
- If your control has a OnEventName that invokes EventName, you have to do:
- EventType deleg = (EventType) Events [event_name_blah];
- if (deleg != null)
- delege (arguments);
- Why? Ben said that if you don't do this, every event, even if not
- used, will take 4 bytes (on a 32 bit box), which is a total of 32 bits
- (4*8). And there are lots of events there that are not always used.
- Q: I hate all those casts when I use ViewState. How can I avoid those.
- A:
- Ben added nice helper methods:
- internal bool GetBool (string key, bool def);
- internal int GetInt (string key, int def);
- internal string GetString (string key, string def);
- If you have enumerations, you will have to cast them to integers to
- take advantage of these methods. Casting also has the advantage of
- using the integer view state form, which is more compact than the
- enumeration one.
- Q: ViewState does not seem to have all the values I expected. How do I get them?
- A: By default, IsTrackingViewState property is set to false. That might be
- preventing your control from storing some/all of the values. Use
- TrackViewState() (protected) to enable that.
- Q: How do I enable client side validation for validators?
- A: a few things are required:
-
- 1. In your machine.config, add the following line to your <httpHandlers>
- <add verb="*" path="WebResource.axd" type="System.Web.Handlers.AssemblyResourceLoader, System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
- 2. Apply the patch file 'browscap.ini.diff' to your browscap.ini (this patch only
- works if you're using firefox, IE6, or Safari. I didn't bother with any others.):
- Once these two steps are completed, just going to be a page with
- validators that have EnableClientScript="true" (the default) should be enough.
- Q: My control can get a URL in one of its attributes. The value can be something
- like "~/blah" or "../bleh" or... How do I translate that into a URL that is
- includes my application directory?
- A: Use Control.ResolveUrl ().
- Q: What should i do in AddParsedSubObject
- A: For control like Label or Hyperlink where there is a text property,
- you need to handle both plain text and child controls. See the code in
- Label for a correct impl of this method
|