source: default/framework/trunk/WebLibrary/Sources/UI/WebControls/Form.cs @ 1687

このファイルの 1687 以降における最終更新内容1687 で hizuya@… が 4週前 に更新しました
  • ControlInitializer で初期化できるようにした。
ファイルサイズ: 7.9 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// ----------------------------------------------------------------------------
22namespace FCSoft.SilverFrost.Framework.Web.UI.WebControls
23{
24    using System;
25    using System.ComponentModel;
26    using System.Web;
27    using System.Web.UI;
28    using System.Web.UI.HtmlControls;
29    using System.Web.UI.WebControls;
30
31
32    /// <summary>
33    /// フォーム機能を提供するクラスです。
34    /// </summary>
35    [ControlInitializerTarget]
36    public class Form :
37        HtmlForm
38    {
39        /// <summary>
40        /// <see cref="SuppressHtmlValidation"/> プロパティ用のキー。
41        /// </summary>
42        private const string SuppressHtmlValidationKey = "SuppressHtmlValidation";
43
44
45        /// <summary>
46        /// インスタンスを作成します。
47        /// </summary>
48        public Form()
49        {
50            ControlInitializer.ApplyProperties(this);
51        }
52
53
54        /// <summary>
55        /// HTML バリデーションを抑制するかどうかを取得または設定します。
56        /// </summary>
57        /// <value>
58        /// HTML バリデーションを抑制する場合は <see langword="true"/>、
59        /// それ以外の場合は <see langword="false"/>。
60        /// 既定値は <see langword="false"/> です。
61        /// </value>
62        [Themeable(false)]
63        [DefaultValue(false)]
64        [WebCategory("Behavior")]
65        [WebDescription("Description_SuppressHtmlValidation")]
66        public virtual bool SuppressHtmlValidation
67        {
68            get
69            {
70                return (bool)(ViewState[SuppressHtmlValidationKey] ?? false);
71            }
72
73            set
74            {
75                ViewState[SuppressHtmlValidationKey] = value;
76            }
77        }
78
79
80        /// <summary>
81        /// 指定した <see cref="HtmlTextWriter" /> オブジェクトに、
82        /// <see cref="Form" /> コントロールの属性をレンダリングします。
83        /// </summary>
84        /// <param name="writer">表示される内容を受け取る <see cref="HtmlTextWriter" />。</param>
85        /// <exception cref="InvalidOperationException">
86        /// <see cref="HtmlForm.DefaultButton" /> プロパティに設定されたコントロール ID の型は
87        /// <see cref="IButtonControl" /> ではありません。
88        /// </exception>
89        protected override void RenderAttributes(HtmlTextWriter writer)
90        {
91            const string NoValidateAttributeName = @"novalidate";
92
93            Attributes.Remove(NoValidateAttributeName);
94
95            // 親を呼び出す
96            base.RenderAttributes(writer);
97
98            if (SuppressHtmlValidation)
99            {
100                writer.WriteAttribute(NoValidateAttributeName, NoValidateAttributeName);
101            }
102        }
103
104        /// <summary>
105        /// <see cref="Form" /> コントロールの子コントロールを表示します。
106        /// </summary>
107        /// <param name="writer">表示される内容を受け取る <see cref="HtmlTextWriter" />。</param>
108        /// <exception cref="HttpException">
109        /// Web ページにはサーバー側の &lt;form&gt; タグが複数あります。
110        /// </exception>
111        protected override void RenderChildren(HtmlTextWriter writer)
112        {
113            // Page.BeginFormRender で処理が足りない箇所の保管
114            BeginFormRender();
115
116            // 親を呼び出す
117            base.RenderChildren(writer);
118        }
119
120        /// <summary>
121        /// <see cref="HtmlForm.DefaultButton"/> に設定したコントロールのネーミングコンテナが
122        /// <see cref="Page"/> では無い場合に正しく動作しないため、それを補完します。
123        /// </summary>
124        /// <remarks>
125        /// <see cref="HtmlForm.DefaultButton"/> は <see cref="Control.UniqueID"/>
126        /// を指定しないと <see cref="HtmlForm.RenderAttributes"/> 内で例外を投げるが、
127        /// <c>Page.BeginFormRender</c> 内では <see cref="HtmlForm.DefaultButton"/>
128        /// を <see cref="Control.ClientID"/> として期待しているためにその違いを吸収します。
129        /// </remarks>
130        private void BeginFormRender()
131        {
132            //// MEMO: Page.BeginFormRender の抜粋です
133
134            string defaultButton = DefaultButton;
135            if (string.IsNullOrEmpty(defaultButton))
136            {
137                return;
138            }
139
140            Control defaultButtonControl = Reflections.Control.FindControlFromPageIfNecessary(this, defaultButton);
141            if (defaultButtonControl == null)
142            {
143                return;
144            }
145
146            string defaultButtonClientID = defaultButtonControl.ClientID;
147
148            // 同じ場合 (ネーミングコンテナが Page の場合) は Page.BeginFormRender の実装で正しく動作している
149            if (defaultButton == defaultButtonClientID)
150            {
151                return;
152            }
153
154            Page page = Page;
155
156            // フォーカス設定用のクライアントスクリプトが使用出来ない場合
157            if (!Reflections.Page.GetClientSupportsJavaScript(page)
158                || !Reflections.Page.GetClientSupportsFocus(page))
159            {
160                return;
161            }
162
163            if (!string.IsNullOrEmpty(Reflections.Page.GetFocusedControlID(page)))
164            {
165                return;
166            }
167
168            if (Reflections.Page.GetFocusedControl(page) != null)
169            {
170                return;
171            }
172
173            if (!string.IsNullOrEmpty(Reflections.Page.GetValidatorInvalidControl(page)))
174            {
175                return;
176            }
177
178            if (!string.IsNullOrEmpty(Reflections.Page.GetLastFocusedControl(page)))
179            {
180                return;
181            }
182
183            if (!string.IsNullOrEmpty(DefaultFocus))
184            {
185                return;
186            }
187
188            Page.ClientScript.RegisterClientScriptResource(typeof(HtmlForm), "Focus.js");
189
190            const string AutoFocusScriptName = "Focus";
191            if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(HtmlForm), AutoFocusScriptName))
192            {
193                Reflections.Page.RegisterWebFormsScript(page);
194                Page.ClientScript.RegisterStartupScript(
195                    typeof(HtmlForm),
196                    AutoFocusScriptName,
197                    "WebForm_AutoFocus(" + ClientScriptUtility.CreateStringLiteral(defaultButtonClientID) + ");",
198                    true);
199            }
200
201            object scriptManager = Reflections.Page.GetScriptManager(page);
202            if (scriptManager != null)
203            {
204                Reflections.InternalScriptManager.SetFocusInternal(scriptManager, defaultButtonClientID);
205            }
206        }
207    }
208}
詳しい使い方は TracBrowser を参照してください。