728x90
index.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@page "/"
<div class="modal-footer">
<button type="button" class="btn btn-primary" @onclick=ShowHelloPopup>Hello Popup</button>
</div>
@code
{
private async Task ShowHelloPopup()
{
ModalDialogOptions options = new ModalDialogOptions()
{
BackgroundClickToClose = false,
};
await ModalDialog.ShowDialogAsync<HelloPopup>("Hello Popup", options);
}
}
|
cs |
HelloPopup.razor
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
|
@if (IsLoaded)
{
<EditForm EditContext=editContext OnValidSubmit="Submit">
<DataAnnotationsValidator />
<input type="text" id="name" @bind="StudentRequeset!.Name" @ref=NameFocus />
<button type="submit">Submit</button>
</EditForm>
}
@code
{
private Student? StudentRequeset { get; set; }
private ElementReference NameFocus;
private EditContext? editContext;
private bool IsLoaded { get; set; } = false;
protected override async Task OnInitializedAsync()
{
await Task.Delay(100);
StudentRequeset = new();
editContext = new(StudentRequeset);
IsLoaded = true;
}
private async Task Submit()
{
await ModalDialog.ShowMessageBoxAsync("Hello", "안녕하세요", MessageBoxButtons.OK);
}
protected override async void OnParametersSet()
{
await InvokeAsync(StateHasChanged);
await SetFocus();
}
private async Task SetFocus()
{
await NameFocus.FocusAsync();
}
public class Student
{
public string? Name { get; set; } = string.Empty;
}
}
|
cs |
1. @ref="NameFocus"
HTML 구성요소에 속성값을 넣거나 기능을 부여
https://learn.microsoft.com/ko-kr/dotnet/architecture/blazor-for-web-forms-developers/components
2. ElementReference 변수 선언
3. OnParametersSet() 이벤트에 Focus 메서 추가
html 페이지에 OnInitialized 또는 OnInitializedAsync 이벤트에서 설정 된 것들이 랜더링 된 이후
OnParametersSet 메서드 수행
Blazor 수명주기 : https://learn.microsoft.com/ko-kr/aspnet/core/blazor/components/lifecycle?view=aspnetcore-7.0
실행
기타 : Nuget에서 설치해야 하는 Popup 컨트롤 (LiquidTechnologies.Blazor.ModalDialog)
'.NET Blazor' 카테고리의 다른 글
blazor Callback parameter (and event call) (0) | 2023.01.17 |
---|
댓글