简体   繁体   English

单击 c# 和 ASP.NET 内核中的另一个按钮时如何隐藏按钮并显示它们?

[英]How to hide buttons and show them when clicking another button in c# and ASP.NET Core?

I have a problem: how can I hide some buttons and make them visible when another button is clicked?我有一个问题:如何隐藏一些按钮并在单击另一个按钮时使它们可见? I am using Blazor and ASP.NET Core, and I have seen several examples but they are in Javascript and jQuery but not in C#. I am using Blazor and ASP.NET Core, and I have seen several examples but they are in Javascript and jQuery but not in C#.

This is the button that will execute the event to show the buttons:这是将执行事件以显示按钮的按钮:

<button class="btn btn-dark" @onclick="GuardarCotizacion"><i class="fa fa-floppy-o" aria-hidden="true"></i> Guardar Cotización</button>

These are the buttons that I want to have hidden are inside a dropdown:这些是我想要隐藏在下拉列表中的按钮:

<div class="box-footer">
    <div class="pull-right">
        <div class="btn-group">
            <div class="btn-group">
                <div class="dropdown">
                    <button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Correo</button>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenu1">
                        <button class="dropdown-item" type="button" @onclick="@DescargarPDF"><i class="oi oi-envelope-closed"></i> Descargar Cotización</button>
                        <button class="dropdown-item" type="button"><i class="oi oi-eye"></i> Ver Correo</button>
                    </div>
                </div>
            </div>

            <button type="button" class="btn btn-outline-info" @onclick="@AgregarProducto"><i class='fa fa-plus'></i> Agregar productos</button>
        </div>
    </div>                              

    <div class="btn-group">
        <div class="btn-group">
            <div class="dropdown">
                <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Facturación</button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
                @if (isFacturado)
                {
                    <button class="dropdown-item" type="button"><i class="oi oi-eye"></i> Ver Factura</button>
                }
                else
                {
                    <button class="dropdown-item" type="button" @onclick="FacturarDemo"><i class="oi oi-file"></i> FacturarDemo</button>
                    <button class="dropdown-item" type="button"><i class="oi oi-file"></i> ---- </button>
                    <button class="dropdown-item" type="button" @onclick="Facturar"><i class="oi oi-file"></i> Facturar</button>
                }
                </div>
            </div>
        </div>
    </div>

    <a class="btn btn-outline-primary" href="detalles-carritos"><i class="oi oi-eye"></i> Ver Artículos</a>
</div>
@if (ShowButtons) 
{
  <button....... />
} 

@code
{
  private bool ShowButtons = true;

  private void GuardarCotizacion() 
  {
    ShowButtons = !ShowButtons;
  } 
} 
<button class="btn btn-dark" @onclick="GuardarCotizacion"><i class="fa fa-floppy-o" aria-hidden="true"></i> Guardar Cotización</button>

In Blazor it is preferable to speak in terms of rendering or not, rather than making them visible or hiding them.在 Blazor 中,最好是在渲染或不渲染方面说话,而不是让它们可见或隐藏它们。 Thus, if GuardarCotizacion is a method that should hide some buttons when called can be as described in the @code block below:因此,如果 GuardarCotizacion 是一种在调用时应该隐藏一些按钮的方法,则可以如下面的@code 块中所述:

@code 
{
   private bool show = false;
   private void GuardarCotizacion()
   {
       show = true;
   } 
}

Note: The code above defines a boolean variable set to false by default.注意:上面的代码定义了一个 boolean 变量,默认设置为 false。 Now, when you click on the "Guardar Cotización" button, the method GuardarCotizacion is called and set the variable to true, after which the StateHasChanged method is automatically called, and the component is rendered.现在,当您单击“Guardar Cotizacion”按钮时,将调用 GuardarCotizacion 方法并将变量设置为 true,然后自动调用 StateHasChanged 方法,并渲染组件。

If you want, for example, to "hide" this button:例如,如果您想“隐藏”此按钮:

<button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Correo</button>

and show it only when you click on the "Guardar Cotización" button, you can wrap the button in an if statement like this:并且仅当您单击“Guardar Cotización”按钮时才显示它,您可以将按钮包装在 if 语句中,如下所示:

@if( show)
{
   <button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Correo</button>

}

Now, when your component is freshly created, this button is not going to be displayed, because the initial value of the variable show is false, but after changing it to true it is going to be rendered.现在,当您的组件刚刚创建时,该按钮将不会显示,因为变量 show 的初始值为 false,但在将其更改为 true 后,它将被渲染。

This is how you can do that with the rest of your button, together or separately, in a simple manner or a sophisticated manner, according to your whims.这就是您如何使用按钮的 rest 以简单或复杂的方式一起或单独执行此操作的方法,具体取决于您的心血来潮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM