惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - Leo.Zhu

访问google的方法 Postgresql 迁移随笔一 英语词根6 英语词根3 英语词根4 英语词根2 英语词根 1 压力测试工具(Apache) “诸葛马前课-小六壬”全面解析 QT 中中文问题 - Leo.Zhu - 博客园 Ubuntu 尝试 C# 开发Activex ListView + ToolTip 的問題 JQueuy 使用记录---FileTree - Leo.Zhu - 博客园 dataGrid 使用记录 - Leo.Zhu - 博客园 Signals and Slots MS Ajax 客户端编程 学习笔记 (2) MS Ajax 客户端编程 学习笔记 (1) Englive.cn 英语 学习 在线背单词 - 语句本
MS Ajax 客户端编程 学习笔记 (3)
Leo.Zhu · 2008-11-24 · via 博客园 - Leo.Zhu

防止用户多次提交

ajax天性的异步,后台操作的特性使得程序在执行的时候,无法使用户了解当前正在等待的情况,所以用户往往会多次提交,为了避免由于重复提交造成的服务器损失以及用户体验降低,就有必要阻止用户多次提交同一个操作。

在ajax 中可以使用PageRequestManager对应所暴露出的 异步回送各个阶段的事件来判断是否重复提交,PageRequestManager正常时候的事件触发顺序如下:

1.用户点击UpdatePanel中的按钮,触发一个异步的操作

2.PageRequestManager对象触发initializeRequest事件。

3.PageRequestManager对象触发beginRequest事件

4.请求发送到服务器

5.客户端成功收到服务的相应。

6.PageRequestManager对象触发pageLoading事件。

7PageRequestManager对象触发pageLoaded事件。

8.Application对象触发load事件

9PageRequestManager对象触发endRequest时间。

示例:

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Multirequest.aspx.cs" Inherits="Multirequest" %>

<!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">
    <title>无标题页</title>
    <script type="text/javascript">
    var prm;
    var btnsubmitid = '<%=Button1.ClientID %>'
      function pageLoad() {
        prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(onInitializeRequest);
        prm.add_pageLoaded(onPageLoaded);
      }
      function onInitializeRequest(sender,args)
      {
        if(prm.get_isInAsyncPostBack() && args.get_postBackElement().id==btnsubmitid)
        {
            args.set_cancel(true);
            showWaitingInfo(true);
            setTimeout("showWaitingInfo(false)",1500);
        }
      }
    function showWaitingInfo(visible)
    {
        var id = $get("messagePanel");
        id.style.display=visible?"block":"none";
    }
    function onPageLoaded(sender,args)
    {
        showWaitingInfo(false);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
        Msg:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK"
            Width="55px" />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Result:"></asp:Label>
        <asp:Label ID="lblresult" runat="server"></asp:Label>
        <div id="messagePanel" style="display:none;">
            Still Processing,Please be patient.
        </div>
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
    </div>
    </form>
</body>
</html>

image

cs:

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    this.lblresult.Text = this.TextBox1.Text + " &&&&" + this.TextBox2.Text + "TIME:" + System.DateTime.Now.ToLocalTime();
    System.Threading.Thread.Sleep(3000);
}

说明:

function pageLoad() {
      //获取PageRequestManager对象
        prm = Sys.WebForms.PageRequestManager.getInstance();
       //设置事件触发
        prm.add_initializeRequest(onInitializeRequest);
        prm.add_pageLoaded(onPageLoaded);
      }
      function onInitializeRequest(sender,args)
      {
      //判断是否正在异步回送中,并且引发两次操作回送的按钮时同一个。
        if(prm.get_isInAsyncPostBack() && args.get_postBackElement().id==btnsubmitid)
        {
            //取消本次的异步
            args.set_cancel(true);
            //显示信息提示用户
            showWaitingInfo(true);
            //1.5s后隐藏提示信息
            setTimeout("showWaitingInfo(false)",1500);
        }
      }

停止正在执行的异步操作

可以使用PageRequestManager对象的abortPostBack()方法停止当前正在执行的异步操作。

相关code:

function btnCancel_onclick()
{

if(prm.get_isInAsyncPostBack())
{

   prm.abortPostBack();
}

}

在执行异步回送时给用户提示

在PageRequestManager的onBeginRequest的时候,判断PageRequestManager的get_postBackElement().id是否为当前提交的按钮,如果是,则显示waiting的信息,并禁止使用页面等。

当onPageLoaded时,恢复页面。

在异步执行时的异常处理

处理PageRequestManager的onEndRequest事件,例如:

function onEndRequest(sender,args)
{

var error = args.get_error();

if(error)
{

//显示异常。

$get(“messagePanel”).innerHTML = error.message';

//告知asp.net ajax Client framework error 已经处理。

args.set_errorHandled(true);
}
}