# Nested Dialogs firing OnClose event

**URL:** <https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470>\
**Category:** Blazor Server\
**Created:** [October 29, 2020, 11:25am UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470 "2020-10-29T11:25:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://forum.radzen.com/user_avatar/forum.radzen.com/kim/32/9988_2.png) [@kim](https://forum.radzen.com/u/kim)\
**Post date:** [October 29, 2020, 11:25am UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/1 "2020-10-29T11:25:49Z")

</div>

Hello Radzen Team!

I have this action hooked to the OnClose event on the Load method in my first dialog:

```auto
DialogService.OnClose += (Action<dynamic>) async delegate { await JSRuntime.InvokeVoidAsync("stop"); };
```

However this applies to the top most dialog. For example, if I open another dialog from my first one, then closes it, the event is triggered too.

So how can I subscribe to the dialog events of only the current dialog?

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [October 29, 2020, 11:40am UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/2 "2020-10-29T11:40:24Z")

</div>

Hi @kim,

The DialogService controls all dialogs and this is why its events fire for all dialogs. If you want to execute code after a specific dialog is closed you can use the Then event of the Open dialog action in Radzen.

---

<div class="post-metadata">

**Author:** ![kim](https://forum.radzen.com/user_avatar/forum.radzen.com/kim/32/9988_2.png) [@kim](https://forum.radzen.com/u/kim)\
**Post date:** [October 29, 2020, 12:15pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/3 "2020-10-29T12:15:28Z")

</div>

Did you mean something like this?

 ![image](https://forum.radzen.com/uploads/default/original/2X/b/b608c207a7521100054c38a8417584d551504775.png)

This will not work either, same results of the previous option.

As soon as a new dialog is opened from another dialog, the result of the first dialog is returned and Then code is executed.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [October 29, 2020, 12:18pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/4 "2020-10-29T12:18:23Z")

</div>

I am afraid I don't understand what the problem is. We will need a reproduction. Please send it to [info@radzen.com](mailto:info@radzen.com)

---

<div class="post-metadata">

**Author:** ![kim](https://forum.radzen.com/user_avatar/forum.radzen.com/kim/32/9988_2.png) [@kim](https://forum.radzen.com/u/kim)\
**Post date:** [October 29, 2020, 1:16pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/6 "2020-10-29T13:16:23Z")

</div>

I'm sorry, but I can't ship the app to your email at this point in development. I'll do my best to explain the problem below:

I have this method attached to the Click event of my button, to open the first dialog:

 ![image](https://forum.radzen.com/uploads/default/original/2X/c/c5b028222e858a17d602f466e71d2095ef942aeb.png)

This is debugging step 1:

 ![image](https://forum.radzen.com/uploads/default/original/2X/f/fd975150b98711ce065072da5fae4afcb056bfd8.png)

Next the first dialog opens:

 ![image](https://forum.radzen.com/uploads/default/original/2X/1/1d639649ac9d752b105035ac4dee17536a5fd8dc.png)

Then in this first dialog I'll open a second dialog:

 ![image](https://forum.radzen.com/uploads/default/original/2X/9/97f4a719ba5d49e9f1e04e303dd0513604f63913.jpeg)

Right after closing the second dialog, the debugging hits step 2, which is very weird since my first dialog hasn't been submitted neither closed:

 ![image](https://forum.radzen.com/uploads/default/original/2X/f/f3f93273581599a98d7da43f3d9ddbfe2994a359.png)

Then it executes the Then code even tough the first dialog is still open:

 ![image](https://forum.radzen.com/uploads/default/original/2X/f/f5b3db5f63236f927cd7a92fb8cbb6834b99748e.png)  
 ![image](https://forum.radzen.com/uploads/default/original/2X/5/50cf89986ab7051baae1b37455c55601ebbc9305.jpeg)

Tell me if you need more info on this problem.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [October 29, 2020, 1:39pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/7 "2020-10-29T13:39:45Z")

</div>

Reproduced the issue and will investigate what is causing it.

---

<div class="post-metadata">

**Author:** ![SloSuenos](https://forum.radzen.com/user_avatar/forum.radzen.com/slosuenos/32/9256_2.png) [@SloSuenos](https://forum.radzen.com/u/SloSuenos)\
**Post date:** [October 29, 2020, 1:53pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/8 "2020-10-29T13:53:08Z")

</div>

@kim I wonder if an @key directive could be used somehow.

Also, could this be related? [DialogService OnClose getting called unexpectedly](http://forum.radzen.com/t/dialogservice-onclose-getting-called-unexpectedly/5434)

Stephen

---

<div class="post-metadata">

**Author:** ![kim](https://forum.radzen.com/user_avatar/forum.radzen.com/kim/32/9988_2.png) [@kim](https://forum.radzen.com/u/kim)\
**Post date:** [October 29, 2020, 4:18pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/9 "2020-10-29T16:18:41Z")

</div>

Hey @SloSuenos!

Maybe every dialog is missing its @key as you mentioned, because I think this was working before.

And that topic is definitely related to this problem, silly me for not noticing at first.

---

<div class="post-metadata">

**Author:** ![LeonGorbaty](https://forum.radzen.com/letter_avatar_proxy/v4/letter/l/e79b87/32.png) [@LeonGorbaty](https://forum.radzen.com/u/LeonGorbaty)\
**Post date:** [October 29, 2020, 7:34pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/11 "2020-10-29T19:34:26Z")

</div>

I just deleted a previous loooong post here ... because ... I got multiple dialogs to work, as follows:

I initially did not notice that DialogService.OpenAsync returns the result - SO, there is no need for OnClose event handlers ... you can just continue working on the result after the awaited call returns.

Once I eliminated the OnClose event handlers all together, nested dialogs seem to work fine?

So it appears to me that DialogService.Close(...) just closes the top-most dialog, while the underlying one remains open and ok.

Thus, regarding:  
[korchev]  
Hi @kim,  
The DialogService controls all dialogs and this is why its events fire for all dialogs. If you want to execute code after a specific dialog is closed you can use the Then event of the Open dialog action in Radzen.

Perhaps we can get by without any event handlers, i.e. if you want to execute code after a specific dialog closes, the you await the call to open it and assign the result in the same call, and then continue to execute in the same method ... works for me, am I missing anything?

---

<div class="post-metadata">

**Author:** ![kim](https://forum.radzen.com/user_avatar/forum.radzen.com/kim/32/9988_2.png) [@kim](https://forum.radzen.com/u/kim)\
**Post date:** [November 3, 2020, 2:43pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/12 "2020-11-03T14:43:03Z")

</div>

Has this problem been addressed in the last version 2.54.6?

After updating it's still happening as shown above.

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [November 3, 2020, 3:32pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/13 "2020-11-03T15:32:06Z")

</div>

Hi @kim,

We've not managed to fix this in our latest build, sorry! I'll will do my best to send you our latest build tomorrow.

UPDATE: You can add empty Then to inner dialog open to fix it:

 ![image](https://forum.radzen.com/uploads/default/original/2X/2/2d42d6b1763c1eb59d3a339a01607419133120ae.png)

---

<div class="post-metadata">

**Author:** ![kim](https://forum.radzen.com/user_avatar/forum.radzen.com/kim/32/9988_2.png) [@kim](https://forum.radzen.com/u/kim)\
**Post date:** [November 3, 2020, 3:47pm UTC](https://forum.radzen.com/t/nested-dialogs-firing-onclose-event/5470/14 "2020-11-03T15:47:39Z")

</div>

Thanks for the updated suggestion, it is working now.
