チェンジセット 1635 (default)


以下の違いを無視:
日時:
2024/02/13 1:10:48 (8ヵ月前)
更新者:
hizuya@…
ログメッセージ:
  • FileUpload コントロールに AutoPostBack の機能を追加。
場所:
framework/trunk
ファイル:
3個の追加
3個の更新

凡例:

未変更
追加
削除
  • framework/trunk/WebApplication/WebApplication.csproj

    r1633 r1635  
    6969    <Content Include="UI\WebControls\CompareValidatorPage.aspx" /> 
    7070    <Content Include="UI\WebControls\ControlPropertyLinkageConnectorPage.aspx" /> 
     71    <Content Include="UI\WebControls\FileUploadPage.aspx" /> 
    7172    <Content Include="UI\WebControls\FormViewBindUserControl1.ascx" /> 
    7273    <Content Include="UI\WebControls\FormViewPage.aspx" /> 
     
    298299    <Compile Include="UI\WebControls\DialogPage.aspx.designer.cs"> 
    299300      <DependentUpon>DialogPage.aspx</DependentUpon> 
     301    </Compile> 
     302    <Compile Include="UI\WebControls\FileUploadPage.aspx.cs"> 
     303      <DependentUpon>FileUploadPage.aspx</DependentUpon> 
     304      <SubType>ASPXCodeBehind</SubType> 
     305    </Compile> 
     306    <Compile Include="UI\WebControls\FileUploadPage.aspx.designer.cs"> 
     307      <DependentUpon>FileUploadPage.aspx</DependentUpon> 
    300308    </Compile> 
    301309    <Compile Include="UI\WebControls\FormViewBindUserControl1.ascx.cs"> 
  • framework/trunk/WebLibrary/Sources/UI/ClientScriptUtility.cs

    r1543 r1635  
    554554 
    555555        /// <summary> 
    556         /// WebForm 用のスクリプトを登録します。 
     556        /// クライアントスクリプトからポストバック可能にするスクリプトを登録します。 
    557557        /// </summary> 
    558558        /// <param name="page"> 
     
    565565 
    566566        /// <summary> 
    567         /// WebForm 用のスクリプトを登録します。 
     567        /// フォーカスを制御するスクリプトを登録します。 
    568568        /// </summary> 
    569569        /// <param name="page"> 
  • framework/trunk/WebLibrary/Sources/UI/WebControls/FileUpload.cs

    r864 r1635  
    2323namespace FCSoft.SilverFrost.Framework.Web.UI.WebControls 
    2424{ 
     25    using System; 
     26    using System.Collections.Specialized; 
     27    using System.ComponentModel; 
     28    using System.Web; 
     29    using System.Web.UI; 
     30 
     31 
    2532    /// <summary> 
    2633    /// ユーザーがファイルを選択してサーバーにアップロードするための 
     
    2936    [WebDescription("Description_FileUpload")] 
    3037    [ControlInitializerTarget] 
     38    [SupportsEventValidation] 
    3139    public class FileUpload : 
    32         System.Web.UI.WebControls.FileUpload 
     40        System.Web.UI.WebControls.FileUpload, 
     41        IPostBackDataHandler 
    3342    { 
    3443        /// <summary> 
     44        /// <see cref="AutoPostBack"/> プロパティ用のキー。 
     45        /// </summary> 
     46        private const string AutoPostBackKey = "AutoPostBack"; 
     47 
     48        /// <summary> 
     49        /// <see cref="CausesValidation"/> プロパティ用のキー。 
     50        /// </summary> 
     51        private const string CausesValidationKey = "CausesValidation"; 
     52 
     53        /// <summary> 
     54        /// <see cref="ValidationGroup"/> プロパティ用のキー。 
     55        /// </summary> 
     56        private const string ValidationGroupKey = "ValidationGroup"; 
     57 
     58 
     59        /// <summary> 
     60        /// <see cref="FileSelected"/> イベントハンドラの識別子。 
     61        /// </summary> 
     62        private static readonly object EventFileSelectedKey = new object(); 
     63 
     64 
     65        /// <summary> 
    3566        /// インスタンスを作成します。 
    3667        /// </summary> 
     
    3869        { 
    3970            ControlInitializer.ApplyProperties(this); 
     71        } 
     72 
     73 
     74        /// <summary> 
     75        /// ファイルが選択されサーバーへのポスト間で変更された場合に発生します。 
     76        /// </summary> 
     77        [WebCategory("Action")] 
     78        [WebDescription("Description_FileSelected")] 
     79        public event EventHandler FileSelected 
     80        { 
     81            add 
     82            { 
     83                Events.AddHandler(EventFileSelectedKey, value); 
     84            } 
     85 
     86            remove 
     87            { 
     88                Events.RemoveHandler(EventFileSelectedKey, value); 
     89            } 
     90        } 
     91 
     92 
     93        /// <summary> 
     94        /// <see cref="FileUpload"/> コントロールがフォーカスを失ったときに、 
     95        /// サーバーへの自動ポストバックが発生するかどうかを示す値を取得または設定します。 
     96        /// </summary> 
     97        /// <value> 
     98        /// サーバーへの自動ポストバックが発生する場合は <see langword="true"/>。 
     99        /// それ以外の場合は <see langword="false"/>。 
     100        /// </value> 
     101        [DefaultValue(false)] 
     102        [Themeable(false)] 
     103        [WebCategory("Behavior")] 
     104        [WebDescription("Description_AutoPostBack")] 
     105        public virtual bool AutoPostBack 
     106        { 
     107            get 
     108            { 
     109                return (bool)(ViewState[AutoPostBackKey] ?? false); 
     110            } 
     111 
     112            set 
     113            { 
     114                ViewState[AutoPostBackKey] = value; 
     115            } 
     116        } 
     117 
     118        /// <summary> 
     119        /// <see cref="FileUpload"/> コントロールによりポストバックされたときに 
     120        /// 検証を実行するかどうかを示す値を取得または設定します。 
     121        /// </summary> 
     122        /// <value> 
     123        /// <see cref="FileUpload"/> コントロールによりポストバックされたときに 
     124        /// 検証を実行する場合は <see langword="true"/>。 
     125        /// それ以外の場合は <see langword="false"/>。 
     126        /// 既定値は <see langword="true"/> です。 
     127        /// </value> 
     128        [Themeable(false)] 
     129        [DefaultValue(true)] 
     130        [WebCategory("Behavior")] 
     131        [WebDescription("Description_CausesValidation")] 
     132        public virtual bool CausesValidation 
     133        { 
     134            get 
     135            { 
     136                return (bool)(ViewState[CausesValidationKey] ?? true); 
     137            } 
     138 
     139            set 
     140            { 
     141                ViewState[CausesValidationKey] = value; 
     142            } 
     143        } 
     144 
     145        /// <summary> 
     146        /// サーバーへのポストバック時に、<see cref="FileUpload"/> コントロールによって発生する 
     147        /// 検証の対象となるコントロールのグループを取得または設定します。 
     148        /// </summary> 
     149        /// <value> 
     150        /// サーバーへのポストバック時に、<see cref="FileUpload"/> コントロールによって発生する 
     151        /// 検証の対象となるコントロールのグループ。 
     152        /// 既定値は空の文字列 ("") です。 
     153        /// </value> 
     154        [Themeable(false)] 
     155        [DefaultValue("")] 
     156        [WebCategory("Behavior")] 
     157        [WebDescription("Description_ValidationGroup")] 
     158        public virtual string ValidationGroup 
     159        { 
     160            get 
     161            { 
     162                return (string)(ViewState[ValidationGroupKey] ?? string.Empty); 
     163            } 
     164 
     165            set 
     166            { 
     167                ViewState[ValidationGroupKey] = value; 
     168            } 
     169        } 
     170 
     171 
     172        #region IPostBackDataHandler メンバ 
     173 
     174        /// <summary> 
     175        /// コントロールのポストバック データを処理します。 
     176        /// </summary> 
     177        /// <param name="postDataKey"> 
     178        /// コントロールのキー識別子。 
     179        /// </param> 
     180        /// <param name="postCollection"> 
     181        /// 受信する名前と値すべてのコレクション。 
     182        /// </param> 
     183        /// <returns> 
     184        /// ポストバックの結果、サーバー コントロールの状態が変化する場合は <see langword="true"/>。 
     185        /// それ以外の場合は <see langword="false"/>。 
     186        /// </returns> 
     187        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) 
     188        { 
     189            return LoadPostData(postDataKey, postCollection); 
     190        } 
     191 
     192        /// <summary> 
     193        /// コントロールの状態が変化したことを ASP.NET アプリケーションに通知するために、 
     194        /// サーバー コントロールにシグナルを送信します。 
     195        /// </summary> 
     196        void IPostBackDataHandler.RaisePostDataChangedEvent() 
     197        { 
     198            RaisePostDataChangedEvent(); 
     199        } 
     200 
     201        #endregion 
     202 
     203        /// <summary> 
     204        /// コントロールのポストバック データを処理します。 
     205        /// </summary> 
     206        /// <param name="postDataKey"> 
     207        /// コントロールのキー識別子。 
     208        /// </param> 
     209        /// <param name="postCollection"> 
     210        /// 受信する名前と値すべてのコレクション。 
     211        /// </param> 
     212        /// <returns> 
     213        /// ポストバックの結果、サーバー コントロールの状態が変化する場合は <see langword="true"/>。 
     214        /// それ以外の場合は <see langword="false"/>。 
     215        /// </returns> 
     216        protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) 
     217        { 
     218            if (Page != null) 
     219            { 
     220                Page.ClientScript.ValidateEvent(postDataKey, string.Empty); 
     221            } 
     222 
     223            // ファイルがポストされていれば true を返す 
     224            HttpPostedFile postedFile = PostedFile; 
     225            return postedFile != null 
     226                && (!string.IsNullOrEmpty(postedFile.FileName) || postedFile.ContentLength > 0); 
     227        } 
     228 
     229        /// <summary> 
     230        /// コントロールの状態が変化したことを ASP.NET アプリケーションに通知するために、 
     231        /// サーバー コントロールにシグナルを送信します。 
     232        /// </summary> 
     233        protected virtual void RaisePostDataChangedEvent() 
     234        { 
     235            if (AutoPostBack && !Page.IsPostBackEventControlRegistered) 
     236            { 
     237                Page.AutoPostBackControl = this; 
     238 
     239                if (CausesValidation) 
     240                { 
     241                    Page.Validate(ValidationGroup); 
     242                } 
     243            } 
     244 
     245            OnFileSelected(EventArgs.Empty); 
     246        } 
     247 
     248        /// <summary> 
     249        /// <see cref="FileSelected"/> イベントを発生させます。 
     250        /// </summary> 
     251        /// <param name="e"> 
     252        /// イベント データを格納している <see cref="EventArgs"/>。 
     253        /// </param> 
     254        protected virtual void OnFileSelected(EventArgs e) 
     255        { 
     256            EventHandler textChangedHandler = (EventHandler)Events[EventFileSelectedKey]; 
     257            if (textChangedHandler != null) 
     258            { 
     259                textChangedHandler(this, e); 
     260            } 
     261        } 
     262 
     263        /// <summary> 
     264        /// このコントロールがレンダリングされる直前に呼び出されるイベントハンドラです。 
     265        /// </summary> 
     266        /// <param name="e"> 
     267        /// イベント データを格納している <see cref="EventArgs"/>。 
     268        /// </param> 
     269        protected override void OnPreRender(EventArgs e) 
     270        { 
     271            // 親を呼び出す 
     272            base.OnPreRender(e); 
     273 
     274            Page page = Page; 
     275            if (page == null) 
     276            { 
     277                return; 
     278            } 
     279 
     280            bool enabled = IsEnabled; 
     281 
     282            if (enabled) 
     283            { 
     284                // ReSharper disable once ExceptionNotDocumented 
     285                page.RegisterRequiresPostBack(this); 
     286 
     287                if (AutoPostBack) 
     288                { 
     289                    ClientScriptUtility.RegisterPostBackScript(page); 
     290                    ClientScriptUtility.RegisterFocusScript(page); 
     291 
     292                    // バリデーションを行う場合 
     293                    if (CausesValidation && page.GetValidators(ValidationGroup).Count > 0) 
     294                    { 
     295                        ClientScriptUtility.RegisterWebFormsScript(page); 
     296                    } 
     297                } 
     298            } 
     299        } 
     300 
     301        /// <summary> 
     302        /// 指定した <see cref="HtmlTextWriter"/> に表示する必要のある HTML 属性およびスタイルを追加します。 
     303        /// </summary> 
     304        /// <param name="writer"> 
     305        /// クライアントに HTML のコンテンツを表示する出力ストリーム。 
     306        /// </param> 
     307        /// <exception cref="HttpException"> 
     308        /// 実行時に、指定されたサーバー コントロールが <see cref="T:System.Web.UI.HtmlControls.HtmlForm" /> 
     309        /// サーバー コントロールの開始タグと終了タグの間に含まれていません。 
     310        /// </exception> 
     311        protected override void AddAttributesToRender(HtmlTextWriter writer) 
     312        { 
     313            Page page = Page; 
     314            if (page != null) 
     315            { 
     316                page.VerifyRenderingInServerForm(this); 
     317            } 
     318 
     319            if (page != null) 
     320            { 
     321                if (AutoPostBack) 
     322                { 
     323                    string onChange = null; 
     324                    if (HasAttributes) 
     325                    { 
     326                        onChange = Attributes["onchange"]; 
     327                        if (onChange != null) 
     328                        { 
     329                            onChange = ClientScriptUtility.EnsureEndWithSemiColon(onChange); 
     330                            Attributes.Remove("onchange"); 
     331                        } 
     332                    } 
     333 
     334                    PostBackOptions options = new PostBackOptions(this, string.Empty); 
     335                    if (CausesValidation) 
     336                    { 
     337                        options.PerformValidation = true; 
     338                        options.ValidationGroup = ValidationGroup; 
     339                    } 
     340 
     341                    if (page.Form != null) 
     342                    { 
     343                        options.AutoPostBack = true; 
     344                    } 
     345 
     346                    writer.AddAttribute( 
     347                        HtmlTextWriterAttribute.Onchange, 
     348                        ClientScriptUtility.MergeScript(onChange, page.ClientScript.GetPostBackEventReference(options, true), false)); 
     349                } 
     350                else 
     351                { 
     352                    page.ClientScript.RegisterForEventValidation(UniqueID, string.Empty); 
     353                } 
     354            } 
     355 
     356            if (Enabled && !IsEnabled && SupportsDisabledAttribute) 
     357            { 
     358                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled"); 
     359            } 
     360 
     361            // 親を呼び出す 
     362            base.AddAttributesToRender(writer); 
    40363        } 
    41364    } 
詳しい使い方は TracChangeset を参照してください。