Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C# What is the simplest method to update a variable on the index page from a component?

lingo57

Coder
Hi

What is the simplest method to update a variable on the index page from a component in the Blazor server?

For instance, if you have an upload component and want it to change a boolean value on the index page.

Should I use state, props?

regards,
 
Hi

What is the simplest method to update a variable on the index page from a component in the Blazor server?

For instance, if you have an upload component and want it to change a boolean value on the index page.

Should I use state, props?

regards,
Hey there!

In Blazor Server, the simplest way to update a variable on the index page from a component is to use EventCallback or shared state. Here's how you can approach it:

Using​

  1. Define an EventCallback in your upload component.
  2. Invoke the callback when you want to update the value.
  3. Pass the callback from the index page and handle it there.
Example:

Index.razor
Code:
@page "/"
@code {
    private bool isUploaded;

    private void UpdateStatus(bool status)
    {
        isUploaded = status;
    }
}

<UploadComponent OnUploadComplete="UpdateStatus" />
UploadComponent.razor
Code:
@code {
    [Parameter]
    public EventCallback<bool> OnUploadComplete { get; set; }

    private async Task TriggerUpload()
    {
        // Do your upload logic here...
        await OnUploadComplete.InvokeAsync(true); // Set true or false based on your logic.
    }
}

Using Shared State​

If you have more complex state sharing needs, consider creating a service to manage shared state between components/pages. This is useful when multiple components need access.

Let me know if you'd like an example for that too! 😊
 
I added this code to my index page:

Code:
[Parameter] public EventCallback<bool> isConverting { get; set; }
private void UpdateParent() { isConverting.InvokeAsync(false); }

The code for the upload page includes:

Code:
[Parameter]
public EventCallback<bool> isConverting { get; set; }

But when I want to use "isConverting " I see a red line .

Code:
 @if (isConverting )
 {
     <div class="progress mt-3">
         <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 100%"></div>
     </div>
 }

I couldn't find a satisfactory answer from AI or other resources to solve the problem.
What is your idea?

regards,
 
Back
Top Bottom