How can i use the value from a textbox component in blazor?

segu

Im trying to create a textbox component to recicle code, but i cant get the value from the textbox in a page. I only need to get the TextBox value from the component to use it into other pages.

this is the TextBox component :

<style>
    .textbox-parameters{
        background: Red;
    }
</style>

    @if (!string.IsNullOrWhiteSpace(Label))
    {
        <label for="@Id">@Label</label>
    }
    <div class="input-group mb-3 " >
        <input type="text" class="form-control textbox-parameters" id="@Id"  @bind="@CurrentValue">
    </div>

@code {
    [Parameter] public string? Label { get; set; }
    [Parameter] public string? Id { get; set; }
    private string CurrentValue {get;set;} = "";
}

and im trying to use there:

<div class="container-fluid">
    <div class="row">
        <div class="col-sm">
            <label >Proyecto:</label>
            <div class="input-group mb-3">
              <input type="text" class="form-control" id="basic-url" aria-describedby="basic-addon3">
            </div>
        </div>
        <div class="col-sm">
                <p class="rcorners2">Lista modelos</p>
        </div>
        <div class="col-sm">
                <p class="rcorners2">Lista Iteraciónes</p>
        </div>
        <div class="col-sm">
                <p class="rcorners2">Imagen Semilla</p>
        </div>
        <div class="col-sm ">
            <div class="row">
                <div class="col-sm">
                    <label>Ancho</label>
                    <div class="input-group mb-3">
                      <div class="input-group-prepend"></div>
                      <input type="text" class="form-control textbox-parameters" id="basic-url" aria-describedby="basic-addon3">
                    </div>
                </div>
                <div class="col-sm">
                    <TextBox Id="Alto" Label="Alto" CurrentValue="@alto" />
                </div>
                <label>Texto: @alto</label>
            </div>
        <div class="row">
            <div class="col-sm text-center">
                <label>Numero Imágenes</label>
                <div class="input-group mb-3 " >
                  <input type="text" class="form-control textbox-parameters" id="basic-url" aria-describedby="basic-addon3">
                </div>
            </div>
        </div>

        </div>

        <div class="col-sm">
           <button type="button" class="btn btn-outline-success">Generar</button>
        </div>
    </div>
</div>


@code{
    string alto="";

}

Sorry for my extra text but StackOverflow need more details but thats my only problem with my code. thanks.

MrC aka Shaun Curtis

You need to set up binding correctly - i.e. set up the Value and ValueChanged parameters on the component.

Here's TextBox:

<style>
    .textbox-parameters{
        background: Red;
    }
</style>

    @if (!string.IsNullOrWhiteSpace(Label))
    {
        <label for="@Id">@Label</label>
    }
    <div class="input-group mb-3 " >
        <input type="text" class="form-control textbox-parameters" id="@Id" value="@Value" @onchange="this.OnChange">
    </div>

@code {
    [Parameter] public string Value {get;set;} = "";
    [Parameter] public EventCallback<string> ValueChanged {get;set;}

    [Parameter] public string? Label { get; set; }
    [Parameter] public string? Id { get; set; }

    private void OnChange(ChangeEventArgs e)
    {
        ValueChanged.InvokeAsync(e.Value.ToString());
    }
}

Note we've bound the value to Value and the change event to OnChange which then invokes ValueChanged.

And a test page:

@page "/"
<TextBox @bind-Value="@this.Value" />

<div>
    Value = @this.Value
</div>
@code {
    private string Value { get; set; } = "Fred";
}

@bind-Value tells the Razor pre-compiler to bind to Value to set the value and ValueChanged to update the bound field/property.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fill in a value from one TextBox to another TextBox in Blazor Server?

How can i get value from textbox?

How can I call '/Identity/Account/ExternalLogin' from a Blazor component?

How can I use a textbox value to set a value on a radiobutton

How can i create a blazor component?

How can I pass List from child component to parent component in blazor?

How I can Create a timer component in Blazor and start it from outside of component or other page

how can i use NavLink to redirect from component to another component

C# ComboBox and TextBox dynamic value - How can I access a TextBox value from another function or event?

How can I reactively update a value in a component from a store value?

How to Fill in Value From One TextBox to Another Texbox in Blazor Server?

How can I display value in textbox from database that value based on selected value from combobox in php?

How can I pass value to my component on `onBlur` event of PrimeNG autocomplete textbox?

How can I get a cascading value in Blazor?

How can I randomly add CSS attributes to Blazor component from parent layer as Vue did?

How can I use HTML attributes in Blazor?

How can i get selected value from a select component

How can I change the value of let from a component prop?

How can I pass an *ngIf value from one component to another?

How can I pass a value from a function to any component in React?

How can i check a value is pasted in Textbox?

How can i use a Textbox, that was created programmatically?

How can I declare a Blazor component<TData,TValue>?

How can I reference a "@typeparam TModel" field in the Blazor (sub)-component?

How can I pass a List<string> as a parameter to a child component in blazor?

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

How can I get the current circuit id in a Blazor Server component?

How can I render a component in Blazor that is defined as object?

How do I call a method in one Blazor (parent) component from another Blazor ("child") component?