ASP.NET在後台代碼實現個功能,根據選擇提示使用者是否繼續執行操作

來源:互聯網
上載者:User

這種情況是指在幕後處理一個HTTP Post命令時將時序重新交給瀏覽器端,然後運行javascript,然後再回到伺服器端,然後繼續進行後邊的操作。如果你會畫時序圖,就可以發現這種有著兩個來回通訊的程式比大多數所知道的那種只有一個來回的通訊的程式要複雜(雖然代碼並不多多少)。這裡用到.NET的IPostBackEventHandler介面。

給個列子如下(關鍵代碼紅色標明):

 

protected void btnAddGift_Click(object sender, EventArgs e)
    {
        string inventoryIdStr = string.Empty;
        string inventoryQtyStr = string.Empty;
        if (GiftRepeater.Items.Count > 0)
        {
            foreach (RepeaterItem item in GiftRepeater.Items)
            {
                string inventoryIdStr1 = string.Empty;
                string inventoryQtyStr1 = string.Empty;
                Repeater GiftSizeRepeater = item.FindControl("SelectSizeRepeater") as Repeater;
                if (GiftSizeRepeater.Items.Count > 0)
                {
                    foreach (RepeaterItem item1 in GiftSizeRepeater.Items)
                    {
                        int inventoryQty = 0;
                        int inventoryQty1 = 0;
                        HiddenField ProductQuentityHiddenField = item1.FindControl("ProductQuentityHiddenField") as HiddenField;
                        TextBox InventoryTextBox = item1.FindControl("InventoryTextBox") as TextBox;
                        HiddenField QtyHiddenField = item1.FindControl("QtyHiddenField") as HiddenField;
                        int.TryParse(QtyHiddenField.Value, out inventoryQty1);
                        if (!string.IsNullOrEmpty(InventoryTextBox.Text))
                        {
                            if (!int.TryParse(InventoryTextBox.Text, out inventoryQty))
                            {
                                MessageLabel.Text = "請確認您輸入的贈品數量格式真確!";
                                return;
                            }
                            if (inventoryQty > inventoryQty1)
                            {
                                MessageLabel.Text = "您輸入的某贈品數量大於當前庫存,請重新選擇或減少輸入贈品數量!";
                                return;
                            }
                            if (inventoryQty > 0)
                            {
                                inventoryIdStr1 = inventoryIdStr1 + ProductQuentityHiddenField.Value + ",";
                                inventoryQtyStr1 = inventoryQtyStr1 + inventoryQty + ",";
                            }
                        }
                    }
                }
                inventoryIdStr = inventoryIdStr + inventoryIdStr1;
                inventoryQtyStr = inventoryQtyStr + inventoryQtyStr1;
            }
        }
        if (!string.IsNullOrEmpty(inventoryIdStr))
        {
            inventoryIdStr = inventoryIdStr.TrimEnd(',');
        }
        if (!string.IsNullOrEmpty(inventoryQtyStr))
        {
            inventoryQtyStr = inventoryQtyStr.TrimEnd(',');
        }
        int giftcount = GiftCount * Times;
        int selectcount = 0;
        if (!string.IsNullOrEmpty(inventoryQtyStr))
        {
            string[] arrs = inventoryQtyStr.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < arrs.Length; i++)
            {
                selectcount = selectcount + int.Parse(arrs[i].ToString());
            }
        }
        if (giftcount > selectcount)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "next step",
           "if(confirm('您所選的贈品小于贈送的數量,確認只選著這些贈品嗎?'))" + this.Page.ClientScript.GetPostBackEventReference(this, "secondnext"), true);
            return;
        }
        if (giftcount < selectcount)
        {
            MessageLabel.Text = "你所選的贈品數量不允許大于贈送的數量!";
            return;
        }
        string result = ShoppingCardManager.AddShoppingCardForPromotionGift(inventoryIdStr, inventoryQtyStr,UserId, 2);
        if (result == "1")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "CloseWithRefreshParent();", true);
        }
        else
        {
            MessageLabel.Text = "操作失敗,請重新操作!";
            return;
        }

    }

 

//這個是實現IPostBackEventHandler介面RaisePostBackEvent方法。

個人理解是:這個介面是.NET(小)架構設計專門為滿足前後台轉變的一種設計。當.NET把這些對象轉化以後,在用戶端點擊一個POST請求時,如果這個POST請求的執行事件代碼中出現了IPostBackEventHandler介面中特定的標記GetPostBackEventReference這種方法時,.NET會把當前這個POST時序執行完同時返回給用戶端遊覽器但這個時候同時會讓用戶端自動再執行一個POST請求,這時這個請求就會去找RaisePostBackEvent方法去執行,然後在返回給用戶端遊覽器。以上只代表個人對這個(小)架構設計的理解。

public void RaisePostBackEvent(string eventArgument)
    {
        if (eventArgument == "secondnext")
        {
            string inventoryIdStr = string.Empty;
            string inventoryQtyStr = string.Empty;
            if (GiftRepeater.Items.Count > 0)
            {
                foreach (RepeaterItem item in GiftRepeater.Items)
                {
                    string inventoryIdStr1 = string.Empty;
                    string inventoryQtyStr1 = string.Empty;
                    Repeater GiftSizeRepeater = item.FindControl("SelectSizeRepeater") as Repeater;
                    if (GiftSizeRepeater.Items.Count > 0)
                    {
                        foreach (RepeaterItem item1 in GiftSizeRepeater.Items)
                        {
                            int inventoryQty = 0;
                            HiddenField ProductQuentityHiddenField = item1.FindControl("ProductQuentityHiddenField") as HiddenField;
                            TextBox InventoryTextBox = item1.FindControl("InventoryTextBox") as TextBox;
                            if (!string.IsNullOrEmpty(InventoryTextBox.Text))
                            {
                                if (!int.TryParse(InventoryTextBox.Text, out inventoryQty))
                                {
                                    MessageLabel.Text = "請確認您輸入的贈品數量格式真確!";
                                    return;
                                }
                                if (inventoryQty > 0)
                                {
                                    inventoryIdStr1 = inventoryIdStr1 + ProductQuentityHiddenField.Value + ",";
                                    inventoryQtyStr1 = inventoryQtyStr1 + inventoryQty + ",";
                                }
                            }
                        }
                    }
                    inventoryIdStr = inventoryIdStr + inventoryIdStr1;
                    inventoryQtyStr = inventoryQtyStr + inventoryQtyStr1;
                }
            }
            if (!string.IsNullOrEmpty(inventoryIdStr))
            {
                inventoryIdStr = inventoryIdStr.TrimEnd(',');
            }
            if (!string.IsNullOrEmpty(inventoryQtyStr))
            {
                inventoryQtyStr = inventoryQtyStr.TrimEnd(',');
                int giftcount = GiftCount * Times;
                int selectcount = 0;
                if (!string.IsNullOrEmpty(inventoryQtyStr))
                {
                    string[] arrs = inventoryQtyStr.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < arrs.Length; i++)
                    {
                        selectcount = selectcount + int.Parse(arrs[i].ToString());
                    }
                }
            }
            string result = ShoppingCardManager.AddShoppingCardForPromotionGift(inventoryIdStr, inventoryQtyStr, UserId, 2);
            if (result == "1")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "CloseWithRefreshParent();", true);
            }
            else
            {
                MessageLabel.Text = "操作失敗,請重新操作!";
                return;
            }
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.