What is the best way to show the loader while uploading the file in gridview

BeUnique 2,112 Reputation points
2024-06-07T17:12:49.2366667+00:00

I am using gridview for displaying and inserting new records.

i'm using loader wherever it is required in the gridview. it is working fine.

i'm trying to use animation loader for specific upload column in gridview.

will it possible to show the loader only for specific column...?

see below my code.

User's image

<div id="divGrid" style="height: 105PX; width: 99%">
 <asp:GridView ID="grdBills" AutoGenerateColumns="False" runat="server"  Width="100%"
  DataKeyNames="Id" >
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
   <HeaderTemplate>
   <label style="text-align: center; display: block;">Country</label>
   </HeaderTemplate>
   <ItemTemplate>
   </ItemTemplate>
   <EditItemTemplate>
        <asp:TextBox ID="txtEditcountry"  runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'></asp:TextBox>
   </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>   
      <label style="text-align: center; display: block;">Name</label>   </HeaderTemplate>
	<ItemTemplate>
	   <asp:Label ID="lblname"  runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Custname") %>'></asp:TextBox>
	</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
   <HeaderTemplate>
   <label style="text-align: center; display: block;">File Upload</label>
   </HeaderTemplate>
   <ItemTemplate>
   </ItemTemplate>
   <EditItemTemplate>
   	   <asp:FileUpload ID="fuUpload" runat="server" />
	   <asp:Label ID="filename" runat="server" />
	   <asp:Button ID="btnUpload" Text="Upload" OnClick="Upload" runat="server" Style="display: none" />
   </EditItemTemplate>
</asp:TemplateField>
protected void Upload(object sender, EventArgs e)
  {
        System.Threading.Thread.Sleep(2000);
        --------
        --------------
         Label label = (control.FindControl("ImageErrorLabel") as Label);
         FileUpload fuUpload = (FileUpload)control.FindControl("fuUpload");
  }

Note : i using loader the entire gridview. i want only animation loader only for upload column of the grid.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,474 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,887 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,251 Reputation points Microsoft Vendor
    2024-06-10T04:14:29.17+00:00

    Hi @Gani_tpt,

    You can do this with the following code.

     <script type="text/javascript">
         function UploadFile(fileUpload) {
             if (fileUpload.value != '') {
                 $("[id*=gvCustomers]").find("[id*=btnUpload]").click();
               
             }
         }
         function ShowProgress() {
             $("[id*=gvCustomers]").find("[id*=loading_img]").show(); 
         }
     </script>
    
     <EditItemTemplate>
         <asp:FileUpload ID="fuUpload" runat="server" />
         <asp:Label ID="ImageErrorLabel" runat="server" Text='<%# Eval("FileName") %>' />
         <asp:Button ID="btnUpload" Text="Upload" OnClick="Upload" runat="server" OnClientClick="ShowProgress();" Style="display: none" />
         <img id="loading_img" runat="server" style="Display: None" src="https://i.pinimg.com/originals/8a/c1/29/8ac12962c05648c55ca85771f4a69b2d.gif" width="50" height="50"/>                                       
     </EditItemTemplate>
    

    Best regards,
    Lan Huang


    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.

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Albert Kallal 5,246 Reputation points
    2024-06-07T17:44:23.4333333+00:00

    The issue is with the standard file upload is it requires a post-back.

    So, you could say show a spinner ... please wait message when you click the save button which presumably triggers a post-back.

    So, the really simple trick is to attach both a client side click event, and a server side event to the button that triggers the upload.

    So, the proof of concept looks like this:

    markup:

                <asp:Button ID="cmdSave" runat="server" 
                    Text="Button" CssClass="btn"
                    OnClick="cmdSave_Click"
                    OnClientClick="$('#myprogress').show()"
                    />
                <br />
    
                <div id="myprogress" style="display:none">
                    <img src="../Content/wait2.gif" width="64px" />
                    Uploading files - please wait
                </div>
    
    

    And our code behind can fake a "long" process, say a few seconds:

            protected void cmdSave_Click(object sender, EventArgs e)
            {
                System.Threading.Thread.Sleep(4000); // fake a delay
            }
    
    

    And the result looks like this:

    wait6

    Note close how we don't have to re-hide the div and progress area, since after the code behind is done, then a WHOLE NEW web page is sent back from the server, and as a result the div area will automatic revert to its original state (display:none).

    However, I actually think much better idea is to up-load files as they are selected, and that means dumping the built in FileUpLoad control (it is difficult to work with). If you adopt a ajax file uploader, then you don't require a post-back to upload such files. This in theory would allow uploading of several files, but no post-back is required. Of course, this then suggests that when you click on the file (upload) button for a row, then you popup a up-loading dialog of some type.

    0 comments No comments

  2. BeUnique 2,112 Reputation points
    2024-06-07T18:22:43.4466667+00:00

    Hi @Albert Kallal ,

    Thanks for your reply.

    No separate button for save/update outside of the gridview...

    upload button is available inside the gridview. so in this case, can we show the loader for entire gridview...?


  3. Bruce (SqlWork.com) 64,816 Reputation points
    2024-06-07T19:39:39.7133333+00:00

    just under a hidden loader next to the upload button, or if using bootstrap add the loader to the button itself. then on client click event display loading.

    <EditItemTemplate>
       	   <asp:FileUpload ID="fuUpload" runat="server" />
    	   <asp:Label ID="filename" runat="server" />
    	   <asp:Button ID="btnUpload" Text="Upload" 
                  OnClick="Upload" 
                  OnClientClick="$(this).closest('tr').find('img').show()"
                  runat="server" 
                  Style="display: none" />
           <img src="../Content/wait2.gif" width="64px" style="display:none" />
    </EditItemTemplate>
    
    
    0 comments No comments

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.