Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Hacking.FAQ « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 816942414c89f7a6fcb97dded3ad20f30e3fb560 (plain)
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
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