Hello:
I am in the process of extending GridView and adding some new features.
I added a Single Click and Double Click, the first causes a postback and the second causes a CallBack.
The problem now is when I define an event handler for those two events, I get something as:
>> The type name 'Grid' does not exist in the type 'Grid.Grid'
The code is as follows, feel free to copy it into a new class called Grid:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Text;
6 using System.Web;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9
10 namespace Grid
11 {
12 ///*** Add the BuildArgument methods***\\\
13 ///
14 [DefaultProperty("Text")]
15 [ToolboxData("<{0}:Grid runat=server></{0}:Grid>")]
16 public class Grid : GridView
17 {
18 // Private fields to handle GetCallBackResult();
19 private bool isDoubleClick = false;
20
21 ///*** TO BE CHANGED LATER ***\\\
22 //Add property attributes ...
23
24 /// <summary>
25 /// Specify whether this property is enabled or not
26 /// </summary>
27 [Bindable(true), Category("Behavior"), DefaultValue(false), Description("To enable RowClick or not")]
28 public bool EnableRowClick
29 {
30 get
31 {
32 bool enableRowClick = false;
33 object obj = ViewState["EnableRowClick"];
34 if (obj != null)
35 {
36 enableRowClick = (bool)obj;
37 }
38 return enableRowClick;
39 }
40 set
41 {
42 ViewState["EnableRowClick"] = value;
43 }
44 }
45
46 /// <summary>
47 /// Specify whether this property is enabled or not
48 /// </summary>
49 [Bindable(true), Category("Behavior"), DefaultValue(false), Description("To enable Double Click on a row or not")]
50 public bool EnableDoubleRowClick
51 {
52 get
53 {
54 bool enableDoubleRowClick = false;
55 object obj = ViewState["EnableDoubleRowClick"];
56 if (obj != null)
57 {
58 enableDoubleRowClick = (bool)obj;
59 }
60 return enableDoubleRowClick;
61 }
62 set
63 {
64 ViewState["EnableDoubleRowClick"] = value;
65 }
66 }
67
68 /// <summary>
69 /// Specify whether this property is enabled or not
70 /// </summary>
71 [Bindable(true), Category("Appearance"), DefaultValue(typeof(Color)), Description("Mouse over color on a row")]
72 public Color MoseOverColor
73 {
74 get
75 {
76 Color mouseOverColor = Color.White;
77 object obj = ViewState["MouseOverColor"];
78 if (obj != null)
79 {
80 mouseOverColor = (Color)obj;
81 }
82 return mouseOverColor;
83 }
84 set
85 {
86 ViewState["MouseOverColor"] = value;
87 }
88 }
89
90 // Define a new event called RowClick
91 private static readonly object EventRowClick = new object();
92 public delegate void RowClickEventHandler(object sender, RowClickEventArgs e);
93 public event RowClickEventHandler RowClick
94 {
95 add
96 {
97 base.Events.AddHandler(Grid.EventRowClick, value);
98 }
99 remove
100 {
101 base.Events.RemoveHandler(Grid.EventRowClick, value);
102 }
103 }
104 protected virtual void OnRowClick(RowClickEventArgs e)
105 {
106 // Set a selection in the Grid
107 this.SelectedIndex = e.GridViewRow.RowIndex;
108
109 RowClickEventHandler handler1 = (RowClickEventHandler)base.Events[Grid.EventRowClick];
110 if (handler1 != null)
111 {
112 handler1(this, e);
113 }
114 }
115
116 // Define a new event called RowDoubleClick
117 private static readonly object EventRowDoubleClick = new object();
118 public delegate void RowDoubleClickEventHandler(object sender, RowDoubleClickEventArgs e);
119 public event RowDoubleClickEventHandler RowDoubleClick
120 {
121 add
122 {
123 base.Events.AddHandler(Grid.EventRowDoubleClick, value);
124 }
125 remove
126 {
127 base.Events.RemoveHandler(Grid.EventRowDoubleClick, value);
128 }
129 }
130 protected virtual void OnRowDoubleClick(RowDoubleClickEventArgs e)
131 {
132 // Set a selection in the Grid
133 this.SelectedIndex = e.GridViewRow.RowIndex;
134
135 RowDoubleClickEventHandler handler1 = (RowDoubleClickEventHandler)base.Events[Grid.EventRowDoubleClick];
136 if (handler1 != null)
137 {
138 handler1(this, e);
139 }
140 }
141
142 // Override the PreRender to add some JS script
143 protected override void OnPreRender(EventArgs e)
144 {
145 // Hold the script key
146 string scriptKey = "";
147
148 // Single/Double click JS routines to manage MouseOver/MouseOut
149 if (EnableRowClick || EnableDoubleRowClick)
150 {
151 // Register two Js methods to change color when the mouse hovers over the row
152 scriptKey = "ChangeColor" + this.UniqueID;
153
154 if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey))
155 {
156 StringBuilder script = new StringBuilder();
157 script.Append("var mouseOutColor;");
158
159 // Add two seperating line
160 script.Append("\r\n");
161
162 // MouseOverRowClick
163 script.Append("function MouseOverRow(source)");
164 script.Append("\r\n");
165 script.Append("{");
166 script.Append("\r\n\t");
167 script.Append("mouseOutColor = source.style.backgroundColor;");
168 script.Append("\r\n\t");
169
170 // Use the MouseOverColor Property set by the user
171 script.Append("source.style.backgroundColor = '" + MoseOverColor.ToKnownColor() + "';");
172 script.Append("\r\n");
173 script.Append("}");
174
175 // Add two seperating lines
176 script.Append("\r\n");
177 script.Append("\r\n");
178
179 // MouseOutRowClick
180 script.Append("function MouseOutRow(source)");
181 script.Append("\r\n");
182 script.Append("{");
183 script.Append("\r\n\t");
184 script.Append("source.style.backgroundColor = mouseOutColor;");
185 script.Append("\r\n");
186 script.Append("}");
187 script.Append("\r\n");
188
189 Page.ClientScript.RegisterStartupScript(typeof(Grid), scriptKey, script.ToString(), true);
190 }
191 }
192
193 // Add the CallBack Js routines needed for double click
194 if (EnableDoubleRowClick)
195 {
196 // Define callback reference.
197 String cbReference = Page.ClientScript.GetCallbackEventReference(this,"arg","ClientDoubleClick",null,false);
198 StringBuilder clientCallBackScript = new StringBuilder();
199 clientCallBackScript.Append("\r\n");
200 clientCallBackScript.Append("function DoubleClickCallBack(arg, context)");
201 clientCallBackScript.Append("\r\n");
202 clientCallBackScript.Append("{");
203 clientCallBackScript.Append("\r\n\t");
204 clientCallBackScript.AppendFormat("{0}", cbReference);
205 clientCallBackScript.Append("\r\n");
206 clientCallBackScript.Append("}");
207 clientCallBackScript.Append("\r\n");
208
209 // Add the client callback method, in this case it is a dummy function
210 clientCallBackScript.Append("function ClientDoubleClick(arg, context) {}");
211 clientCallBackScript.Append("\r\n");
212
213 // Register two Js methods to change color when the mouse hovers over the row
214 scriptKey = "DoubleClick" + this.UniqueID;
215
216 if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey))
217 {
218 // Register script blocks will perform call to the server.
219 Page.ClientScript.RegisterStartupScript(typeof(Grid), scriptKey, clientCallBackScript.ToString(), true);
220 }
221 }
222
223 // Call base PreRender
224 base.OnPreRender(e);
225 }
226
227 // Customize each row to distinguish the RowClicked event
228 protected override void PrepareControlHierarchy()
229 {
230 base.PrepareControlHierarchy();
231
232 // Hold the row/double click row
233 string argsData = string.Empty;
234
235 // if the EnableRowClick is true, then customize the rows
236 if (EnableRowClick)
237 {
238 for (int i = 0; i < (base.Rows.Count); i++)
239 {
240 argsData = "rc" + base.Rows[i].RowIndex.ToString();
241 this.Rows[i].Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, argsData));
242 //this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRowClick(this);");
243 //this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRowClick(this);");
244
245 ///*** TO BE CHANGED LATER ***\\\
246 //I will have a dropdownlist to change the cursor wanted
247 //this.Rows[i].Style.Add("cursor", "hand");
248 }
249 }
250
251 // if the EnableDoubleRowClick is true, then customize the rows
252 if (EnableDoubleRowClick)
253 {
254 for (int i = 0; i < (base.Rows.Count); i++)
255 {
256 argsData = "rdc" + base.Rows[i].RowIndex.ToString();
257 this.Rows[i].Attributes.Add("onDblClick", "DoubleClickCallBack('" + argsData + "','')");
258 //this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRowClick(this);");
259 //this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRowClick(this);");
260
261 ///*** TO BE CHANGED LATER ***\\\
262 //I will have a dropdownlist to change the cursor wanted
263 //this.Rows[i].Style.Add("cursor", "hand");
264 }
265 }
266
267 // If any of the two properties is enabled add the following
268 // customization features.
269 if (EnableRowClick || EnableDoubleRowClick)
270 {
271 for (int i = 0; i < (base.Rows.Count); i++)
272 {
273 this.Rows[i].Attributes.Add("onMouseOver", "MouseOverRow(this);");
274 this.Rows[i].Attributes.Add("onMouseOut", "MouseOutRow(this);");
275 }
276 }
277
278 }
279
280 // Override the RaisePostBackEvent to handle the Row Click
281 protected override void RaisePostBackEvent(string eventArgument)
282 {
283 if (eventArgument.StartsWith("rc"))
284 {
285 int rowClicked = Int32.Parse(eventArgument.Substring(2));
286 RowClickEventArgs rowClickEventArgs = new RowClickEventArgs(this.Rows[rowClicked]);
287 OnRowClick(rowClickEventArgs);
288 }
289 else
290 {
291 base.RaisePostBackEvent(eventArgument);
292 }
293 }
294
295 // Override the RaiseCallbackEvent to handle the row double click
296 protected override void RaiseCallbackEvent(string eventArgument)
297 {
298 if (eventArgument.StartsWith("rdc"))
299 {
300 int rowDoubleClicked = Int32.Parse(eventArgument.Substring(3));
301 RowDoubleClickEventArgs rowDoubleClickEventArgs = new RowDoubleClickEventArgs(this.Rows[rowDoubleClicked]);
302 isDoubleClick = true;
303 OnRowDoubleClick(rowDoubleClickEventArgs);
304
305 }
306 else
307 {
308 base.RaiseCallbackEvent(eventArgument);
309 }
310 }
311
312 // Override GetCallBackResult to process client call back event
313 protected override string GetCallbackResult()
314 {
315 // if Double Click Event, return nothing
316 if (isDoubleClick)
317 {
318 return string.Empty;
319 }
320
321 // return normal result to client
322 return base.GetCallbackResult();
323 }
324
325 }
326
327 // RowClickEventArgs defined
328 public class RowClickEventArgs : EventArgs
329 {
330 private GridViewRow gridViewRow;
331 public GridViewRow GridViewRow
332 {
333 get { return gridViewRow; }
334 set { gridViewRow = value; }
335 }
336
337 public RowClickEventArgs()
338 {
339 }
340
341 public RowClickEventArgs(GridViewRow gridViewRow)
342 {
343 this.gridViewRow = gridViewRow;
344 }
345 }
346
347 // RowDoubleClickEventArgs defined
348 public class RowDoubleClickEventArgs : EventArgs
349 {
350 private GridViewRow gridViewRow;
351 public GridViewRow GridViewRow
352 {
353 get { return gridViewRow; }
354 set { gridViewRow = value; }
355 }
356
357 public RowDoubleClickEventArgs()
358 {
359 }
360
361 public RowDoubleClickEventArgs(GridViewRow gridViewRow)
362 {
363 this.gridViewRow = gridViewRow;
364 }
365 }
366 }
Any idea?
Thanks.