How to display a loading image until a gridview is fully loaded without Ajax Toolkit?

前端 未结 6 677
一个人的身影
一个人的身影 2021-01-03 06:10

QUESTION

Can anyone suggest how a loading image can be displayed until a gridview is fully loaded?

This gridview is to be rendered on page

相关标签:
6条回答
  • 2021-01-03 06:11

    As you are not willing to use the ASP.NET Update panel which is designed to handle the kind of requirement you have. Using jquery AJAX and using the web method to update the Grid will not work as you expect because you need to update the viewstate and other information. One of the solution that you can apply is showing the modal dialog box till your page is completed loaded its html which includes your Grid data.

                  <script type = "javascript/text">
                  $(document).ready(function() { // show modal dialog });
                  $(window).load(function() {// hide the dialog box});
                  </script> 
    
    0 讨论(0)
  • 2021-01-03 06:16

    Use beforeSend in conjunction with success and error like so:

    $(function() {
      $.ajax({
        type: "POST",
        url: "Default.aspx/UpdateGV",
        data: "{}",
        contentType: "application/json",
        dataType: "json",
        beforeSend: function() {
            $("#loader").show();
        },
        success: function() {
            $("#loader").hide();
            // Run return method.
        },
        // it's good to have an error fallback
        error: function(jqhxr, stat, err) {
           $("#loader").hide();
           $("#error").show();
        }
      });
    });
    

    (provided you have <img id="loader" src="..." /> and <img id="error" src="..." />)

    complete fires after success and error so using both success and error instead of complete and error assures no overlap.

    Hope this was what you were looking for!

    0 讨论(0)
  • 2021-01-03 06:25

    I would implement this by using a web api controller that returns the appropriate data and then simply try to load the data on document.ready with jquery. Something along the lines of this (Will be posting the answer in c# but it should be simple enough to translate):

     public class TableDataController : ApiController
      {
         public IEnumerable<SimplePocoForOneTableRow> Get()
         {
            var tableData = GetTableDataFromSomewhere();
    
            return tableData;
         }
      }
    

    To learn more about how to setup a WebApiController in a web forms project, please refer to this article.

    I would implement the HTML something like this:

    <table class="tableToLoadStuffInto">
       <tr>
           <td><img src="yourCoolSpinningAjaxLoader.gif" /></td>
       <tr>
    </table>
    

    And the jquery to load the data and present it in the table would look something like this:

    <script type="text/javascript">
    $(document).ready(function () {
        jQuery.ajax({
                url: '/api/TableData',
                type: 'GET',
                contentType: 'application/json; charset=utf-8',
                success: function (result) {
                if(!result) {
                    //Show some error message, we didn't get any data
                }                    
    
                var tableData = JSON.parse(result);
                var tableHtml = '';
                for(var i = 0; i<tableData.length; i++){
                        var tableRow = tableData[i];
                        tableHtml = '<tr><td>' + 
                        tableRow.AwesomeTablePropertyOnYourObject +
                        '</td></'tr>'
                   }    
                 var table = $('.tableToLoadStuffInto');
                 //Could do some fancy fading and stuff here
                 table.find('tr').remove();
                 table.append(tableHtml);
                }
            });
    });
    </script>
    

    To tidy the string concatenation of the jQuery up a bit you could use a template like handlebars, but it's not strictly necessary.

    0 讨论(0)
  • 2021-01-03 06:28

    From your code :

    $(function() {
      $.ajax({
        type: "POST",
        url: "Default.aspx/UpdateGV",
        data: "{}",
        contentType: "application/json",
        dataType: "json",
        success: function() {
          // Run return method.
        },
        // add these lines
        beforeSend:function {
           //this will show your image loader
           $("$Loader").css("display","block");
        },
        complete:function {
           //this will hide your image loader provided that you give an id named Loader to your image
           $("$Loader").css("display","none");
        }
      });
    });
    
    0 讨论(0)
  • 2021-01-03 06:35

    You should load grid in another page and call that page in a div on parent page and display required loading image while loading div as per below code:

      $(document).ready(function()
       {
     $("#loader").show();
     $.ajax({
        type: "GET",
        url: "LoadGrid.aspx",
        data: "{}",
        contentType: "application/json",
        dataType: "json",   
        success: function(data) {
            $("#loader").hide();
           $(#dvgrid).html(data);
        },
        // it's good to have an error fallback
        error: function(jqhxr, stat, err) {
           $("#loader").hide();
     $(#dvgrid).html('');
           $("#error").show();
        }
      });
    });
    

    In LoadGrid.aspx you should load grid by normal C# code. and simply your parent page will call the LoadGrid.aspx and rendered html will display in parent div with loading image...

    0 讨论(0)
  • 2021-01-03 06:37

    You can achieve this using jquery, there is a jquery block UI project on github and you could just use that one to block the grid view without using ajax.

    here is the code you will need to do this, and it' tested and works fine like below:

    Step 1 add these two lines in your page head

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
      <script src="http://malsup.github.io/jquery.blockUI.js"></script>
    

    Step 2 an extension jquery after the codes above:

    <script type="text/javascript">
        $(document).ready(function () {
            $('#Button1').click(function () {
                $('.blockMe').block({
                    message: 'Please wait...<br /><img src="Images/loadingBar.gif" />',
                    css: { padding: '10px' }
                });
            });
        });
    </script>
    

    Step 3 in my case I bind my grid view using a button , but you could always use any other controls as well:

    <asp:Button ID="Button1" runat="server" Text="Bind Grid View" 
                ClientIDMode="Static" OnClick="Button1_Click" />
        <div class="blockMe">
            <asp:GridView ID="GridView1" runat="server" Width="100%">
            </asp:GridView>
        </div>
    

    Step 4 bind the grid view on button clicked

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable tblCourse = myAccount.GetEnroledCourse("arpl4113");
    
            //Bind courses
            GridView1.DataSource = tblCourse;
            GridView1.DataBind();
        }
    

    and that's it, NO AJAX Toolkit (only jQuery) so the result will be look like this:

    enter image description here


    A trick to do the above solution at the page load

    First of all this is NOT a function on Page_Load event on server side but on the client side ;-)

    So to achieve this you need to have a hidden control to keep a value on page view-state and also make the same button hidden to trigger it on page load. and a little changes on the extension jQuery above. Done!

    Step 1. Add the css below to your page header:

     <style> .hidden { display: none; } </style>
    

    Step 2. Add a hidden field plus make your button hidden like this:

        <asp:HiddenField ID="hidTrigger" runat="server" ClientIDMode="Static" Value="" />
        <asp:Button ID="btnHidden" runat="server" ClientIDMode="Static" 
                    OnClick="btnHidden_Click" CssClass="hidden" />
        <div class="blockMe">
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>
        </div>
    

    Step 3. Make this changes on your extension script like below:

      <script type="text/javascript">
        $(document).ready(function () {
    
            //check whether the gridview has loaded
            if ($("#hidTrigger").val() != "fired") {
    
                //set the hidden field as fired to prevent multiple loading
                $("#hidTrigger").val("fired");
    
                //block the gridview area
                $('.blockMe').block({
                    message: 'Please wait...<br /><img src="Images/loadingBar.gif" />',
                    css: { padding: '10px' }
                });
    
                //fire the hidden button trigger
                $('#btnHidden').click();
            }
        });
    </script>
    

    That's it! and your button click on the code behind remains the same as before, in this example I only change the name of the button. no code on Page_Load event though.

        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void btnHidden_Click(object sender, EventArgs e)
        {
            DataTable tblCourse = myAccount.GetEnroledCourse("arpl4113");
    
            //Bind courses
            GridView1.DataSource = tblCourse;
            GridView1.DataBind();
        }
    

    I hope it helps you.

    0 讨论(0)
提交回复
热议问题