背景:在做asp.net項目時,遇到頁面開啟載入資料的時候,如果資料載入過慢,就會導致頁面出現空白,在網上也尋找了一些資料,都是用的js類比的,尋找了一陣子,未果。
所以自己思考了下,自己寫了個。
實現原理:
頁面中添加一個UpdatePanel,裡面添加updateProgress,並在這裡添加載入時候的動畫頁面,在updatePanel裡面添加一個按鈕,類比點擊事件。暈了,快說不明白了,直接上代碼吧,代碼如下:
前台代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DevDetailPage.aspx.cs" Inherits="ManageService.DevDetailPage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title></title> <link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" /> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <style type="text/css"> body { margin: 0; padding: 0; } #upLoading{height:2px} </style> <script type="text/javascript"> window.onload = function () { $("#LoadData").click(); } </script></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="upLoading" runat="server"> <ContentTemplate> <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="upLoading"> <ProgressTemplate> <img src="Images/loadinfo.gif" />正在載入資料... </ProgressTemplate> </asp:UpdateProgress> <asp:Button ID="LoadData" runat="server" Text="Button" onclick="LoadData_Click" style="display:none"/> </ContentTemplate> </asp:UpdatePanel> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div id="allData" runat="Server" visible="false"> 編寫控制項綁定資料 </div> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
後台代碼:
protected void LoadData_Click(object sender, EventArgs e) { if (Request.QueryString.Count > 0) { //綁定資料 BindData(); allData.Visible = true; } }
總結:一句話就是在在updatePanel裡面添加按鈕,在載入的時候執行按鈕事件擷取資料,
覺得這種方法還是不大好,如有更好的方法請說下