Blazor Server App - Print without button

Cenk 1,036 Reputation points
2022-07-09T10:57:14.043+00:00

Hello guys,

I have a component as follows;

 @using IMS.CoreBusiness  
      
    @inject NavigationManager NavigationManager  
      
    @if (Customer != null)  
    {  
        <tr>  
            <td>@Customer.Name</td>  
            <td>@Customer.TaxNumber</td>  
            <td>@Customer.TaxAdministration</td>  
            <td>@Customer.PhoneNumber</td>  
            <td>@Customer.Email</td>  
            <td>@Customer.Address</td>  
            <td>@Customer.DeliveryAddress</td>  
            <td>@Customer.MainResponsibleName</td>  
            <td>@Customer.AssistantResponsibleName</td>  
            <td>  
                <button type="button" class="btn btn-light" @onclick="() => EditCustomer(Customer.CustomerId)">Edit</button>  
            </td>  
      
        </tr>  
    }  
      
      
    @code {  
        [Parameter]  
        public Customer? Customer { get; set; }  
      
        private void EditCustomer(int cusId)  
        {  
            NavigationManager.NavigateTo($"/editcustomer/{cusId}");  
        }  
    }  

And here is my print preview;
219009-print.png

I wonder if there is a way not to print the edit button? If not, I have to implement a separate report for this.

Developer technologies .NET Blazor
0 comments No comments
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,066 Reputation points
    2022-07-11T04:15:08.523+00:00

    Hi @Cenk ,

    The html render code requires a certain response time. JS interop calls will not wait for the html render. You can add a delay to achieve your requirement:

    private async void PrintReport()  
    {  
        ShowEdit = false;  
        await Task.Delay(1000);   
        JSRuntime.InvokeVoidAsync("print");  
    }  
    

    If you also need redisplay the edit button after printing, you need change your code:

        private async void PrintReport()  
        {  
            ShowEdit = false;  
            await Task.Delay(1000);   
            JSRuntime.InvokeVoidAsync("printInvoke");  
            ShowEdit  = true;  
            InvokeAsync(() => StateHasChanged());  
        }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Rena

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-09T16:30:10.44+00:00

    Just add a parameter that suppresses the button.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-10T01:25:57.217+00:00

    define parameter showEdit and a simple if :

    @if (showEdit) {  
                    <button type="button" class="btn btn-light" @onclick="() => EditCustomer(Customer.CustomerId)">Edit</button>  
    }  
      
    
    0 comments No comments

  3. Cenk 1,036 Reputation points
    2022-07-10T05:20:03.967+00:00

    I put showEdit parameter as you suggested @Bruce (SqlWork.com) ;

     <td>  
                @if (ShowEdit)  
                {  
                    <button type="button" class="btn btn-light" @onclick="() => EditCustomer(Customer.CustomerId)">Edit</button>  
                }  
                  
            </td>  
      
        </tr>  
    }  
      
      
    @code {  
        [Parameter]  
        public Customer? Customer { get; set; }  
      
        [Parameter]  
        public bool ShowEdit { get; set; } = true;  
    

    And set it false on button click but still displays edit button in the report.

    <tbody>  
                @foreach (var cus in this._listCustomers)  
                {  
                    <CustomerItemComponent Customer="cus" ShowEdit="ShowEdit"></CustomerItemComponent>  
                }  
            </tbody>  
    @code {  
        private List<Customer>? _listCustomers;  
        private bool ShowEdit = true;  
      
        protected override async Task OnInitializedAsync()  
        {  
             
            var ven = await ViewCustomersByNameUseCase.ExecuteAsync();  
      
            this._listCustomers = ven.ToList();  
        }  
      
        private void OnSearchCustomers(List<Customer> vendors)  
        {  
            _listCustomers = vendors;  
        }  
      
        private void AddCustomer()  
        {  
            NavigationManager.NavigateTo("/addcustomer");  
        }  
        private void PrintReport()  
        {  
            ShowEdit = false;  
            JSRuntime.InvokeVoidAsync("print");  
        }  
    
    0 comments No comments

  4. Cenk 1,036 Reputation points
    2022-07-11T05:05:37.953+00:00

    How to get back edit button after print @Rena Ni - MSFT ?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.