Repository navigation
Expand file tree
/
Copy pathIndex.razor
More file actions
103 lines (82 loc) · 2.7 KB
/
Copy pathIndex.razor
File metadata and controls
103 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
@page "/"
@inject ISessionStorageService sessionStorage
<h1>Blazored SessionStorage Sample</h1>
<hr class="mb-5" />
<div class="row mb-5">
<div class="col-md-4">
<h5>Add Item to session storage</h5>
<div class="input-group">
<input class="form-control" type="text" placeholder="Enter a value" @bind="Name" />
<div class="input-group-append">
<button class="btn btn-primary" @onclick="SaveName">Save</button>
</div>
</div>
</div>
<div class="col-md-4">
<h5>Remove item from session storage</h5>
<button class="btn btn-primary" @onclick="RemoveName">Remove Item</button>
</div>
<div class="col-md-4">
<h5>Clear session storage</h5>
<button class="btn btn-primary" @onclick="ClearSessionStorage">Clear All</button>
</div>
</div>
<div class="row">
<div class="col-md-4">
<h5>Value Read from session storage</h5>
@NameFromSessionStorage
</div>
<div class="col-md-4">
<h5>Items in session storage</h5>
@ItemsInSessionStorage
</div>
</div>
@code {
string NameFromSessionStorage { get; set; }
int ItemsInSessionStorage { get; set; }
string Name { get; set; }
protected override async Task OnInitializedAsync()
{
await GetNameFromSessionStorage();
await GetSessionStorageLength();
sessionStorage.Changed += (sender, e) =>
{
Console.WriteLine($"Value for key {e.Key} changed from {e.OldValue} to {e.NewValue}");
};
}
async Task SaveName()
{
await sessionStorage.SetItemAsync("name", Name);
await GetNameFromSessionStorage();
await GetSessionStorageLength();
Name = "";
}
async Task GetNameFromSessionStorage()
{
NameFromSessionStorage = await sessionStorage.GetItemAsync<string>("name");
if (string.IsNullOrEmpty(NameFromSessionStorage))
{
NameFromSessionStorage = "Nothing Saved";
}
}
async Task RemoveName()
{
await sessionStorage.RemoveItemAsync("name");
await GetNameFromSessionStorage();
await GetSessionStorageLength();
}
async Task ClearSessionStorage()
{
Console.WriteLine("Calling Clear...");
await sessionStorage.ClearAsync();
Console.WriteLine("Getting name from session storage...");
await GetNameFromSessionStorage();
Console.WriteLine("Calling Get Length...");
await GetSessionStorageLength();
}
async Task GetSessionStorageLength()
{
Console.WriteLine(await sessionStorage.LengthAsync());
ItemsInSessionStorage = await sessionStorage.LengthAsync();
}
}