チェンジセット 1604 (default)


以下の違いを無視:
日時:
2023/09/08 2:22:44 (13ヵ月前)
更新者:
hizuya@…
ログメッセージ:
  • GridView に SelectedValues プロパティを追加し、コレクションでバインドできるように機能追加。
  • FormView 関係の動作サンプルを追加。
場所:
framework/trunk
ファイル:
8個の追加
2個の更新

凡例:

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

    r1603 r1604  
    6666    <Content Include="UI\WebControls\FormViewPage.aspx" /> 
    6767    <Content Include="UI\WebControls\ObjectDataSourceGridViewPage.aspx" /> 
     68    <Content Include="UI\WebControls\FormViewBindPage.aspx" /> 
     69    <Content Include="UI\WebControls\SelectFieldBoundInnerGridViewPage.aspx" /> 
    6870    <Content Include="Web.config" /> 
    6971  </ItemGroup> 
     
    341343    <Compile Include="UI\WebControls\RowStyleFieldPage.aspx.designer.cs"> 
    342344      <DependentUpon>RowStyleFieldPage.aspx</DependentUpon> 
     345    </Compile> 
     346    <Compile Include="UI\WebControls\FormViewBindPage.aspx.cs"> 
     347      <DependentUpon>FormViewBindPage.aspx</DependentUpon> 
     348      <SubType>ASPXCodeBehind</SubType> 
     349    </Compile> 
     350    <Compile Include="UI\WebControls\FormViewBindPage.aspx.designer.cs"> 
     351      <DependentUpon>FormViewBindPage.aspx</DependentUpon> 
     352    </Compile> 
     353    <Compile Include="UI\WebControls\SelectFieldBoundInnerGridViewPage.aspx.cs"> 
     354      <DependentUpon>SelectFieldBoundInnerGridViewPage.aspx</DependentUpon> 
     355      <SubType>ASPXCodeBehind</SubType> 
     356    </Compile> 
     357    <Compile Include="UI\WebControls\SelectFieldBoundInnerGridViewPage.aspx.designer.cs"> 
     358      <DependentUpon>SelectFieldBoundInnerGridViewPage.aspx</DependentUpon> 
    343359    </Compile> 
    344360    <Compile Include="UI\WebControls\StringLengthValidatorPage.aspx.cs"> 
  • framework/trunk/WebLibrary/Sources/UI/WebControls/GridView.cs

    r1602 r1604  
    116116 
    117117        /// <summary> 
     118        /// 設定された選択済の値。 
     119        /// </summary> 
     120        private object selectedValues; 
     121 
     122        /// <summary> 
    118123        /// 最後に作成したフィールドのコレクション。 
    119124        /// </summary> 
     
    175180            get 
    176181            { 
    177                 int targetIndex = -1; 
    178                 DataControlFieldCollection columns = Columns; 
    179                 for (int i = 0; i < columns.Count; i++) 
    180                 { 
    181                     SelectField selectField = columns[i] as SelectField; 
    182                     if (selectField != null) 
    183                     { 
    184                         targetIndex = i; 
    185                         break; 
    186                     } 
    187                 } 
    188  
     182                int targetIndex = GetSelectFieldIndex(); 
    189183                if (targetIndex == -1) 
    190184                { 
     
    192186                } 
    193187 
    194                 DataKeyArray allKeys = DataKeys; 
    195188                ArrayList selectedKeys = new ArrayList(); 
    196                 foreach (GridViewRow row in Rows) 
    197                 { 
    198                     if (row.RowType != DataControlRowType.DataRow) 
    199                     { 
    200                         continue; 
    201                     } 
    202  
    203                     ICheckBoxControl checkBox 
    204                         = SelectField.GetCheckBoxControl((DataControlFieldCell)row.Cells[targetIndex]); 
    205                     if (checkBox == null || !checkBox.Checked) 
    206                     { 
    207                         continue; 
    208                     } 
    209  
    210                     selectedKeys.Add(allKeys[row.RowIndex]); 
     189                foreach (DataKey dataKey in GetSelectedDataKeyEnumerable(targetIndex)) 
     190                { 
     191                    selectedKeys.Add(dataKey); 
    211192                } 
    212193 
     
    280261 
    281262                return selectedDataKeysArray; 
     263            } 
     264        } 
     265 
     266        /// <summary> 
     267        /// <see cref="SelectField"/> を使用して選択された行を表す 
     268        /// <see cref="DataKey"/> のコレクションを返します。 
     269        /// </summary> 
     270        /// <value> 
     271        /// <see cref="DataKey"/> のコレクション。 
     272        /// </value> 
     273        /// <remarks> 
     274        /// <see cref="SelectField"/> は、カラムの最初から検索していき、 
     275        /// 始めに見つかった <see cref="SelectField"/> を対象にします。 
     276        /// 複数の <see cref="SelectField"/> を指定している場合、 
     277        /// 2 個目以降の <see cref="SelectField"/> は無視されます。 
     278        /// </remarks> 
     279        [Bindable(true, BindingDirection.TwoWay)] 
     280        [Themeable(false)] 
     281        [DefaultValue(null)] 
     282        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
     283        [WebCategory("Behavior")] 
     284        [WebDescription("Description_GridView_SelectedValues")] 
     285        public object SelectedValues 
     286        { 
     287            get 
     288            { 
     289                int targetIndex = GetSelectFieldIndex(); 
     290                if (targetIndex == -1) 
     291                { 
     292                    return null; 
     293                } 
     294 
     295                Type valueType = AnalyzeDataKeyFirstValueType(); 
     296                List<object> values = new List<object>(); 
     297                foreach (DataKey dataKey in GetSelectedDataKeyEnumerable(targetIndex)) 
     298                { 
     299                    values.Add(dataKey.Value); 
     300                } 
     301 
     302                Array array = Array.CreateInstance(valueType, values.Count); 
     303                for (int i = 0; i < values.Count; i++) 
     304                { 
     305                    // ReSharper disable once ExceptionNotDocumented 
     306                    array.SetValue(values[i], i); 
     307                } 
     308 
     309                return array; 
     310            } 
     311 
     312            set 
     313            { 
     314                if (Rows.Count != 0) 
     315                { 
     316                    UpdateSelectFieldState(); 
     317                } 
     318 
     319                // データバインドされたときのために常に保持する 
     320                selectedValues = value; 
    282321            } 
    283322        } 
     
    514553            { 
    515554                base.EnsureDataBound(); 
     555            } 
     556        } 
     557 
     558        /// <summary> 
     559        /// 指定したデータ ソースを <see cref="GridView" /> コントロールにバインドします。 
     560        /// </summary> 
     561        /// <param name="data">データ ソースを格納している <see cref="IEnumerable" />。</param> 
     562        protected override void PerformDataBinding(IEnumerable data) 
     563        { 
     564            // 親を呼び出す 
     565            base.PerformDataBinding(data); 
     566 
     567            // SelectField の状態を更新 
     568            if (selectedValues != null) 
     569            { 
     570                UpdateSelectFieldState(); 
     571                selectedValues = null; 
    516572            } 
    517573        } 
     
    849905            return row; 
    850906        } 
     907 
     908        /// <summary> 
     909        /// カラムの先頭から最初に見つかった <see cref="SelectField"/> 
     910        /// のインデックス番号を返します。 
     911        /// </summary> 
     912        /// <returns> 
     913        /// <see cref="System.Web.UI.WebControls.GridView.Columns"/> 内のインデックス。 
     914        /// 見付からない場合は <c>-1</c>。 
     915        /// </returns> 
     916        private int GetSelectFieldIndex() 
     917        { 
     918            DataControlFieldCollection columns = Columns; 
     919            for (int i = 0; i < columns.Count; i++) 
     920            { 
     921                SelectField selectField = columns[i] as SelectField; 
     922                if (selectField != null) 
     923                { 
     924                    return i; 
     925                } 
     926            } 
     927 
     928            return -1; 
     929        } 
     930 
     931        /// <summary> 
     932        /// データキーの最初のフィールドを検査し、全ての最初のフィールドの値を格納可能な型を返します。 
     933        /// </summary> 
     934        /// <returns> 
     935        /// 最初のフィールドの値を格納可能な型。 
     936        /// </returns> 
     937        private Type AnalyzeDataKeyFirstValueType() 
     938        { 
     939            DataKeyArray keys = DataKeys; 
     940            if (keys.Count == 0) 
     941            { 
     942                return typeof(object); 
     943            } 
     944 
     945            Type type = null; 
     946            foreach (DataKey dataKey in keys) 
     947            { 
     948                object value = dataKey.Value; 
     949                if (value == null) 
     950                { 
     951                    continue; 
     952                } 
     953 
     954                Type currentType = value.GetType(); 
     955 
     956                // 値の型が object の場合は、object 型しか有り得ない 
     957                if (currentType == typeof(object)) 
     958                { 
     959                    return currentType; 
     960                } 
     961 
     962                if (type == null) 
     963                { 
     964                    type = currentType; 
     965                    continue; 
     966                } 
     967 
     968                while (!type.IsAssignableFrom(currentType)) 
     969                { 
     970                    // 継承元の型に変更 
     971                    type = type.BaseType; 
     972 
     973                    // 継承元の型が object の場合は、object 型しか有り得ない 
     974                    if (type == null || type == typeof(object)) 
     975                    { 
     976                        return typeof(object); 
     977                    } 
     978                } 
     979            } 
     980 
     981            return type ?? typeof(object); 
     982        } 
     983 
     984        /// <summary> 
     985        /// 指定した位置にある <see cref="SelectField"/> を使用して選択された行の 
     986        /// <see cref="DataKey"/> を列挙する列挙子を返します。 
     987        /// </summary> 
     988        /// <param name="selectFieldIndex"><see cref="SelectField"/> のインデックス。</param> 
     989        /// <returns> 
     990        /// 列挙子。 
     991        /// </returns> 
     992        private IEnumerable<DataKey> GetSelectedDataKeyEnumerable(int selectFieldIndex) 
     993        { 
     994            DataKeyArray allKeys = DataKeys; 
     995            foreach (GridViewRow row in Rows) 
     996            { 
     997                if (row.RowType != DataControlRowType.DataRow) 
     998                { 
     999                    continue; 
     1000                } 
     1001 
     1002                ICheckBoxControl checkBox 
     1003                    = SelectField.GetCheckBoxControl((DataControlFieldCell)row.Cells[selectFieldIndex]); 
     1004                if (checkBox == null || !checkBox.Checked) 
     1005                { 
     1006                    continue; 
     1007                } 
     1008 
     1009                yield return allKeys[row.RowIndex]; 
     1010            } 
     1011        } 
     1012 
     1013        /// <summary> 
     1014        /// <see cref="SelectField"/> の状態を指定されている値で更新します。 
     1015        /// </summary> 
     1016        private void UpdateSelectFieldState() 
     1017        { 
     1018            int targetIndex = GetSelectFieldIndex(); 
     1019            if (targetIndex == -1) 
     1020            { 
     1021                return; 
     1022            } 
     1023 
     1024            // 値の一覧をハッシュセットに変換 
     1025            IEnumerable enumerable = (IEnumerable)selectedValues; 
     1026            HashSet<object> valueMap = new HashSet<object>(); 
     1027            if (enumerable != null) 
     1028            { 
     1029                foreach (object obj in enumerable) 
     1030                { 
     1031                    valueMap.Add(obj); 
     1032                } 
     1033            } 
     1034 
     1035            DataKeyArray allKeys = DataKeys; 
     1036            foreach (GridViewRow row in Rows) 
     1037            { 
     1038                if (row.RowType != DataControlRowType.DataRow) 
     1039                { 
     1040                    continue; 
     1041                } 
     1042 
     1043 
     1044                ICheckBoxControl checkBox 
     1045                    = SelectField.GetCheckBoxControl((DataControlFieldCell)row.Cells[targetIndex]); 
     1046                if (checkBox == null) 
     1047                { 
     1048                    continue; 
     1049                } 
     1050 
     1051                checkBox.Checked = valueMap.Contains(allKeys[row.RowIndex].Value); 
     1052            } 
     1053        } 
    8511054    } 
    8521055} 
詳しい使い方は TracChangeset を参照してください。