Melon NG
Melon NG

Reputation: 2984

How can I access the parent Component in Blazor server-side?

I am using the server-side of blazor.

Here is a child Component which named Child:

<div class="Child">
    <img @OnClick="ShowFullImage" src="/img/aaa.jpg"/>
</div>
@code{
   private void ShowFullImage(){}
}

And here is a Parent Component:

<Child></Child>
<img id="FullImage"/>

The child Component is about to display the thumbnail. When the user clicks the thumbnail(just the img in the child Component), the full image will display in the img of parent Component which named FullImage.

Now the question is although I can add an onlick function in the child component, I don't know how to access its parent component yet.

Upvotes: 6

Views: 7547

Answers (1)

Vencovsky
Vencovsky

Reputation: 31565

I think that what you are looking for is EventCallback

// Child
<div class="Child">
    <img @onclick="ShowFullImage" src="/img/aaa.jpg"/>
</div>

@code{
   [Parameter]
   public EventCallback ShowFullImage { get; set; }
}
// Parent
<Child ShowFullImage="@ShowFullImage" />

@code{
    public void ShowFullImage()
    {
        // do what you want
    }
}

Upvotes: 8

Related Questions