Skip to content

Commit 4bb7df5

Browse files
authored
Simple ViewState for compatibility (#93)
* Possible ViewState implementation? (#93) * Added a test to verify ViewState can be stored and retrieved
1 parent 4938599 commit 4bb7df5

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@inherits TestComponentBase
2+
3+
<Fixture Test="FirstTest" Setup="Setup">
4+
<ComponentUnderTest>
5+
<Repeater @ref="MyRepeater" Items="Widget.SimpleWidgetList"
6+
ItemType="Widget" OnUnload="OnRepeaterInit"
7+
Context="Item">
8+
<ItemTemplate><span>@Item.Name</span></ItemTemplate>
9+
<SeparatorTemplate><hr /></SeparatorTemplate>
10+
</Repeater>
11+
</ComponentUnderTest>
12+
</Fixture>
13+
14+
15+
@code {
16+
17+
public Repeater<Widget> MyRepeater { get; set; }
18+
int randomNumber;
19+
const string ViewStateKey = "RandomNumber";
20+
21+
void Setup()
22+
{
23+
24+
randomNumber = new Random().Next(100, 1000000);
25+
26+
}
27+
28+
void OnRepeaterInit(EventArgs args)
29+
{
30+
31+
MyRepeater.ViewState.Add(ViewStateKey, randomNumber);
32+
33+
}
34+
35+
void FirstTest()
36+
{
37+
38+
var cut = GetComponentUnderTest();
39+
40+
MyRepeater.ViewState[ViewStateKey].ShouldBe(randomNumber);
41+
42+
}
43+
44+
45+
}

src/BlazorWebFormsComponents/BaseWebFormsComponent.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public abstract class BaseWebFormsComponent : ComponentBase, IAsyncDisposable
2020
public string ID { get; set; }
2121

2222
/// <summary>
23-
/// 🚨🚨 ViewState is not available in Blazor 🚨🚨
23+
/// While ViewState is supported by this library, this parameter does nothing
2424
/// </summary>
25-
[Parameter(), Obsolete("ViewState is not available in Blazor")]
25+
[Parameter(), Obsolete("ViewState is supported, but EnableViewState does nothing")]
2626
public bool EnableViewState { get; set; }
2727

2828
/// <summary>
@@ -63,6 +63,13 @@ public abstract class BaseWebFormsComponent : ComponentBase, IAsyncDisposable
6363
[Parameter]
6464
public short TabIndex { get; set; }
6565

66+
/// <summary>
67+
/// ViewState is supported for compatibility with those components and pages that add and retrieve items from ViewState.!-- It is not binary compatible, but is syntax compatible
68+
/// </summary>
69+
/// <value></value>
70+
[Obsolete("ViewState is supported for compatibility and is discouraged for future use")]
71+
public Dictionary<string,object> ViewState { get; } = new Dictionary<string, object>();
72+
6673
/// <summary>
6774
/// Is the content of this component rendered and visible to your users?
6875
/// </summary>

0 commit comments

Comments
 (0)