Hi,
Is it possible to design a custom dialog component that contains the pages I have designed in the past? In other words, a custom dialog component whose input parameter is the page name and displays the contents of the page in the dialog?
Thank you
|
<SfDialog @ref="DialogObj" @bind-Visible="@Visible" Width="250px">
<DialogTemplates>
<Header> @Header </Header>
<Content> @Content </Content>
<FooterTemplate>
<button class="e-btn e-primary" @onclick="OnOkClick">Ok</button>
<button class="e-btn" @onclick="OnCancelClick">Cancel</button>
</FooterTemplate>
</DialogTemplates>
</SfDialog>
@code {
SfDialog DialogObj;
[Parameter]
public string Header { get; set; } = "";
[Parameter]
public string Content { get; set; } = "";
[Parameter]
public bool Visible { get; set; } = false;
public void Show()
{
this.DialogObj.Show();
}
private void OnOkClick()
{
this.DialogObj.Hide();
}
private void OnCancelClick()
{
this.DialogObj.Hide();
}
}
|
|
<Dialog @ref="ConfirmDialog" Visible=@Visibility Header="@DlgHeader" Content="@DlgContent"></Dialog>
@code {
Dialog ConfirmDialog;
public bool Visibility { get; set; } = false;
public string DlgHeader { get; set; } = "";
public string DlgContent { get; set; } = "";
private void ShowDialog()
{
this.DlgHeader = "Index page";
this.DlgContent = "Are you want to close this confirm Dialog in index page ?";
this.ConfirmDialog.Show();
}
} |
Hi Vinitha Jeyakumar,
I reviewed examples of your site. Is it possible to implement the following example with the reusable dialog component that you designed?
Thank you
|
using Microsoft.AspNetCore.Components;
namespace Dialog.Components
{
public class DialogOptions
{
public string Header { get; set; }
public RenderFragment Content { get; set; }
public RenderFragment FooterTemplate { get; set; }
public bool IsModal { get; set; }
public bool ShowCloseIcon { get; set; }
}
}
|
|
using System;
using Microsoft.AspNetCore.Components;
namespace Dialog.Components
{
public class DialogService
{
public event Action<DialogOptions> DialogInstance;
public void Open(DialogOptions options)
{
// Invoke DialogComponent to update and show the dialog with options
this.DialogInstance.Invoke(options);
}
}
}
|
|
@using Syncfusion.Blazor.Popups;
@if (RenderDialog)
{
<SfDialog IsModal="@Options.IsModal" ShowCloseIcon="@Options.ShowCloseIcon" @bind-Visible="@Visibility">
<DialogTemplates>
<Header> @Options.Header </Header>
<Content> @Options.Content </Content>
<FooterTemplate>@Options.FooterTemplate</FooterTemplate>
</DialogTemplates>
</SfDialog>
}
@code {
[Inject]
public DialogService DialogService { get; set; }
private bool RenderDialog { get; set; } = false;
private bool Visibility { get; set; } = false;
// Parameter
private DialogOptions Options = new DialogOptions();
protected override void OnInitialized()
{
// Update the parameter in local variable and render the dialog
DialogService.DialogInstance += (DialogOptions options) =>
{
InvokeAsync(() =>
{
this.Options.Header = options.Header;
this.Options.Content = options.Content;
this.Options.IsModal = options.IsModal;
this.Options.ShowCloseIcon = options.ShowCloseIcon;
this.Options.FooterTemplate = options.FooterTemplate;
this.RenderDialog = true;
this.Visibility = true;
this.StateHasChanged();
});
};
}
}
|
|
using Dialog.Components;
namespace Dialog
{
public class Program
{
public static async Task Main(string[] args)
{
...
builder.Services.AddScoped<DialogService>();
...
}
}
}
|
|
@using Dialog.Components
…
…
<DialogComponent />
|
|
@page "/"
@using Dialog.Components
@inject DialogService DialogServices
<button class="e-btn" @onclick="@SignInDialog"> Sign In Dialog </button>
<button class="e-btn" @onclick="@SignUpDialog"> Sign Up Dialog </button>
@code {
private void SignInDialog()
{
this.DialogServices.Open(new DialogOptions()
{
Header = "Sign In",
Content = @<SignIn />, // Fragment loaded from external component
IsModal = true,
ShowCloseIcon = true,
FooterTemplate = @<button> OK </button> // Fragments
});
}
private void SignUpDialog()
{
this.DialogServices.Open(new DialogOptions()
{
Header = "Sign Up",
Content = @<SignUp />, // Fragment loaded from external component
IsModal = true,
ShowCloseIcon = true,
FooterTemplate = @<button> OK </button> // Fragments
});
}
}
|
The last post seems to work perfectly, i am just confused about one thing.
could someone explain to me, by taking that approach how i can close the dialog by a button present in the dialog
so for instance by taking the example above, from a button within the signup or signin dialogs
Hi Dennis,
You can achieve your requirement by adding dialog buttons inside the SfDialog component and binding the click event to close the dialog. In the below sample, we have hidden the dialog when you click the dialog OK button. Please find the below code and sample for your reference.
Components/DialogComponent.razor
|
<SfDialog @ref="DialogObj">…… <DialogButtons> <DialogButton @ref="DialogButton" Content="OK" OnClick="@CloseDialog"/> </DialogButtons> </SfDialog>
@code { SfDialog DialogObj; public async Task CloseDialog() // Trigger when you click the dialog button { await this.DialogObj.HideAsync(); } } |
Regards,
Buvana S
With this example, how would you go about returning data from the dialog to the calling component, when the dialog is closed?
Hi Greg Paff ,
Thank you for your patience. To achieve a reusable dialog that can be invoked from anywhere and also return data back to the calling component (like an input value), you can use TaskCompletionSource to await the dialog's result. This allows the calling component to await the dialog result and continue execution once the dialog is closed—making it ideal for scenarios like retrieving form input values.
We have attached a sample for your reference. Please review the sample if you have any concerns or queries ,please get back to us.
Regards,
Priyanka K