source: default/framework/trunk/WebLibrary/Sources/UI/WebControls/CompareValidator.cs @ 1622

このファイルの 1622 以降における最終更新内容1622 で hizuya@… が 13ヵ月前 に更新しました
  • Validator 系に、親や祖先の Enabled の影響を受ける設定を追加。
  • PlaceHolder の Enabled プロパティを追加。
ファイルサイズ: 12.0 KB
 
1// ----------------------------------------------------------------------------
2// <copyright company="F.C. Soft., Inc.">
3//   Copyright(c) F.C. Soft., Inc.  All rights reserved.
4// </copyright>
5// <license>
6//   Licensed to F.C. Soft., Inc. (FCSoft) under one or more contributor
7//   license agreements.  See the NOTICE file distributed with this work for
8//   additional information regarding copyright ownership.  FCSoft licenses
9//   this file to You under the Apache License, Version 2.0 (the "License");
10//   you may not use this file except in compliance with the License.  You
11//   may obtain a copy of the License at
12//
13//     http://www.apache.org/licenses/LICENSE-2.0
14//
15//   Unless required by applicable law or agreed to in writing, software
16//   distributed under the License is distributed on an "AS IS" BASIS,
17//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18//   See the License for the specific language governing permissions and
19//   limitations under the License.
20// </license>
21// ----------------------------------------------------------------------------
22
23namespace FCSoft.SilverFrost.Framework.Web.UI.WebControls
24{
25    using System;
26    using System.ComponentModel;
27    using System.Web.UI;
28
29
30    /// <summary>
31    /// ユーザーが入力コントロールに入力した値を、
32    /// 別の入力コントロールに入力された値または定数値のいずれかと比較します。
33    /// </summary>
34    [WebDescription("Description_CompareValidator")]
35    [ControlInitializerTarget]
36    public class CompareValidator :
37        System.Web.UI.WebControls.CompareValidator
38    {
39        /// <summary>
40        /// <see cref="AffectedByAncestors"/> プロパティ用のキー。
41        /// </summary>
42        private const string AffectedByAncestorsKey = "AffectedByAncestors";
43
44        /// <summary>
45        /// <see cref="ErrorMessageFormatString"/> プロパティ用のキー。
46        /// </summary>
47        private const string ErrorMessageFormatStringKey = "ErrorMessageFormatString";
48
49        /// <summary>
50        /// <see cref="ControlNameToValidate"/> プロパティ用のキー。
51        /// </summary>
52        private const string ControlNameToValidateKey = "ControlNameToValidate";
53
54        /// <summary>
55        /// <see cref="CompareNameToValidate"/> プロパティ用のキー。
56        /// </summary>
57        private const string CompareNameToValidateKey = "CompareNameToValidate";
58
59
60        /// <summary>
61        /// エラーメッセージを準備する必要があるかどうか。
62        /// </summary>
63        private bool requiresErrorMessage;
64
65        /// <summary>
66        /// <see cref="Control.PreRender"/> に到達しているかどうか。
67        /// </summary>
68        private bool preRendered;
69
70
71        /// <summary>
72        /// インスタンスを作成します。
73        /// </summary>
74        public CompareValidator()
75        {
76            ControlInitializer.ApplyProperties(this);
77        }
78
79
80        /// <summary>
81        /// 親や祖先の要素の影響を受けるかどうかを取得または設定します。
82        /// </summary>
83        /// <value>
84        /// 親や祖先の要素の影響を受けてバリデーションを実行するかどうかを決定する場合は <see langword="true"/>。
85        /// それ以外の場合は <see langword="false"/>。
86        /// 既定値は <see langword="false"/>。
87        /// </value>
88        [Themeable(false)]
89        [DefaultValue(false)]
90        [WebCategory("Behavior")]
91        [WebDescription("Description_AllowBlank")]
92        public bool AffectedByAncestors
93        {
94            get
95            {
96                return (bool)(ViewState[AffectedByAncestorsKey] ?? false);
97            }
98
99            set
100            {
101                ViewState[AffectedByAncestorsKey] = value;
102            }
103        }
104
105        /// <summary>
106        /// <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> の元になるフォーマット文字列です。
107        /// </summary>
108        /// <value>
109        /// <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> の元になるフォーマット文字列。
110        /// 既定値は空文字。
111        /// </value>
112        /// <remarks>
113        /// <para>
114        /// このプロパティが設定されると、このプロパティ値をフォーマット文字列として
115        /// <c>{0}</c> に <see cref="ControlNameToValidate"/>、
116        /// <c>{1}</c> に <see cref="CompareNameToValidate"/>
117        /// が埋め込まれた値が
118        /// <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> が設定されます。
119        /// </para>
120        /// <para>
121        /// フォーマット文字列を無効にする場合は、空文字列をセットします。
122        /// ただし、その場合でも、元のエラーメッセージは復元されません。
123        /// 再度 <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> に設定し直す必要があります。
124        /// </para>
125        /// </remarks>
126        [Localizable(true)]
127        [Themeable(false)]
128        [DefaultValue("")]
129        [WebCategory("Appearance")]
130        [WebDescription("Description_ErrorMessageFormatString")]
131        public string ErrorMessageFormatString
132        {
133            get
134            {
135                return (string)(ViewState[ErrorMessageFormatStringKey] ?? string.Empty);
136            }
137
138            set
139            {
140                if (ErrorMessageFormatString == value)
141                {
142                    return;
143                }
144
145                ViewState[ErrorMessageFormatStringKey] = value;
146                RequiresErrorMessage = true;
147            }
148        }
149
150        /// <summary>
151        /// 検証対象のコントロールの名称を表します。
152        /// </summary>
153        /// <value>
154        /// 検証対象のコントロールの名称。
155        /// 既定値は空文字。
156        /// </value>
157        [Localizable(true)]
158        [Themeable(false)]
159        [DefaultValue("")]
160        [WebCategory("Appearance")]
161        [WebDescription("Description_ControlNameToValidate")]
162        public virtual string ControlNameToValidate
163        {
164            get
165            {
166                return (string)(ViewState[ControlNameToValidateKey] ?? string.Empty);
167            }
168
169            set
170            {
171                if (ControlNameToValidate == value)
172                {
173                    return;
174                }
175
176                ViewState[ControlNameToValidateKey] = value;
177                RequiresErrorMessage = true;
178            }
179        }
180
181        /// <summary>
182        /// 比較対象の名称を表します。
183        /// </summary>
184        /// <value>
185        /// 比較対象の名称。
186        /// 既定値は空文字。
187        /// </value>
188        [Localizable(true)]
189        [Themeable(false)]
190        [DefaultValue("")]
191        [WebCategory("Appearance")]
192        [WebDescription("Description_CompareNameToValidate")]
193        public virtual string CompareNameToValidate
194        {
195            get
196            {
197                return (string)(ViewState[CompareNameToValidateKey] ?? string.Empty);
198            }
199
200            set
201            {
202                if (CompareNameToValidate == value)
203                {
204                    return;
205                }
206
207                ViewState[CompareNameToValidateKey] = value;
208                RequiresErrorMessage = true;
209            }
210        }
211
212        /// <summary>
213        /// コントロールの <see cref="System.Web.UI.WebControls.WebControl.IsEnabled" />
214        /// プロパティが <see langword="false"/> の場合、レンダリングされた HTML 要素の disabled 属性を
215        /// "無効" に設定するかどうかを示す値を取得します。
216        /// </summary>
217        /// <value>
218        /// <see cref="Control.RenderingCompatibility" /> プロパティが
219        /// 4.0 より低い ASP.NET のバージョンを示す場合は <see langword="true"/>。
220        /// それ以外の場合は <see langword="false"/>。
221        /// </value>
222        public override bool SupportsDisabledAttribute
223        {
224            get
225            {
226                return !AffectedByAncestors && base.SupportsDisabledAttribute;
227            }
228        }
229
230        /// <summary>
231        /// 検証コントロールを有効にするかどうかを示す値を取得または設定します。
232        /// </summary>
233        /// <value>
234        /// 検証コントロールを有効にする場合は <see langword="true"/>。
235        /// それ以外の場合は <see langword="false"/>。
236        /// 既定値は、<see langword="true"/> です。
237        /// </value>
238        public override bool Enabled
239        {
240            get
241            {
242                return base.Enabled && (!AffectedByAncestors || IsEnabled);
243            }
244
245            set
246            {
247                base.Enabled = value;
248            }
249        }
250
251        /// <summary>
252        /// エラーメッセージを準備する必要があるかどうかを取得及び設定します。
253        /// </summary>
254        /// <value>
255        /// 準備する場合は <see langword="true"/>、それ以外の場合は <see langword="false"/>。
256        /// 既定値は <see langword="false"/>。
257        /// </value>
258        protected virtual bool RequiresErrorMessage
259        {
260            get
261            {
262                return requiresErrorMessage;
263            }
264
265            set
266            {
267                if (value && preRendered && Page != null && !Page.IsCallback)
268                {
269                    requiresErrorMessage = true;
270                    EnsureErrorMessage();
271                }
272                else
273                {
274                    requiresErrorMessage = value;
275                }
276            }
277        }
278
279
280        /// <summary>
281        /// <see cref="Control.Init"/> イベントを呼び出します。
282        /// </summary>
283        /// <param name="e">
284        /// イベント データを格納している <see cref="EventArgs"/>。
285        /// </param>
286        protected override void OnInit(EventArgs e)
287        {
288            // 親を呼び出す
289            base.OnInit(e);
290
291            EnsureErrorMessage();
292        }
293
294        /// <summary>
295        /// このコントロールがレンダリングされる直前に呼び出されるイベントハンドラです。
296        /// </summary>
297        /// <param name="e">
298        /// イベント データを格納している <see cref="EventArgs"/>。
299        /// </param>
300        protected override void OnPreRender(EventArgs e)
301        {
302            preRendered = true;
303
304            // エラーメッセージを準備
305            EnsureErrorMessage();
306
307            // 親を呼び出す
308            base.OnPreRender(e);
309        }
310
311        /// <summary>
312        /// エラーメッセージを作成する必要がある場合に作成します。
313        /// </summary>
314        protected virtual void EnsureErrorMessage()
315        {
316            if (!RequiresErrorMessage)
317            {
318                return;
319            }
320
321            BuildErrorMessage();
322        }
323
324        /// <summary>
325        /// エラーメッセージを作成します。
326        /// </summary>
327        protected virtual void BuildErrorMessage()
328        {
329            string format = ErrorMessageFormatString;
330
331            // フォーマット文字列が未設定の場合は何もしない
332            if (string.IsNullOrEmpty(format))
333            {
334                return;
335            }
336
337            ErrorMessage
338                = Utility.Format(
339                    format,
340                    ControlNameToValidate,
341                    CompareNameToValidate);
342
343            RequiresErrorMessage = false;
344        }
345    }
346}
詳しい使い方は TracBrowser を参照してください。