什么是最简单的方法来创建“请等待”或类似于ASP.NET中的东西 [英] What is the easyest way to create "please wait" or something like that in ASP.NET

查看:92
本文介绍了什么是最简单的方法来创建“请等待”或类似于ASP.NET中的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ASP.NET服务器与aspx页面。
它通过按钮单击使文件起作用的动作。
我想告诉客户行动时请等待。
当动作完成时,我想告诉客户我们完成并返回到我们之前的aspx页面(带按钮的页面)



服务器端:

  using System; 
使用System.Collections.Generic;
使用System.Diagnostics;
使用System.IO;
使用System.Linq;
使用System.Threading;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;

命名空间WebApplication1
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);
}

protected void Button1_Click(object sender,EventArgs e)
{
//FindControl(UpdateProgress1.ID).Visible = true;
Thread.Sleep(2000);
//FindControl(UpdateProgress1.ID).Visible = false;
UpdateProgress1.Attributes.Add(style,display:none);
l1.Attributes.Add(style,display:none);
//l1.Visible = false;
sendBackToUser();
}

private void sendBackToUser()
{
FileInfo file = new FileInfo(@C:\F\f.xlsx);
if(file.Exists)
{
//Response.Clear();
//Response.ClearHeaders();


//Response.ClearContent();
Response.AddHeader(content-disposition,attachment; filename =+ @C:\F\f.xlsx);
//Response.AddHeader(\"Content-Type,application / Excel);
//Response.ContentType =application / vnd.xlsx;
//Response.AddHeader(\"Content-Length,file.Length.ToString());
//Response.WriteFile(file.FullName);
//Response.End();
}
else
{
Response.Write(此文件不存在);
}
}
}

}



客户端:

 <%@ Page Language =C#AutoEventWireup = trueCodeBehind =WebForm1.aspx.csInherits =WebApplication1.WebForm1%> 

<!DOCTYPE html>

< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title>< / title>
< / head>
< body>
< form id =form1runat =server>
< asp:ScriptManager ID =ScriptManager1runat =server>< / asp:ScriptManager>
< asp:UpdatePanel runat =serverID =UpdatePanel1>
<触发器>
< asp:PostBackTrigger ControlID =Button1/>
< / Triggers>
< ContentTemplate>
< asp:Button ID =Button1runat =serverOnClick =Button1_ClickText =ButtonOnClientClick =ShowProgress()/>
< / ContentTemplate>
< / asp:UpdatePanel>
< asp:UpdateProgress ID =UpdateProgress1style =display:nonerunat =server>
< ProgressTemplate>
该报告正在创建请稍候...
< / ProgressTemplate>
< / asp:UpdateProgress>

< / form>
< asp:Label runat =serverID =l1style =display:none>报告正在创建请稍候...< / asp:Label>


< script type =text / javascript>
document.getElementById('<%Response.Write(l1.ID);%>')。style.display =none;
document.getElementById('<%Response.Write(UpdateProgress1.ClientID);%>')。style.display =none;
function ShowProgress()
{
document.getElementById('<%Response.Write(l1.ID);%>')。style.display =inline;
document.getElementById('<%Response.Write(UpdateProgress1.ClientID);%>')。style.display =inline;
}
< / script>

< / body>
< / html>


解决方案

使用更新面板:

 < asp:UpdatePanel runat =serverID =UpdatePanel1> 
< ContentTemplate>
<! - 您的页面标记在这里 - >
< / ContentTemplate>
< / asp:UpdatePanel>

添加更新进度控件,让您等待一下

 < asp:UpdateProgress ID =UpdateProgress1runat =serverAssociatedUpdatePanelID =UpdatePanel1
DisplayAfter =10>
< ProgressTemplate>
< asp:Panel ID =panProcessingrunat =serverCssClass =processing>
< asp:Image ID =imgProcessingrunat =serverImageUrl =〜/ Images / processingsmall.gif
AlternateText =Processing/>< br />
< br />
< strong>处理...< / strong>
< / asp:Panel>
< / ProgressTemplate>
< / asp:UpdateProgress>

您可以替换< asp:Panel 在UpdateProgress中,无论您想要什么,但本质上,如果操作花费的时间超过 DisplayAfter ,UpdateProgress将在页面上显示其内容。



这通常会显示在包含该消息的页面内容之上的模态窗口,但您可以将其设计为喜欢。



该点ASP是在这里重视的。


I have ASP.NET server with aspx page. It make actions that work with files by button click. I want to tell the client "wait please when actions run". When the actions finish I want to tell the client that we finish and go back to aspx page we was before (the page with the button)

server side :

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button1);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //FindControl(UpdateProgress1.ID).Visible = true;
            Thread.Sleep(2000);
            //FindControl(UpdateProgress1.ID).Visible = false;
            UpdateProgress1.Attributes.Add("style", "display:none");
            l1.Attributes.Add("style", "display:none");
            //l1.Visible = false;
            sendBackToUser();
        }

        private void sendBackToUser()
        {
            FileInfo file = new FileInfo(@"C:\F\f.xlsx");
            if (file.Exists)
            {
                //Response.Clear();
                //Response.ClearHeaders();


    //Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + @"C:\F\f.xlsx");
            //Response.AddHeader("Content-Type", "application/Excel");
            //Response.ContentType = "application/vnd.xlsx";
            //Response.AddHeader("Content-Length", file.Length.ToString());
            //Response.WriteFile(file.FullName);
            //Response.End();
        }
        else
        {
            Response.Write("This file does not exist.");
        }
    }
}

}

client side :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel runat="server" ID="UpdatePanel1">
            <Triggers>
                <asp:PostBackTrigger ControlID="Button1" />
            </Triggers>
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="ShowProgress()"/>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress1" style="display:none" runat="server" >
            <ProgressTemplate>
               The report is creating please wait...        
            </ProgressTemplate>
        </asp:UpdateProgress>

    </form>
    <asp:Label runat="server" ID="l1" style="display:none">The report is creating please wait...   </asp:Label>


    <script type="text/javascript">
        document.getElementById('<% Response.Write(l1.ID); %>').style.display = "none";
        document.getElementById('<% Response.Write(UpdateProgress1.ClientID); %>').style.display = "none";
    function ShowProgress()
    {
        document.getElementById('<% Response.Write(l1.ID); %>').style.display = "inline";
        document.getElementById('<% Response.Write(UpdateProgress1.ClientID); %>').style.display = "inline";
    }
    </script>

</body>
</html>

解决方案

Use an update panel:

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <ContentTemplate>
        <!-- Your page markup goes here -->
    </ContentTemplate>
</asp:UpdatePanel>

Add the Update Progress control to give you the "please wait" bit

<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
    DisplayAfter="10">
    <ProgressTemplate>
        <asp:Panel ID="panProcessing" runat="server" CssClass="processing">
            <asp:Image ID="imgProcessing" runat="server" ImageUrl="~/Images/processingsmall.gif"
                AlternateText="Processing" /><br />
            <br />
            <strong>Processing...</strong>
        </asp:Panel>
    </ProgressTemplate>
</asp:UpdateProgress>

You can replace the <asp:Panel in the UpdateProgress with whatever you want but essentially, if the operation is taking longer than DisplayAfter, the UpdateProgress will display its content on the page.

This will normally appear as a modal window above the page content that contains the message but you can style it however you like.

The point is that ASP takes care of the heavy lifting here.

这篇关于什么是最简单的方法来创建“请等待”或类似于ASP.NET中的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆