source: default/framework/trunk/WebLibrary/Sources/UI/WebControls/RangeValidator.cs @ 864

このファイルの 864 以降における最終更新内容864 で hizuya@… が 12年前 に更新しました
  • プリミティブ型がある場合はそれを使用するように変更。
  • XML コメント中の null, true, false を <see langword=""/> を使うように変更。
  • using ディレクティブを SA1200 に合わせて最適化。
ファイルサイズ: 8.4 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    /// </summary>
33    [WebDescription("Description_RangeValidator")]
34    [ControlInitializerTarget]
35    public class RangeValidator :
36        System.Web.UI.WebControls.RangeValidator
37    {
38        /// <summary>
39        /// <see cref="ErrorMessageFormatString"/> プロパティ用のキー。
40        /// </summary>
41        private const string ErrorMessageFormatStringKey = "ErrorMessageFormatString";
42
43        /// <summary>
44        /// <see cref="ControlNameToValidate"/> プロパティ用のキー。
45        /// </summary>
46        private const string ControlNameToValidateKey = "ControlNameToValidate";
47
48
49        /// <summary>
50        /// エラーメッセージを準備する必要があるかどうか。
51        /// </summary>
52        private bool requiresErrorMessage;
53
54        /// <summary>
55        /// <see cref="Control.PreRender"/> に到達しているかどうか。
56        /// </summary>
57        private bool preRendered;
58
59
60        /// <summary>
61        /// インスタンスを作成します。
62        /// </summary>
63        public RangeValidator()
64        {
65            ControlInitializer.ApplyProperties(this);
66        }
67
68
69        /// <summary>
70        /// <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> の元になるフォーマット文字列です。
71        /// </summary>
72        /// <value>
73        /// <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> の元になるフォーマット文字列。
74        /// 既定値は空文字。
75        /// </value>
76        /// <remarks>
77        /// <para>
78        /// このプロパティが設定されると、このプロパティ値をフォーマット文字列として
79        /// <c>{0}</c> に <see cref="ControlNameToValidate"/>、
80        /// <c>{1}</c> に <see cref="System.Web.UI.WebControls.RangeValidator.MinimumValue"/>、
81        /// <c>{2}</c> に <see cref="System.Web.UI.WebControls.RangeValidator.MaximumValue"/>
82        /// が埋め込まれた値が
83        /// <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> が設定されます。
84        /// </para>
85        /// <para>
86        /// フォーマット文字列を無効にする場合は、空文字列をセットします。
87        /// ただし、その場合でも、元のエラーメッセージは復元されません。
88        /// 再度 <see cref="System.Web.UI.WebControls.BaseValidator.ErrorMessage"/> に設定し直す必要があります。
89        /// </para>
90        /// </remarks>
91        [Localizable(true)]
92        [Themeable(false)]
93        [DefaultValue("")]
94        [WebCategory("Appearance")]
95        [WebDescription("Description_ErrorMessageFormatString")]
96        public string ErrorMessageFormatString
97        {
98            get
99            {
100                return (string)(ViewState[ErrorMessageFormatStringKey] ?? string.Empty);
101            }
102
103            set
104            {
105                if (ErrorMessageFormatString == value)
106                {
107                    return;
108                }
109
110                ViewState[ErrorMessageFormatStringKey] = value;
111                RequiresErrorMessage = true;
112            }
113        }
114
115        /// <summary>
116        /// 検証対象のコントロールの名称を表します。
117        /// </summary>
118        /// <value>
119        /// 検証対象のコントロールの名称。
120        /// 既定値は空文字。
121        /// </value>
122        [Localizable(true)]
123        [Themeable(false)]
124        [DefaultValue("")]
125        [WebCategory("Appearance")]
126        [WebDescription("Description_ControlNameToValidate")]
127        public virtual string ControlNameToValidate
128        {
129            get
130            {
131                return (string)(ViewState[ControlNameToValidateKey] ?? string.Empty);
132            }
133
134            set
135            {
136                if (ControlNameToValidate == value)
137                {
138                    return;
139                }
140
141                ViewState[ControlNameToValidateKey] = value;
142                RequiresErrorMessage = true;
143            }
144        }
145
146        /// <summary>
147        /// エラーメッセージを準備する必要があるかどうかを取得及び設定します。
148        /// </summary>
149        /// <value>
150        /// 準備する場合は <see langword="true"/>、それ以外の場合は <see langword="false"/>。
151        /// 既定値は <see langword="false"/>。
152        /// </value>
153        protected virtual bool RequiresErrorMessage
154        {
155            get
156            {
157                return requiresErrorMessage;
158            }
159
160            set
161            {
162                if (value && preRendered && Page != null && !Page.IsCallback)
163                {
164                    requiresErrorMessage = true;
165                    EnsureErrorMessage();
166                }
167                else
168                {
169                    requiresErrorMessage = value;
170                }
171            }
172        }
173
174
175        /// <summary>
176        /// <see cref="Control.Init"/> イベントを呼び出します。
177        /// </summary>
178        /// <param name="e">
179        /// イベント データを格納している <see cref="EventArgs"/>。
180        /// </param>
181        protected override void OnInit(EventArgs e)
182        {
183            // 親を呼び出す
184            base.OnInit(e);
185
186            EnsureErrorMessage();
187        }
188
189        /// <summary>
190        /// このコントロールがレンダリングされる直前に呼び出されるイベントハンドラです。
191        /// </summary>
192        /// <param name="e">
193        /// イベント データを格納している <see cref="EventArgs"/>。
194        /// </param>
195        protected override void OnPreRender(EventArgs e)
196        {
197            preRendered = true;
198
199            // エラーメッセージを準備
200            EnsureErrorMessage();
201
202            // 親を呼び出す
203            base.OnPreRender(e);
204        }
205
206        /// <summary>
207        /// エラーメッセージを作成する必要がある場合に作成します。
208        /// </summary>
209        protected virtual void EnsureErrorMessage()
210        {
211            if (!RequiresErrorMessage)
212            {
213                return;
214            }
215
216            BuildErrorMessage();
217        }
218
219        /// <summary>
220        /// エラーメッセージを作成します。
221        /// </summary>
222        protected virtual void BuildErrorMessage()
223        {
224            string format = ErrorMessageFormatString;
225
226            // フォーマット文字列が未設定の場合は何もしない
227            if (string.IsNullOrEmpty(format))
228            {
229                return;
230            }
231
232            ErrorMessage
233                = Utility.Format(
234                    format,
235                    ControlNameToValidate,
236                    MinimumValue,
237                    MaximumValue);
238
239            RequiresErrorMessage = false;
240        }
241    }
242}
詳しい使い方は TracBrowser を参照してください。