|
9 | 9 | namespace Blazor.WebForm.UI.ControlComponents |
10 | 10 | { |
11 | 11 | public abstract class MultipleSelectionListControlComponent<TControl> : ListControlComponent<TControl> |
12 | | - where TControl : ListControl, new() |
| 12 | + where TControl : MultipleSelectionListControl, new() |
13 | 13 | { |
14 | | - [Parameter] |
15 | | - public override string SelectedValue |
16 | | - { |
17 | | - get |
18 | | - { |
19 | | - return base.SelectedValue; |
20 | | - } |
21 | | - set |
22 | | - { |
23 | | - if (this.IsMultipleSelection && !string.IsNullOrEmpty(value)) |
24 | | - { |
25 | | - foreach (ListItem item in this.Control.Items) |
26 | | - { |
27 | | - if (item.Selected) |
28 | | - { |
29 | | - if (item.Value == value) |
30 | | - { |
31 | | - return; |
32 | | - } |
33 | | - break; |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - base.SelectedValue = value; |
38 | | - } |
39 | | - } |
| 14 | + private bool _hasBindSelectedValues; |
40 | 15 |
|
41 | 16 | [Parameter] |
42 | 17 | public string[] SelectedValues |
43 | 18 | { |
44 | 19 | get |
45 | 20 | { |
46 | | - List<string> list = new List<string>(); |
47 | | - foreach (ListItem item in this.Control.Items) |
48 | | - { |
49 | | - if (item.Selected) |
50 | | - { |
51 | | - list.Add(item.Value); |
52 | | - } |
53 | | - } |
54 | | - return list.ToArray(); |
| 21 | + return this.Control.SelectedValues; |
55 | 22 | } |
56 | 23 | set |
57 | 24 | { |
58 | | - bool autoPostBack = this.AutoPostBack; |
59 | | - if (autoPostBack) |
60 | | - { |
61 | | - this.AutoPostBack = false; |
62 | | - } |
63 | | - try |
| 25 | + if (_hasBindSelectedValues) |
64 | 26 | { |
65 | | - foreach (ListItem item in this.Control.Items) |
66 | | - { |
67 | | - item.Selected = (Array.IndexOf(value, item.Value) != -1); |
68 | | - } |
| 27 | + ((IBindingMultipleSelectionListControl)this.Control).SelectedValues = value; |
69 | 28 | } |
70 | | - finally |
| 29 | + else |
71 | 30 | { |
72 | | - this.AutoPostBack = autoPostBack; |
| 31 | + this.Control.SelectedValues = value; |
73 | 32 | } |
74 | 33 | } |
75 | 34 | } |
76 | 35 |
|
77 | | - protected abstract bool IsMultipleSelection { get; } |
78 | | - |
79 | 36 | protected override void OnInitialized() |
80 | 37 | { |
81 | 38 | base.OnInitialized(); |
82 | 39 | if (this.HasPropertyBindEvent<string[]>(nameof(this.SelectedValues))) |
83 | 40 | { |
| 41 | + _hasBindSelectedValues = true; |
84 | 42 | this.Control.AutoPostBack = true; |
85 | 43 | this.SetBindEventProperty(nameof(this.OnSelectedIndexChanged), this.BindSelectedIndexChanged, i => this.Control.SelectedIndexChanged += i, i => this.Control.SelectedIndexChanged -= i); |
86 | 44 | this.SetBindEventProperty(OnDataBindingSelectedIndexChanged, this.BindSelectedIndexChanged, i => ((IBindingListControl)this.Control).DataBindingSelectedIndexChanged += i, i => ((IBindingListControl)this.Control).DataBindingSelectedIndexChanged -= i); |
|
0 commit comments