怎么在页面还没有加载完成时候,在页面上显示一个:"数据加载中......"

http://topic.csdn.net/u/20080123/09/5b720dc5-9cd7-437c-84de-e7f4967c92e0.html

 

你新建2个页Default.aspx/Progress.aspx
第一个页加一个Button:

HTML code
<asp:Button id="Button1" runat="server" Text="Start Long Task!"></asp:Button>


后台代码:
C# code

protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

private void Button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread thread
=new System.Threading.Thread(new System.Threading.ThreadStart(LongTask));
thread.Start();
Session[
"State"]=1;
OpenProgressBar(
this.Page);
}

private void LongTask()
{
//模拟长时间任务
//每个循环模拟任务进行到不同的阶段
for(int i=0;i<11;i++)
{
System.Threading.Thread.Sleep(
1000);
//设置每个阶段的state值,用来显示当前的进度
Session["State"] = i+1;
}
//任务结束
Session["State"] = 100;
}

public static void OpenProgressBar(System.Web.UI.Page Page)
{
StringBuilder sbScript
= new StringBuilder();

sbScript.Append(
"<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append(
"<!--\n");
//需要IE5.5以上支持
// sbScript.Append("window.showModalDialog('Progress.aspx','','dialogHeight: 100px; dialogWidth: 350px; edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:No;');\n");
//IE5.5以下使用window.open(Windows2003/Xp的默认版本下)
sbScript.Append("window.open('Progress.aspx','', 'height=100, width=350, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');\n");
sbScript.Append(
"// -->\n");
sbScript.Append(
"</script>\n");
Page.RegisterClientScriptBlock(
"OpenProgressBar", sbScript.ToString());
}



第2页加个Panel和Label:
HTML code

<asp:Label id="lblMessages" runat="server"></asp:Label>
<asp:Panel id="panelBarSide" runat="server" Width="300px" BorderStyle="Solid" BorderWidth="1px"
ForeColor
="Silver">
<asp:Panel id="panelProgress" Runat="server"></asp:Panel>
</asp:Panel>
<asp:Label ID="lblPercent" Runat="server" ForeColor="Blue"></asp:Label>


后台代码:
C# code

protected System.Web.UI.WebControls.Label lblMessages;
protected System.Web.UI.WebControls.Panel panelProgress;
protected System.Web.UI.WebControls.Label lblPercent;
protected System.Web.UI.WebControls.Panel panelBarSide;

private int state = 0;

private void Page_Load(object sender, System.EventArgs e)
{
if(Session["State"]!=null)
{
state
= Convert.ToInt32(Session["State"].ToString());
}
else
{
Session[
"State"]=0;
}
if(state>0&&state<=10)
{
this.lblMessages.Text = "Task undertaking!";
this.panelProgress.Width = state*30;
this.lblPercent.Text = state*10 + "%";
Page.RegisterStartupScript(
"","<script>window.setTimeout('window.Form1.submit()',100);</script>");
}
if(state==100)
{
this.panelProgress.Visible = false;
this.panelBarSide.Visible = false;
this.lblMessages.Text = "Task Completed!";
Page.RegisterStartupScript(
"","<script>window.close();</script>");
}
}



注:请稍微修改一下代码,别想拿来就用-_-

http://topic.csdn.net/u/20091125/14/0e6efb00-31ac-4143-8d5e-d20cc2e99c0e.html?seed=307734643&r=61425307#r_61425307 

 

posted on 2009-11-25 15:55  风雨者2  阅读(2737)  评论(0编辑  收藏  举报

导航