dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.5k stars 10.04k forks source link

How to initialize a Blazor component in codebehind ? (Without Jsinterop) #17606

Closed Indrajith-Sync closed 4 years ago

Indrajith-Sync commented 4 years ago

Currently in my project i am creating a component(Server-side), While calling the StateHasChanged().

It throws an exception error The render handle is not yet assigned.

<div>@Updatedvalue</div>

@code {

public string Updatedvalue;

public void function() { Updatedvalue = "Render part is called". StatehasChanged() // Getting exception in this line and the render part is not called. } Is there any other way to called the render part of the code section after the code block is executed.

Since i am rendering the component dynamically on a button Click is there any possibility to achieve this scenario ?

javiercn commented 4 years ago

@Indrajith-Sync thanks for contacting us.

We're not sure we understand your scenario. Could you provide a minimal repro with detailed steps to illustrate your scenario, what you are expecting to happen and what is happening?

Indrajith-Sync commented 4 years ago

Hi @javiercn

Actually i am rendering a server side component in blazor. i Have initiated the code section part of the rendering and after that i need to call the StateHasChanged to make changes in the above defined Component tags.Here, I am facing issue in this in-between cycle as (The render handle is not yet assigned) from the StateHasChanged() which is in the code block section as mentioned in the example above.

javiercn commented 4 years ago

@Indrajith-Sync Your snippet doesn’t have enough context for us to understand your issue. Could you provide a minimal repro project as we asked?

We can’t see where in which context you are calling something that calls statehaschanged.

Otherwise we won’t be able to help you.

Indrajith-Sync commented 4 years ago

/ Code behind source / (Razor file)

<MyComponent Content="@dialog.Content" Header="@dialog.Header" />

@code { public void Alert(string args) { dialog = new Dialog() { Content = args, Header = "Information" }; this.StateHasChanged(); // Getting an exception here during the run-time as mentioned above. } }

/ My razor code / (From this file i am calling my source file defined above)

<Component @ref="compRef"></Component>

@code {
compRef.Alert("Testing"); }

Can you understand the issue now. I don't have any repo project since my project is only under implementation.

Indrajith-Sync commented 4 years ago

Hi @javiercn

Here is the sample repo (https://github.com/Indrajith-Sync/Blazor). In this repo in Index.razor file i have initialized MyComponent and trying to create it during the runtime on a button click.

javiercn commented 4 years ago

We understand now. Thanks for the clarification. The framework is responsible for the lifetime of the components.

If you instantiate a component yourself, it is not attached to the Blazor rendering process in any way.

I’m not exactly sure what you are trying to accomplish, but I suggest you look into using a component reference instead. See https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1#capture-references-to-components for details