Blazor Server App - Print without button

Cenk 1,041 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

Answer accepted by question author
  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) 81,976 Reputation points Volunteer Moderator
    2022-07-11T16:51:29.413+00:00

    if your print preview is not a re-render, but just the browser preview, then just use CSS to hide the edit

    <style>   
           @media print {   
                   .noprint {   
                      visibility: hidden;   
                   }   
           }   
    </style>   
       
       
    <button type="button" class="btn btn-light noprint" @onclick="() => EditCustomer(Customer.CustomerId)">Edit</button>   
    
    0 comments No comments

Your answer

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