ASP.NET Tutorial/Ajax/Asynchronous Event — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:57, 26 мая 2010

Aborting the Previous Asynchronous Postback

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void btnGetFortune_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(3000); // wait 3 seconds
        lblFortune.Text = String.Format("At {0:T}, the oracle says: ", DateTime.Now);
        Random rnd = new Random();
        switch (rnd.Next(4))
        {
            case 0:
                lblFortune.Text += "You"re doomed!";
                break;
            case 1:
                lblFortune.Text += "Good luck is around the corner.";
                break;
            case 2:
                lblFortune.Text += "Don"t leave home.";
                break;
            case 3:
                lblFortune.Text += "Buy stock today.";
                break;
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>UpdatePanel Abort</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="up1" runat="server">
        <ContentTemplate>
            <asp:Button
                id="btnGetFortune"
                Text="Get Fortune"
                OnClick="btnGetFortune_Click"
                Runat="server" />
            <asp:Button
                id="btnCancel"
                Text="Cancel"
                Enabled="false"
                Runat="server" />
                <br />
                <asp:Label ID="lblFortune" runat="server" />
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
    <script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(prm_initializeRequest);
    function prm_initializeRequest(sender, args)
    {
        if (args.get_postBackElement().id == "btnCancel")
        {
            prm.abortPostBack();
            alert("Fortune Aborted!");
        }
        else
        {
            $get("btnCancel").disabled = false;
        }
     }
    </script>
</body>
</html>


Canceling the Current Asynchronous Postback

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(3000); // sleep 3 seconds
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>UpdatePanel Cancel</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="up1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <%= DateTime.Now.ToString("T") %>
        <asp:Button
            id="btnSubmit1"
            Text="Submit 1"
            OnClick="btnSubmit_Click"
            Runat="server"/>
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="up2" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <%= DateTime.Now.ToString("T") %>
        <asp:Button
            id="btnSubmit2"
            Text="Submit 2"
            OnClick="btnSubmit_Click"
            Runat="server" />
    </ContentTemplate>
    </asp:UpdatePanel>
    </form>
    <script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest( prm_initializeRequest );
    function prm_initializeRequest(sender, args)
    {
        if (prm.get_isInAsyncPostBack())
        {
            alert("Still Processing First Request");
            args.set_cancel(true);
        }
    }
    </script>
</body>
</html>


Passing Additional Information During an Asynchronous Postback

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void dtlProduct_DataBound(object sender, EventArgs e)
    {
        string productTitle = (string)DataBinder.Eval(dtlProduct.DataItem, "Title");
        if (sm1.IsInAsyncPostBack)
        {
            sm1.RegisterDataItem(Head1, productTitle);
        }
        else
        {
            Head1.Title = productTitle;
            hTitle.InnerHtml = productTitle;
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>UpdatePanel DataItem</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager
        id="sm1"
        Runat="server" />
    <h1 id="hTitle" runat="server"></h1>

    <asp:UpdatePanel
        id="upSearch"
        Runat="server">
        <ContentTemplate>
        <asp:DetailsView
            id="dtlProduct"
            DataSourceID="srcProducts"
            AllowPaging="true"
            Runat="server" OnDataBound="dtlProduct_DataBound" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:con %>"
        SelectCommand="SELECT Id,Title,Director FROM Product"
        Runat="server" />
    </div>
    </form>
    <script type="text/javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded( prm_pageLoaded );
    function prm_pageLoaded(sender, args)
    {
        if (prm.get_isInAsyncPostBack())
        {
            var productTitle = args.get_dataItems()["Head1"];
            document.title = productTitle;
            $get("hTitle").innerHTML = productTitle;
        }
    }
    </script>
</body>
</html>