Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass event args with an onclick in Blazor for the ID

I am trying to create a highlighted tab system in a Blazor app. Like how you would have an active nav component that is highlighted.
So when a button is clicked on, the background changes the colour to represent it is selected.

I can do this easily using JavaScript by passing e and having e.id to get the HTML ID of the element.
However, I am now trying to do this in a Blazor app and I can’t figure out how to complete this code.

First I couldn’t pass a string as a parameter when clicked. So I am now trying to pass the ID of HTML.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I pass the parameter?
If that can’t be done, what should I do to make this tabbing system?

Here is some basic code I was playing with to pass the values and change the colour of one of the buttons.

@page "/store"

<PageTitle>Store</PageTitle>

<style>
    .tab-button {
        background-color: #ccc;
        color: #333;
    }

        /* CSS for selected buttons/tabs */
        .tab-button.selected {
            background-color: #007bff;
            color: #fff;
        }
</style>

<div class="h-screen">
    <div class="flex h-full">
        <section class="hidden md:grid md:grid-rows-nav bg-slate-600 h-full min-w-[250px] border-r-2 border-black border-solid h-full">
            <NavMenu />
        </section>

        <main class="relative w-full h-full grid">
            <h3>Store</h3>
            <div>
                <button id="abc" class="tab-button @(selectedTab == "tab1" ? "selected" : "")" @onclick="() => SelectTab(e)">Tab 1</button>
                <button id="xyz" class="tab-button @(selectedTab == "tab1" ? "selected" : "")" @onclick="() => SelectTab(e)">Tab 2</button>
            </div>
            <div>
                @if (selectedTab == "tab1")
                {
                    <p>Content for Tab 1</p>
                }
                else if (selectedTab == "tab2")
                {
                    <p>Content for Tab 2</p>
                }
            </div>
        </main>
    </div>
</div>


@code {
    private string selectedTab = "tab1";

    private void SelectTab(EventArgs e)
    {
        Console.WriteLine($"fsdfs {e.id}");
    }
}

I tried to pass various params and used eventArgs
With this code, I have ‘e’ as undefined and it is not passing any data. The error mentions it is not defined in context.

>Solution :

The simplest way will be to pass the value as an argument.

<button id="abc" class="tab-button @(selectedTab == "tab1" ? "selected" : "")" 
    @onclick="@(() => SelectTab("tab1"))">Tab 1</button>
<button id="xyz" class="tab-button @(selectedTab == "tab2" ? "selected" : "")" 
    @onclick="@(() => SelectTab("tab2"))">Tab 2</button>
private void SelectTab(string e)
{
    selectedTab = e;
}

Demo @ BlazorFiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading