ASP.NET在后台代码实现个功能,根据选择提示用户是否继续执行操作

这种情况是指在后台处理一个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;
            }
        }
    }

 

原文地址:http://www.cnblogs.com/scottpei/archive/2012/02/29/2373879.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。