這種情況是指在幕後處理一個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;
}
}
}