[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

01/05/2008 at 09:51AM PST, ID: 23060965
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Can someone help me combine two cs files.

Asked by mathieu_cupryk in C# Programming Language, Programming for ASP.NET

Tags: display, enableclientscript, month

I have one cs. DateChangedEventArgs.cs

Here is the code:
// Active DateTime v1.1
// Copyright (c) 2002 Active Up SPRL - http://www.activeup.com
//
// LIMITATION OF LIABILITY
// The software is supplied "as is". Active Up cannot be held liable to you
// for any direct or indirect damage, or for any loss of income, loss of
// profits, operating losses or any costs incurred whatsoever. The software
// has been designed with care, but Active Up does not guarantee that it is
// free of errors.

using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
[assembly: CLSCompliantAttribute(true)]

namespace ActiveUp.WebControls
{
      /// <summary>
      /// This server control allows users to select a valid date and/or time trough selectors.
      /// </summary>
      [ToolboxData("<{0}:ActiveDateTime runat=server></{0}:ActiveDateTime>")]
      public class ActiveDateTime : System.Web.UI.Control, IPostBackDataHandler
      {
            private const string ActiveDateTimeScriptKey = "ActiveDateTimeIncludeScript";
            private bool _renderUplevel, _autoAdjust, _alertEnabled, _monthNamesDisabled;
            private int _minYear, _maxYear;
            private System.Web.UI.WebControls.Style _dateStyle, _timeStyle, _baseStyle;
            private string _format, _alertText;
            private string[] _months;
            private DateTime _oldDate;

            /// <summary>
            /// The default constructor.
            /// </summary>
            public ActiveDateTime()
            {
                  _months = new string[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
                  _format = "MONTH;/;DAY;/;YEAR;-;HOUR;:;MINUTE";
                  _minYear = DateTime.Now.Year - 100;
                  _maxYear = DateTime.Now.Year - 18;
                  _dateStyle = new System.Web.UI.WebControls.Style();
                  _timeStyle = new System.Web.UI.WebControls.Style();
                  _baseStyle = new System.Web.UI.WebControls.Style();
                  _alertText = "The date you selected is not valid and has been reset to the last day in the month.";
            }

            /// <summary>
            /// Register the client side validation script in the ASP page.
            /// </summary>
            protected void RegisterValidatorScript()
            {
                  // Register the script block is not allready done.
                  if (!Page.IsClientScriptBlockRegistered(ActiveDateTimeScriptKey))
                  {
                        string includeScript = @"
                        <script language='javascript'>
                        // Active DateTime v1.0 Server Control for ASP.NET
                        // Copyright (c) 2002 Active Up - http://www.activeup.com/?r=adt11

                        // Check if the specified date is valid.
                        function isValid(year, month, day)
                        {
                              syear = document.getElementById(year);
                              smonth = document.getElementById(month);
                              sday = document.getElementById(day);

                              maximum = maxDays(syear[syear.selectedIndex].value, smonth[smonth.selectedIndex].value, sday[sday.selectedIndex].value);
                              
                              if (maximum <  sday[sday.selectedIndex].value)
                              {
                                    sday[maximum-1].selected = true;";
                        
                        if (_alertEnabled)
                        {
                              includeScript += @"
                                    alert('" + _alertText + "');";
                        }

                        includeScript += @"
                              }
                        }

                        // Returns the maximum day number in the specified month. Use leap year calculation.
                        function maxDays(year, month, day)
                        {
                              if (month == 2)
                                    return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28;
                              else
                                    return (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
                        }
                        </script>
                        ";

                        // Create client script block.
                        Page.RegisterClientScriptBlock(ActiveDateTimeScriptKey, includeScript);  
                  }
            }                  

            /// <summary>
            /// Raises the PreRender event.
            /// </summary>
            /// <remarks>This method notifies the server control to perform any necessary prerendering steps prior to saving view state and rendering content.</remarks>
            /// <param name="e">An <see cref="EventArgs"/> object that contains the event data. </param>
            protected override void OnPreRender(EventArgs e)
            {
                  base.OnPreRender(e);
            
                  // Determine if you can or want to use the client side validation.
                  _renderUplevel = DetermineRenderUplevel();

                  if (_renderUplevel)
                  {
                        // Register the client side validation script.
                        RegisterValidatorScript();
                  }
            }

            /// <summary>
            /// Determine if we need to register the client side validation script.
            /// </summary>
            /// <returns>true if validation is needed; otherwise false.</returns>
            protected virtual bool DetermineRenderUplevel()
            {
                  // Must be on a page.
                  Page page = Page;
                  if (page == null || page.Request == null)
                        return false;

                  // Check whether the client browser has turned off scripting and check
                  // browser capabilities. Active DateTime needs the W3C DOM level 1 for
                  // control manipulation and at least ECMAScript 1.2.
                  return (EnableClientScript
                        //&& page.Request.Browser.W3CDomVersion.Major >= 1
                        && ((page.Request.Browser.Browser.ToUpper().IndexOf("IE") > -1 && page.Request.Browser.MajorVersion >= 4)
                              || (page.Request.Browser.Browser.ToUpper().IndexOf("NETSCAPE") > -1 && page.Request.Browser.MajorVersion >= 6)
                              || (page.Request.Browser.Browser.ToUpper().IndexOf("OPERA") > -1 && page.Request.Browser.MajorVersion >= 3))
                        && page.Request.Browser.EcmaScriptVersion.CompareTo(new Version(1, 2)) >= 0
                        && this._format.ToUpper().IndexOf("DAY") > -1);
            }

            /// <summary>
            /// Sends server control content to a provided HtmlTextWriter object, which writes the content to be rendered on the client.
            /// </summary>
            /// <param name="output">The HtmlTextWriter object that receives the server control content.</param>
            protected override void Render(HtmlTextWriter output)
            {
                  base.Render(output);

                  // A workaround before finding a solution
                  output.AddAttribute(HtmlTextWriterAttribute.Type,"Hidden");
                  output.AddAttribute(HtmlTextWriterAttribute.Name,UniqueID);
                  output.AddAttribute(HtmlTextWriterAttribute.Value,Date.Ticks.ToString());
                  output.RenderBeginTag(HtmlTextWriterTag.Input);
                  output.RenderEndTag();

                  // Check each element separated by semicolon char in the Format string.
                  // If the element is the string representation of a specific date part,
                  // the code render the selector. If not, the element is rendered as
                  // text to the HtmlTextWriter.
                  foreach(string element in Format.Split(';'))
                  {
                        switch (element.ToUpper())
                        {
                              case "DAY":
                                    WriteSelector(output, "_day", DateStyle, "isValid('" + UniqueID + "_year', '" + UniqueID + "_month', '" + UniqueID + "_day')", 1, 31, 2, Date.Day);
                                    break;
                              case "MONTH":
                                    WriteSelector(output, "_month", DateStyle, "isValid('" + UniqueID + "_year', '" + UniqueID + "_month', '" + UniqueID + "_day')", 1, 12, 2, Date.Month);
                                    break;
                              case "YEAR":
                                    WriteSelector(output, "_year", DateStyle, "isValid('" + UniqueID + "_year', '" + UniqueID + "_month', '" + UniqueID + "_day')", _minYear, _maxYear, 4, Date.Year-18);
                                    break;
                              case "HOUR":
                                    WriteSelector(output, "_hour", TimeStyle, null, 0, 23, 2, Date.Hour);
                                    break;
                              case "MINUTE":
                                    WriteSelector(output, "_minute", TimeStyle, null, 0, 59, 2, Date.Minute);
                                    break;
                              case "SECOND":
                                    WriteSelector(output, "_second", TimeStyle, null, 0, 59, 2, Date.Second);
                                    break;
                              case "MILLISECOND":
                                    WriteSelector(output, "_millisecond", TimeStyle, null, 0, 999, 3, Date.Millisecond);
                                    break;
                              default:
                                    output.Write(element);
                                    break;
                        }
                  }
            }

            /// <summary>
            /// This method create a selector based on the parameters.
            /// </summary>
            /// <param name="output">The HtmlTextWriter to write.</param>
            /// <param name="suffix">The suffix to use to identify the selector with the LoadPostData method.</param>
            /// <param name="style">The style class to use.</param>
            /// <param name="onchange">The value of the OnChange attribute of the selector to use with the client side validator.</param>
            /// <param name="min">The minimum value of the selector.</param>
            /// <param name="max">The maximum value of the selector.</param>
            /// <param name="padding">The number of chars to use with padding.</param>
            /// <param name="selectedValue">The selected value.</param>
            private void WriteSelector(HtmlTextWriter output, string suffix, System.Web.UI.WebControls.Style style, string onchange, int min, int max, int padding, int selectedValue)
            {
                  // Some variable we will use
                  int index;

                  // Check if the actual year value can be displayed in the selector
                  if (selectedValue < min || selectedValue > max)
                        throw new Exception("The year value (" + Date.Year.ToString() + ") of the Date property is greater than the maximum (" + max.ToString() + ") or less than the minimum (" + min.ToString() + "). Please adjust values or set AutoAdjust property to true.");

                  // Write the selector
                  output.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID + suffix);
                                    
                  // Render the validation action code only if needed
                  if(_renderUplevel && onchange != null)
                        output.AddAttribute(HtmlTextWriterAttribute.Onchange, onchange);
                  
                  // Add the styles to the selector after the merge with the base style.
                  style.MergeWith(_baseStyle);
                  style.AddAttributesToRender(output);

                  output.RenderBeginTag(HtmlTextWriterTag.Select);
                  
                  // Write the option tags
                  for(index=min;index<=max;index++)
                  {
                        output.AddAttribute(HtmlTextWriterAttribute.Value, index.ToString());

                        // Set the selected value
                        if (index == selectedValue)
                              output.AddAttribute(HtmlTextWriterAttribute.Selected, null);

                        output.RenderBeginTag(HtmlTextWriterTag.Option);
                        if (suffix == "_month" && !_monthNamesDisabled)
                              output.InnerWriter.Write(_months[index-1]);
                        else
                              output.InnerWriter.Write(index.ToString().PadLeft(padding, '0'));
                        output.RenderEndTag();
                  }

                  // Write the selector end tag
                  output.RenderEndTag();
            }

            /// <summary>
            /// Processes post back data for an the server control.
            /// </summary>
            /// <param name="postDataKey">The key identifier for the control.</param>
            /// <param name="postCollection">The collection of all incoming name values.</param>
            /// <returns>true if the server control's state changes as a result of the post back; otherwise false.</returns>
            public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
            {
                  int day, month, year, hour, minute, second, millisecond;

                  _oldDate = Date;

                  day = Convert.ToInt16(postCollection[UniqueID + "_day"]);
                  month = Convert.ToInt16(postCollection[UniqueID + "_month"]);
                  year = Convert.ToInt16(postCollection[UniqueID + "_year"]);
                  hour = Convert.ToInt16(postCollection[UniqueID + "_hour"]);
                  minute = Convert.ToInt16(postCollection[UniqueID + "_minute"]);
                  second = Convert.ToInt16(postCollection[UniqueID + "_second"]);
                  millisecond = Convert.ToInt16(postCollection[UniqueID + "_millisecond"]);

                  try
                  {
                        Date = new DateTime(year == 0 ? DateTime.Now.Year : year, month == 0 ? DateTime.Now.Month : month, day == 0 ? DateTime.Now.Day : day, hour, minute, second, millisecond);
                  }
                  catch
                  {
                        throw new Exception("The selectors represent an invalid date. Please use client side validation to prevent this.");
                  }

                  if (!Date.Equals(_oldDate))
                        return true;

                  return false;
            }

            /// <summary>
            /// Signals the server control object to notify the ASP.NET application that the state of the control has changed.
            /// </summary>
            public virtual void RaisePostDataChangedEvent()
            {
                  OnDateChanged(new DateChangedEventArgs(_oldDate, Date));
            }

            /// <summary>
            /// Occurs when the Date property value changes.
            /// </summary>
            public event DateChangedEventHandler DateChanged;

            /// <summary>
            /// The DateChanged event handler.
            /// </summary>
            public delegate void DateChangedEventHandler(object sender, DateChangedEventArgs e);

            /// <summary>
            /// Raises the DateChanged event.
            /// </summary>
            /// <param name="e">An <see cref="DateChangedEventArgs"/> that contains the event data.</param>
            protected virtual void OnDateChanged(DateChangedEventArgs e)
            {
                  // Check if someone use our event.
                  if (DateChanged != null)
                        DateChanged(this,e);
            }

            /// <summary>
            /// Gets or sets a value indicating whether client-side validation is enabled.
            /// </summary>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets or sets a value indicating whether client-side validation is enabled.")]
            public bool EnableClientScript
            {
                  get
                  {
                        object o = ViewState["EnableClientScript"];
                        return((o == null) ? true : (bool)o);
                  }
                  set
                  {
                        ViewState["EnableClientScript"] = value;
                  }
            }

            /// <summary>
            /// Gets or sets the date of the control.
            /// </summary>
            [Bindable(true),
            Category("Data"),
            Description("Gets or sets the date of the control.")]
            public DateTime Date
            {
                  get
                  {
                        if (ViewState["_date"] == null)
                              ViewState["_date"] = DateTime.Now;
                        return (DateTime)ViewState["_date"];
                  }
                  set
                  {
                        ViewState["_date"] = value;
                  }
            }

            /// <summary>
            /// Gets or sets the minimum year to display in the year selector.
            /// </summary>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets or sets the minimum year to display in the year selector.")]
            public int MinYear
            {
                  get
                  {
                        return _minYear;
                  }
                  set
                  {
                        _minYear = value;
                  }
            }

            /// <summary>
            /// Gets or sets the maximum year to display in the year selector.
            /// </summary>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets or sets the maximum year to display in the year selector.")]
            public int MaxYear
            {
                  get
                  {
                        return _maxYear;
                  }
                  set
                  {
                        _maxYear = value;
                  }
            }

            /// <summary>
            /// Lets you specify if you want the control to show an alert popup when a invalid date is selected.
            /// </summary>
            public bool AlertEnabled
            {
                  get
                  {
                        return _alertEnabled;
                  }
                  set
                  {
                        _alertEnabled = value;
                  }
            }

            /// <summary>
            /// Set to true is we want to auto adjust the maximum and/or minimum year with the data.
            /// </summary>
            /// <remarks>Setting a high value to <see cref="MaxYear"/> or setting a low value in <see cref="MinYear"/> can produce performance problem.
            /// For each year, more than 20 bytes are added to the browser HTML output.
            /// By setting this property to true, you can prevent from exception throws without having to set very high maximum year or very low minimum year.
            /// If the maximum year value is less than the actual date year value, the maximum year will be adjusted to the actual date year. Same for the minimum year.</remarks>
            [Bindable(true),
            Category("Appearance"),
            Description("Set to true is we want to auto adjust the maximum and/or minimum year with the data.")]
            public bool AutoAdjust
            {
                  get
                  {
                        return _autoAdjust;
                  }
                  set
                  {
                        _autoAdjust = value;
                  }
            }

            /// <summary>
            /// Gets the style properties of the date and time selectors base.
            /// </summary>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets the style properties of the date and time selectors base.")]
            public System.Web.UI.WebControls.Style BaseStyle
            {
                  get
                  {
                        return _baseStyle;
                  }
                  set
                  {
                        _baseStyle = value;
                  }
            }

            /// <summary>
            /// Gets the style properties of the date selectors.
            /// </summary>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets the style properties of the date selectors.")]
            public System.Web.UI.WebControls.Style DateStyle
            {
                  get
                  {
                        return _dateStyle;
                  }
                  set
                  {
                        _dateStyle = value;
                  }
            }

            /// <summary>
            /// Gets or sets the month names.
            /// </summary>
            /// <remarks>
            /// <code>
            /// // This line will replace the january month name by 'Janvier'
            /// MyDate.Months[0] = "Janvier";
            ///
            /// // This line will replace the december month name by 'Décembre (noel)'
            /// MyDate.Months[11] = "Décembre (noel)";
            /// </code>
            /// </remarks>
            public string[] Months
            {
                  get
                  {
                        return _months;
                  }
                  set
                  {
                        _months = value;
                  }
            }

            /// <summary>
            /// Sets the month names.
            /// </summary>
            /// <remarks>
            /// This array of string contains the month names. You can set your own month names to match with your culture. If your website is in french, you will prefer use Février in replacment of February.
            /// By default, the months are in english. If the <see cref="MonthNamesDisabled"/> property is set to true, numbers will replace month names.
            /// <code>
            /// // Please verify that MonthNamesDisabled property is not set to true
            /// &lt;AU:ActiveDateTime runat="server" id="CompleteSelector" Format="day;/;month;/;year; ;hour;:;minute;:;second;:;millisecond" SetMonthNames="Janvier,F&amp;eacute;vrier,Mars,Avril,Mai,Juin,Juillet,Ao&amp;ucirc;t,Septembre,Octobre,Novembre,D&amp;eacute;cembre"&gt;&lt;/AU:ActiveDateTime&gt;
            /// </code>
            /// </remarks>
            public string SetMonthNames
            {
                  set
                  {
                        string[] months = value.Split(',');
                        int index;

                        for(index=0;index<months.Length;index++)
                        {
                              _months[index] = months[index];
                        }
                  }
            }
      
            /// <summary>
            /// Specify if you want to display month names or month numbers. False by default.
            /// </summary>
            public bool MonthNamesDisabled
            {
                  get
                  {
                        return _monthNamesDisabled;
                  }
                  set
                  {
                        _monthNamesDisabled = value;
                  }
            }

            /// <summary>
            /// Gets the style properties of the time selectors.
            /// </summary>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets the style properties of the time selectors.")]
            public System.Web.UI.WebControls.Style TimeStyle
            {
                  get
                  {
                        return _timeStyle;
                  }
                  set
                  {
                        _timeStyle = value;
                  }
            }

            /// <summary>
            /// Gets or sets the alert text to use when a invalid date is selected.
            /// </summary>
            public string AlertText
            {
                  get
                  {
                        return _alertText;
                  }
                  set
                  {
                        _alertText = value;
                  }
            }

            /// <summary>
            /// Gets or sets the format to use to render the selectors.
            /// </summary>
            /// <remarks>You can specify the display layout using the <c>format specifiers</c> like <c>"hour"</c> or <c>"month"</c>.<br></br>
            /// Fields must be separated by ; (semicolon) char. Non reconized fields are send to the HtmlTextWriter as literal text.<br></br>
            /// Here are some examples (<c>[</c> and <c>]</c> represent a selector:<br></br><br></br>
            ///      <c>"month;/;day;/;year"</c> will display<br></br><c>[MONTH]/[DAY]/[YEAR]</c>.<br></br><br></br>
            ///      <c>"hour;:;minute"</c> will display<br></br><c>[HOUR]:[MINUTE]</c>.<br></br><br></br>
            ///      <c>"Date : ;day;/;month;/;year; Time : ;hour;:;minute;:;second"</c> will display<br></br><c>Date : [DAY]/[MONTH]/[YEAR] Time : [HOUR]:[MINUTE]:[SECOND]</c>.<br></br><br></br>
            ///      <table><tr><td bgcolor="#F0F0F0">Format Specifier</td><td bgcolor="#F0F0F0">Name</td></tr>
            ///      <tr><td><b>day</b></td><td>The day part (1 to 31).</td></tr>
            ///      <tr><td><b>month</b></td><td>The month part (1 to 12).</td></tr>
            ///      <tr><td><b>year</b></td><td>The year part (variable).</td></tr>
            ///      <tr><td><b>hour</b></td><td>The hour part (0 to 23).</td></tr>
            ///      <tr><td><b>minute</b></td><td>The minute part (0 to 59).</td></tr>
            ///      <tr><td><b>second</b></td><td>The second part (0 to 59).</td></tr>
            ///      <tr><td><b>millisecond</b></td><td>The millisecond part (0 to 999).</td></tr>
            ///      <tr><td><i>other literal</i></td><td>Rendered as literal text.</td></tr>
            ///      </table>
            /// </remarks>
            [Bindable(true),
            Category("Appearance"),
            Description("Gets or sets the format to use to render the selectors.")]
            public string Format
            {
                  get
                  {
                        return _format;
                  }
                  set
                  {
                        _format = value;
                  }
            }
      }
}

I need to incorporate it in anotehr file called ActiveDateTime.cs

See attached file.

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:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
// Active DateTime v1.1
// Copyright (c) 2002 Active Up SPRL - http://www.activeup.com
//
// LIMITATION OF LIABILITY
// The software is supplied "as is". Active Up cannot be held liable to you
// for any direct or indirect damage, or for any loss of income, loss of
// profits, operating losses or any costs incurred whatsoever. The software
// has been designed with care, but Active Up does not guarantee that it is
// free of errors.
 
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
[assembly: CLSCompliantAttribute(true)]
 
namespace ActiveUp.WebControls
{
	/// <summary>
	/// This server control allows users to select a valid date and/or time trough selectors.
	/// </summary>
	[ToolboxData("<{0}:ActiveDateTime runat=server></{0}:ActiveDateTime>")]
	public class ActiveDateTime : System.Web.UI.Control, IPostBackDataHandler
	{
		private const string ActiveDateTimeScriptKey = "ActiveDateTimeIncludeScript";
		private bool _renderUplevel, _autoAdjust, _alertEnabled, _monthNamesDisabled;
		private int _minYear, _maxYear;
		private System.Web.UI.WebControls.Style _dateStyle, _timeStyle, _baseStyle;
		private string _format, _alertText;
		private string[] _months;
		private DateTime _oldDate;
 
		/// <summary>
		/// The default constructor.
		/// </summary>
		public ActiveDateTime()
		{
			_months = new string[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
			_format = "MONTH;/;DAY;/;YEAR;-;HOUR;:;MINUTE";
			_minYear = DateTime.Now.Year - 100;
			_maxYear = DateTime.Now.Year - 18;
			_dateStyle = new System.Web.UI.WebControls.Style();
			_timeStyle = new System.Web.UI.WebControls.Style();
			_baseStyle = new System.Web.UI.WebControls.Style();
			_alertText = "The date you selected is not valid and has been reset to the last day in the month.";
		}
 
		/// <summary>
		/// Register the client side validation script in the ASP page.
		/// </summary>
		protected void RegisterValidatorScript() 
		{
			// Register the script block is not allready done.
			if (!Page.IsClientScriptBlockRegistered(ActiveDateTimeScriptKey)) 
			{
				string includeScript = @"
				<script language='javascript'>
				// Active DateTime v1.0 Server Control for ASP.NET
				// Copyright (c) 2002 Active Up - http://www.activeup.com/?r=adt11
 
				// Check if the specified date is valid.
				function isValid(year, month, day)
				{
					syear = document.getElementById(year);
					smonth = document.getElementById(month);
					sday = document.getElementById(day);
 
					maximum = maxDays(syear[syear.selectedIndex].value, smonth[smonth.selectedIndex].value, sday[sday.selectedIndex].value);
					
					if (maximum <  sday[sday.selectedIndex].value)
					{
						sday[maximum-1].selected = true;";
				
				if (_alertEnabled)
				{
					includeScript += @"
						alert('" + _alertText + "');";
				}
 
				includeScript += @"
					}
				}
 
				// Returns the maximum day number in the specified month. Use leap year calculation.
				function maxDays(year, month, day)
				{
					if (month == 2)
						return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28;
					else
						return (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
				}
				</script>
				";
 
				// Create client script block.
				Page.RegisterClientScriptBlock(ActiveDateTimeScriptKey, includeScript);   
			}
		}                  
 
		/// <summary>
		/// Raises the PreRender event.
		/// </summary>
		/// <remarks>This method notifies the server control to perform any necessary prerendering steps prior to saving view state and rendering content.</remarks>
		/// <param name="e">An <see cref="EventArgs"/> object that contains the event data. </param>
		protected override void OnPreRender(EventArgs e) 
		{
			base.OnPreRender(e);
		
			// Determine if you can or want to use the client side validation.
			_renderUplevel = DetermineRenderUplevel();
 
			if (_renderUplevel) 
			{
				// Register the client side validation script.
				RegisterValidatorScript();
			}
		}
 
		/// <summary>
		/// Determine if we need to register the client side validation script.
		/// </summary>
		/// <returns>true if validation is needed; otherwise false.</returns>
		protected virtual bool DetermineRenderUplevel() 
		{
			// Must be on a page.
			Page page = Page;
			if (page == null || page.Request == null) 
				return false;
 
			// Check whether the client browser has turned off scripting and check
			// browser capabilities. Active DateTime needs the W3C DOM level 1 for
			// control manipulation and at least ECMAScript 1.2.
			return (EnableClientScript 
				//&& page.Request.Browser.W3CDomVersion.Major >= 1
				&& ((page.Request.Browser.Browser.ToUpper().IndexOf("IE") > -1 && page.Request.Browser.MajorVersion >= 4)
					|| (page.Request.Browser.Browser.ToUpper().IndexOf("NETSCAPE") > -1 && page.Request.Browser.MajorVersion >= 6)
					|| (page.Request.Browser.Browser.ToUpper().IndexOf("OPERA") > -1 && page.Request.Browser.MajorVersion >= 3))
				&& page.Request.Browser.EcmaScriptVersion.CompareTo(new Version(1, 2)) >= 0
				&& this._format.ToUpper().IndexOf("DAY") > -1);
		}
 
		/// <summary>
		/// Sends server control content to a provided HtmlTextWriter object, which writes the content to be rendered on the client.
		/// </summary>
		/// <param name="output">The HtmlTextWriter object that receives the server control content.</param>
		protected override void Render(HtmlTextWriter output)
		{
			base.Render(output);
 
			// A workaround before finding a solution
			output.AddAttribute(HtmlTextWriterAttribute.Type,"Hidden");
			output.AddAttribute(HtmlTextWriterAttribute.Name,UniqueID);
			output.AddAttribute(HtmlTextWriterAttribute.Value,Date.Ticks.ToString());
			output.RenderBeginTag(HtmlTextWriterTag.Input);
			output.RenderEndTag();
 
			// Check each element separated by semicolon char in the Format string.
			// If the element is the string representation of a specific date part,
			// the code render the selector. If not, the element is rendered as
			// text to the HtmlTextWriter.
			foreach(string element in Format.Split(';'))
			{
				switch (element.ToUpper())
				{
					case "DAY":
						WriteSelector(output, "_day", DateStyle, "isValid('" + UniqueID + "_year', '" + UniqueID + "_month', '" + UniqueID + "_day')", 1, 31, 2, Date.Day);
						break;
					case "MONTH":
						WriteSelector(output, "_month", DateStyle, "isValid('" + UniqueID + "_year', '" + UniqueID + "_month', '" + UniqueID + "_day')", 1, 12, 2, Date.Month);
						break;
					case "YEAR":
						WriteSelector(output, "_year", DateStyle, "isValid('" + UniqueID + "_year', '" + UniqueID + "_month', '" + UniqueID + "_day')", _minYear, _maxYear, 4, Date.Year-18);
						break;
					case "HOUR":
						WriteSelector(output, "_hour", TimeStyle, null, 0, 23, 2, Date.Hour);
						break;
					case "MINUTE":
						WriteSelector(output, "_minute", TimeStyle, null, 0, 59, 2, Date.Minute);
						break;
					case "SECOND":
						WriteSelector(output, "_second", TimeStyle, null, 0, 59, 2, Date.Second);
						break;
					case "MILLISECOND":
						WriteSelector(output, "_millisecond", TimeStyle, null, 0, 999, 3, Date.Millisecond);
						break;
					default:
						output.Write(element);
						break;
				}
			}
		}
 
		/// <summary>
		/// This method create a selector based on the parameters.
		/// </summary>
		/// <param name="output">The HtmlTextWriter to write.</param>
		/// <param name="suffix">The suffix to use to identify the selector with the LoadPostData method.</param>
		/// <param name="style">The style class to use.</param>
		/// <param name="onchange">The value of the OnChange attribute of the selector to use with the client side validator.</param>
		/// <param name="min">The minimum value of the selector.</param>
		/// <param name="max">The maximum value of the selector.</param>
		/// <param name="padding">The number of chars to use with padding.</param>
		/// <param name="selectedValue">The selected value.</param>
		private void WriteSelector(HtmlTextWriter output, string suffix, System.Web.UI.WebControls.Style style, string onchange, int min, int max, int padding, int selectedValue)
		{
			// Some variable we will use
			int index;
 
			// Check if the actual year value can be displayed in the selector
			if (selectedValue < min || selectedValue > max)
				throw new Exception("The year value (" + Date.Year.ToString() + ") of the Date property is greater than the maximum (" + max.ToString() + ") or less than the minimum (" + min.ToString() + "). Please adjust values or set AutoAdjust property to true.");
 
			// Write the selector
			output.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID + suffix);
						
			// Render the validation action code only if needed
			if(_renderUplevel && onchange != null)
				output.AddAttribute(HtmlTextWriterAttribute.Onchange, onchange);
			
			// Add the styles to the selector after the merge with the base style.
			style.MergeWith(_baseStyle);
			style.AddAttributesToRender(output);
 
			output.RenderBeginTag(HtmlTextWriterTag.Select);
			
			// Write the option tags
			for(index=min;index<=max;index++)
			{
				output.AddAttribute(HtmlTextWriterAttribute.Value, index.ToString());
 
				// Set the selected value
				if (index == selectedValue)
					output.AddAttribute(HtmlTextWriterAttribute.Selected, null);
 
				output.RenderBeginTag(HtmlTextWriterTag.Option);
				if (suffix == "_month" && !_monthNamesDisabled)
					output.InnerWriter.Write(_months[index-1]);
				else
					output.InnerWriter.Write(index.ToString().PadLeft(padding, '0'));
				output.RenderEndTag();
			}
 
			// Write the selector end tag
			output.RenderEndTag();
		}
 
		/// <summary>
		/// Processes post back data for an the server control.
		/// </summary>
		/// <param name="postDataKey">The key identifier for the control.</param>
		/// <param name="postCollection">The collection of all incoming name values.</param>
		/// <returns>true if the server control's state changes as a result of the post back; otherwise false.</returns>
		public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) 
		{
			int day, month, year, hour, minute, second, millisecond;
 
			_oldDate = Date;
 
			day = Convert.ToInt16(postCollection[UniqueID + "_day"]);
			month = Convert.ToInt16(postCollection[UniqueID + "_month"]);
			year = Convert.ToInt16(postCollection[UniqueID + "_year"]);
			hour = Convert.ToInt16(postCollection[UniqueID + "_hour"]);
			minute = Convert.ToInt16(postCollection[UniqueID + "_minute"]);
			second = Convert.ToInt16(postCollection[UniqueID + "_second"]);
			millisecond = Convert.ToInt16(postCollection[UniqueID + "_millisecond"]);
 
			try
			{
				Date = new DateTime(year == 0 ? DateTime.Now.Year : year, month == 0 ? DateTime.Now.Month : month, day == 0 ? DateTime.Now.Day : day, hour, minute, second, millisecond);
			}
			catch
			{
				throw new Exception("The selectors represent an invalid date. Please use client side validation to prevent this.");
			}
 
			if (!Date.Equals(_oldDate))
				return true;
 
			return false;
		}
 
		/// <summary>
		/// Signals the server control object to notify the ASP.NET application that the state of the control has changed.
		/// </summary>
		public virtual void RaisePostDataChangedEvent() 
		{
			OnDateChanged(new DateChangedEventArgs(_oldDate, Date));
		}
 
		/// <summary>
		/// Occurs when the Date property value changes.
		/// </summary>
		public event DateChangedEventHandler DateChanged;
 
		/// <summary>
		/// The DateChanged event handler.
		/// </summary>
		public delegate void DateChangedEventHandler(object sender, DateChangedEventArgs e);
 
		/// <summary>
		/// Raises the DateChanged event.
		/// </summary>
		/// <param name="e">An <see cref="DateChangedEventArgs"/> that contains the event data.</param>
		protected virtual void OnDateChanged(DateChangedEventArgs e) 
		{
			// Check if someone use our event.
			if (DateChanged != null)
				DateChanged(this,e);
		}
 
		/// <summary>
		/// Gets or sets a value indicating whether client-side validation is enabled.
		/// </summary>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets or sets a value indicating whether client-side validation is enabled.")]
		public bool EnableClientScript 
		{
			get 
			{
				object o = ViewState["EnableClientScript"];
				return((o == null) ? true : (bool)o);
			}
			set 
			{
				ViewState["EnableClientScript"] = value;
			}
		}
 
		/// <summary>
		/// Gets or sets the date of the control.
		/// </summary>
		[Bindable(true),
		Category("Data"),
		Description("Gets or sets the date of the control.")]
		public DateTime Date
		{
			get
			{
				if (ViewState["_date"] == null)
					ViewState["_date"] = DateTime.Now;
				return (DateTime)ViewState["_date"];
			}
			set
			{
				ViewState["_date"] = value;
			}
		}
 
		/// <summary>
		/// Gets or sets the minimum year to display in the year selector.
		/// </summary>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets or sets the minimum year to display in the year selector.")]
		public int MinYear
		{
			get
			{
				return _minYear;
			}
			set
			{
				_minYear = value;
			}
		}
 
		/// <summary>
		/// Gets or sets the maximum year to display in the year selector.
		/// </summary>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets or sets the maximum year to display in the year selector.")]
		public int MaxYear
		{
			get
			{
				return _maxYear;
			}
			set
			{
				_maxYear = value;
			}
		}
 
		/// <summary>
		/// Lets you specify if you want the control to show an alert popup when a invalid date is selected.
		/// </summary>
		public bool AlertEnabled
		{
			get
			{
				return _alertEnabled;
			}
			set
			{
				_alertEnabled = value;
			}
		}
 
		/// <summary>
		/// Set to true is we want to auto adjust the maximum and/or minimum year with the data.
		/// </summary>
		/// <remarks>Setting a high value to <see cref="MaxYear"/> or setting a low value in <see cref="MinYear"/> can produce performance problem.
		/// For each year, more than 20 bytes are added to the browser HTML output.
		/// By setting this property to true, you can prevent from exception throws without having to set very high maximum year or very low minimum year.
		/// If the maximum year value is less than the actual date year value, the maximum year will be adjusted to the actual date year. Same for the minimum year.</remarks>
		[Bindable(true),
		Category("Appearance"),
		Description("Set to true is we want to auto adjust the maximum and/or minimum year with the data.")]
		public bool AutoAdjust
		{
			get
			{
				return _autoAdjust;
			}
			set
			{
				_autoAdjust = value;
			}
		}
 
		/// <summary>
		/// Gets the style properties of the date and time selectors base.
		/// </summary>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets the style properties of the date and time selectors base.")]
		public System.Web.UI.WebControls.Style BaseStyle
		{
			get
			{
				return _baseStyle;
			}
			set
			{
				_baseStyle = value;
			}
		}
 
		/// <summary>
		/// Gets the style properties of the date selectors.
		/// </summary>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets the style properties of the date selectors.")]
		public System.Web.UI.WebControls.Style DateStyle
		{
			get
			{
				return _dateStyle;
			}
			set
			{
				_dateStyle = value;
			}
		}
 
		/// <summary>
		/// Gets or sets the month names.
		/// </summary>
		/// <remarks>
		/// <code>
		/// // This line will replace the january month name by 'Janvier'
		/// MyDate.Months[0] = "Janvier";
		/// 
		/// // This line will replace the december month name by 'Décembre (noel)'
		/// MyDate.Months[11] = "Décembre (noel)";
		/// </code>
		/// </remarks>
		public string[] Months
		{
			get
			{
				return _months;
			}
			set
			{
				_months = value;
			}
		}
 
		/// <summary>
		/// Sets the month names.
		/// </summary>
		/// <remarks>
		/// This array of string contains the month names. You can set your own month names to match with your culture. If your website is in french, you will prefer use Février in replacment of February.
		/// By default, the months are in english. If the <see cref="MonthNamesDisabled"/> property is set to true, numbers will replace month names.
		/// <code>
		/// // Please verify that MonthNamesDisabled property is not set to true
		/// &lt;AU:ActiveDateTime runat="server" id="CompleteSelector" Format="day;/;month;/;year; ;hour;:;minute;:;second;:;millisecond" SetMonthNames="Janvier,F&amp;eacute;vrier,Mars,Avril,Mai,Juin,Juillet,Ao&amp;ucirc;t,Septembre,Octobre,Novembre,D&amp;eacute;cembre"&gt;&lt;/AU:ActiveDateTime&gt;
		/// </code>
		/// </remarks>
		public string SetMonthNames
		{
			set
			{
				string[] months = value.Split(',');
				int index;
 
				for(index=0;index<months.Length;index++)
				{
					_months[index] = months[index];
				}
			}
		}
	
		/// <summary>
		/// Specify if you want to display month names or month numbers. False by default.
		/// </summary>
		public bool MonthNamesDisabled
		{
			get
			{
				return _monthNamesDisabled;
			}
			set
			{
				_monthNamesDisabled = value;
			}
		}
 
		/// <summary>
		/// Gets the style properties of the time selectors.
		/// </summary>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets the style properties of the time selectors.")]
		public System.Web.UI.WebControls.Style TimeStyle
		{
			get
			{
				return _timeStyle;
			}
			set
			{
				_timeStyle = value;
			}
		}
 
		/// <summary>
		/// Gets or sets the alert text to use when a invalid date is selected.
		/// </summary>
		public string AlertText
		{
			get
			{
				return _alertText;
			}
			set
			{
				_alertText = value;
			}
		}
 
		/// <summary>
		/// Gets or sets the format to use to render the selectors.
		/// </summary>
		/// <remarks>You can specify the display layout using the <c>format specifiers</c> like <c>"hour"</c> or <c>"month"</c>.<br></br>
		/// Fields must be separated by ; (semicolon) char. Non reconized fields are send to the HtmlTextWriter as literal text.<br></br>
		/// Here are some examples (<c>[</c> and <c>]</c> represent a selector:<br></br><br></br>
		///	<c>"month;/;day;/;year"</c> will display<br></br><c>[MONTH]/[DAY]/[YEAR]</c>.<br></br><br></br>
		///	<c>"hour;:;minute"</c> will display<br></br><c>[HOUR]:[MINUTE]</c>.<br></br><br></br>
		///	<c>"Date : ;day;/;month;/;year; Time : ;hour;:;minute;:;second"</c> will display<br></br><c>Date : [DAY]/[MONTH]/[YEAR] Time : [HOUR]:[MINUTE]:[SECOND]</c>.<br></br><br></br>
		///	<table><tr><td bgcolor="#F0F0F0">Format Specifier</td><td bgcolor="#F0F0F0">Name</td></tr>
		///	<tr><td><b>day</b></td><td>The day part (1 to 31).</td></tr>
		///	<tr><td><b>month</b></td><td>The month part (1 to 12).</td></tr>
		///	<tr><td><b>year</b></td><td>The year part (variable).</td></tr>
		///	<tr><td><b>hour</b></td><td>The hour part (0 to 23).</td></tr>
		///	<tr><td><b>minute</b></td><td>The minute part (0 to 59).</td></tr>
		///	<tr><td><b>second</b></td><td>The second part (0 to 59).</td></tr>
		///	<tr><td><b>millisecond</b></td><td>The millisecond part (0 to 999).</td></tr>
		///	<tr><td><i>other literal</i></td><td>Rendered as literal text.</td></tr>
		///	</table>
		/// </remarks>
		[Bindable(true),
		Category("Appearance"),
		Description("Gets or sets the format to use to render the selectors.")]
		public string Format
		{
			get
			{
				return _format;
			}
			set
			{
				_format = value;
			}
		}
	}
}
[+][-]01/05/08 10:05 AM, ID: 20589935

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/05/08 10:50 AM, ID: 20590129

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/05/08 05:19 PM, ID: 20592068

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/05/08 08:33 PM, ID: 20592509

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Programming for ASP.NET
Tags: display, enableclientscript, month
Sign Up Now!
Solution Provided By: JimBrandley
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20091111-EE-VQP-91 / EE_QW_2_20070628