(Sorry in advance for not formatting source code, my browser locks up when I try to add styling to any text block.)
Hello,
I would like to add a generic dialog box (As a component, probably) to my application where I can call it and specify only the header text and the body text. All other aspects/styling/logic will be defined in the component itself.
So what I want is something similar to a MessageBox on Winforms. What I want to avoid is to need to have an <SfDialog> component on every one of my pages/components that need it.
I've created such a component (By default Visible="false"), and I pass it the title and body text in a ShowDialog() method I created:
public async Task ShowDialog(string title, string message) {
this._title = title;
this._message = message;
await this._dlgMessage.Show();
}
My dialog is defined like this:
<SfDialog @ref="@_dlgMessage" Width="50%" AllowDragging="true" IsModal="true" ShowCloseIcon="true" Visible="false">
<DialogTemplates>
<Header>@_title</Header>
<Content>
@_message
</Content>
</DialogTemplates>
<DialogButtons>
<DialogButton Content="Ok" IsPrimary="true" OnClick="OnDlgMessageOkClick"></DialogButton>
</DialogButtons>
</SfDialog>
So far, so good... But when I call my ShowDialog() method, the _message and _title contents are blank/null/empty. so I get an empty dialog box with a button.
Then I click the button to dismiss the dialog box (which calls "this._dlgMessage.CloseDialog(null);"), then the real contents flash on the screen right as the dialog is closed.
If I call the dialog show method again with different values, the _previous_ values are shown and process repeats itself.
It's like the system is not binding the values as the dialog is shown, only when it's being closed.
Also, all the SfDialog examples show using a @bind-Visible="@Visible" mechanism for hiding and showing the dialog, but that does not work for me at all, only calling the Show()/ShowDialog() and CloseDialog() work for showing and hiding the dialog box.
Is there a better way to go about this?
|
<button class="e-btn" @onclick="@OnBtnClick1">Dialog 1</button>
<button class="e-btn" @onclick="@OnBtnClick2">Dialog 2</button>
<SfDialog @ref ="dlg" Width="50%" AllowDragging="true" IsModal="true" ShowCloseIcon="true" Visible="false">
<DialogTemplates>
<Header>@title</Header>
<Content>
@message
</Content>
</DialogTemplates>
<DialogButtons>
<DialogButton Content="Ok" IsPrimary="true" OnClick="@close"></DialogButton>
</DialogButtons>
</SfDialog>
@code {
SfDialog dlg;
private string title { get; set; }
private string message { get; set; }
public async Task ShowDialog(string title, string message)
{
this.title = title;
this.message = message;
await this.dlg.ShowAsync();
}
public async Task OnBtnClick1()
{
await this.ShowDialog("Dialog Header1", "Content1");
}
public async Task OnBtnClick2()
{
await this.ShowDialog("Dialog Header2", "Content2");
}
public async Task close()
{
await this.dlg.HideAsync();
}
}
} |
Hi Vinitha, thank you for the answer. This is not the use case I have: I have the
So in the case that you mention, where the
Let me ask the question of the fundamental problem I'm trying to solve: Is there a way to define a dialog only once in my application (layout, css, buttons, etc) and then reuse that as needed from all other pages/components? All I'm trying to do is have a consistent pop up dialog for displaying messages to the user from any page/component in my application. It does not need to be implemented the way I'm trying to, I just need it to work.
|
@using Syncfusion.Blazor.Popups
<CascadingValue Value="@this">
. . .
. . .
<SfDialog @ref="dlg" Width="50%" AllowDragging="true" IsModal="true" ShowCloseIcon="true" Visible="false">
<DialogTemplates>
<Header>@title</Header>
<Content>
@message
</Content>
</DialogTemplates>
<DialogButtons>
<DialogButton Content="Ok" IsPrimary="true" OnClick="@close"></DialogButton>
</DialogButtons>
</SfDialog>
</div>
</CascadingValue>
@code {
SfDialog dlg;
private string title { get; set; }
private string message { get; set; }
public async Task ShowDialog(string title, string message)
{
this.title = title;
this.message = message;
StateHasChanged();
await this.dlg.ShowAsync();
}
public async Task close()
{
await this.dlg.HideAsync();
}
} |
|
<div>
<p>Click the button to show the dialog</p>
<button class="e-btn" @onclick="@ClickHandler">Index Button</button>
</div>
@code{
[CascadingParameter]
public MainLayout mainLayoutObj { get; set; }
private async Task ClickHandler()
{
await mainLayoutObj.ShowDialog("DialogHeader1", "Dialogcontent1");
}
}
|
Hi Vinitha,
Thank you so much, that worked perfectly and elegantly!
I was missing the call to StateHasChanged() and also you showed me how to share components on all pages, thank you!