source: default/framework/trunk/WebLibrary/Sources/UI/WebControls/Button.cs @ 1686

このファイルの 1686 以降における最終更新内容1686 で hizuya@… が 4週前 に更新しました
  • HTML フォームのバリデーションを無効にする機能を追加。
ファイルサイズ: 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    using System.Web.UI.WebControls;
29
30
31    /// <summary>
32    /// Web ページにプッシュ ボタン コントロールを表示します。
33    /// </summary>
34    [WebDescription("Description_Button")]
35    [ControlInitializerTarget]
36    public class Button :
37        System.Web.UI.WebControls.Button
38    {
39        /// <summary>
40        /// <see cref="SuppressHtmlValidation"/> プロパティ用のキー。
41        /// </summary>
42        private const string SuppressHtmlValidationKey = "SuppressHtmlValidation";
43
44
45        /// <summary>
46        /// <see cref="RaiseEventWithIsNotValid"/> プロパティ用のキー。
47        /// </summary>
48        private const string RaiseEventWithIsNotValidKey = "RaiseEventWithIsNotValid";
49
50        /// <summary>
51        /// <see cref="ConfirmControl"/> プロパティ用のキー。
52        /// </summary>
53        private const string ConfirmControlKey = "ConfirmControl";
54
55        /// <summary>
56        /// <see cref="ConfirmArgument"/> プロパティ用のキー。
57        /// </summary>
58        private const string ConfirmArgumentKey = "ConfirmArgument";
59
60
61        /// <summary>
62        /// インスタンスを作成します。
63        /// </summary>
64        public Button()
65        {
66            ControlInitializer.ApplyProperties(this);
67        }
68
69
70        /// <summary>
71        /// HTML のフォームバリデーションを抑制するかどうかを取得または設定します。
72        /// </summary>
73        /// <value>
74        /// HTML のフォームバリデーションを抑制する場合は <see langword="true"/>、
75        /// それ以外の場合は <see langword="false"/>。
76        /// 既定値は <see langword="false"/> です。
77        /// </value>
78        [Themeable(false)]
79        [DefaultValue(false)]
80        [WebCategory("Behavior")]
81        [WebDescription("Description_SuppressHtmlValidation")]
82        public virtual bool SuppressHtmlValidation
83        {
84            get
85            {
86                return (bool)(ViewState[SuppressHtmlValidationKey] ?? false);
87            }
88
89            set
90            {
91                ViewState[SuppressHtmlValidationKey] = value;
92            }
93        }
94
95        /// <summary>
96        /// バリデーションにエラーがあった場合でも
97        /// <see cref="System.Web.UI.WebControls.ListControl.SelectedIndexChanged"/>
98        /// イベントを実行するかどうかを取得及び設定します。
99        /// </summary>
100        /// <value>
101        /// バリデーションにエラーがあった場合でも
102        /// <see cref="System.Web.UI.WebControls.ListControl.SelectedIndexChanged"/>
103        /// イベントを実行する場合は <see langword="true"/>、
104        /// それ以外の場合は <see langword="false"/>。
105        /// 既定値は <see langword="true"/>。
106        /// </value>
107        [Themeable(false)]
108        [DefaultValue(true)]
109        [WebCategory("Behavior")]
110        [WebDescription("Description_RaiseEventWithIsNotValid")]
111        public virtual bool RaiseEventWithIsNotValid
112        {
113            get
114            {
115                return (bool)(ViewState[RaiseEventWithIsNotValidKey] ?? true);
116            }
117
118            set
119            {
120                ViewState[RaiseEventWithIsNotValidKey] = value;
121            }
122        }
123
124        /// <summary>
125        /// クリックされたときに、クライアントサイド確認を行う
126        /// <see cref="Confirm"/> コントロールのIDを取得及び設定します。
127        /// </summary>
128        /// <value>
129        /// <see cref="Confirm"/> コントロールのID。
130        /// 既定値は空の文字列 ("")。
131        /// </value>
132        [IDReferenceProperty(typeof(Confirm))]
133        [Themeable(false)]
134        [DefaultValue("")]
135        [WebCategory("Behavior")]
136        [WebDescription("Description_ConfirmControl")]
137        public virtual string ConfirmControl
138        {
139            get
140            {
141                return (string)(ViewState[ConfirmControlKey] ?? string.Empty);
142            }
143
144            set
145            {
146                ViewState[ConfirmControlKey] = value;
147            }
148        }
149
150        /// <summary>
151        /// クリックされたときに、クライアントサイド確認を行う
152        /// <see cref="Confirm"/> コントロールに渡す引数を取得及び設定します。
153        /// </summary>
154        /// <value>
155        /// <see cref="Confirm"/> コントロールに渡す引数。
156        /// 既定値は空の文字列 ("")。
157        /// </value>
158        [Themeable(false)]
159        [DefaultValue("")]
160        [WebCategory("Behavior")]
161        [WebDescription("Description_ConfirmArgument")]
162        public virtual string ConfirmArgument
163        {
164            get
165            {
166                return (string)(ViewState[ConfirmArgumentKey] ?? string.Empty);
167            }
168
169            set
170            {
171                ViewState[ConfirmArgumentKey] = value;
172            }
173        }
174
175
176        /// <summary>
177        /// サーバーへのポストバック時に、<see cref="Button"/> コントロールのイベントを発生させます。
178        /// </summary>
179        /// <param name="eventArgument">
180        /// イベントの引数。
181        /// </param>
182        protected override void RaisePostBackEvent(string eventArgument)
183        {
184            // バリデータの結果を気にしない場合は、親クラスの機能を使用する
185            if (!CausesValidation || RaiseEventWithIsNotValid)
186            {
187                base.RaisePostBackEvent(eventArgument);
188                return;
189            }
190
191            // バリデータのチェック
192            Page.Validate(ValidationGroup);
193            if (!Page.IsValid)
194            {
195                return;
196            }
197
198            // イベントを実行
199            OnClick(EventArgs.Empty);
200            OnCommand(new CommandEventArgs(CommandName, CommandArgument));
201        }
202
203        /// <summary>
204        /// <see cref="HyperLink"/> コントロールの属性を表示用の出力ストリームに追加します。
205        /// </summary>
206        /// <param name="writer">
207        /// クライアントで表示するための出力ストリーム。
208        /// </param>
209        protected override void AddAttributesToRender(HtmlTextWriter writer)
210        {
211            string confirmControlId = ConfirmControl;
212            if (!string.IsNullOrEmpty(confirmControlId))
213            {
214                EnsureID();
215            }
216
217            // 親を呼び出す
218            base.AddAttributesToRender(writer);
219
220            if (SuppressHtmlValidation)
221            {
222                writer.AddAttribute(Utility.FormNoValidateAttributeName, Utility.FormNoValidateAttributeName);
223            }
224
225            // 確認ダイアログとの連携
226            if (!DesignMode && IsEnabled)
227            {
228                if (!string.IsNullOrEmpty(confirmControlId))
229                {
230                    WebUtility.FindControl<Confirm>(this, confirmControlId).RegisterOnClientClick(this, ConfirmArgument);
231                }
232            }
233        }
234    }
235}
詳しい使い方は TracBrowser を参照してください。