日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

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

發(fā)布時(shí)間:2023/11/29 asp.net 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转载:ASP.NET在后台代码实现个功能,根据选择提示用户是否继续执行操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這種情況是指在后臺(tái)處理一個(gè)HTTP Post命令時(shí)將時(shí)序重新交給瀏覽器端,然后運(yùn)行javascript,然后再回到服務(wù)器端,然后繼續(xù)進(jìn)行后邊的操作。如果你會(huì)畫(huà)時(shí)序圖,就可以發(fā)現(xiàn)這種有著兩個(gè)來(lái)回通訊的程序比大多數(shù)所知道的那種只有一個(gè)來(lái)回的通訊的程序要復(fù)雜(雖然代碼并不多多少)。這里用到.NET的IPostBackEventHandler接口。

給個(gè)列子如下(關(guān)鍵代碼紅色標(biāo)明):

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 = "請(qǐng)確認(rèn)您輸入的贈(zèng)品數(shù)量格式真確!";
??????????????????????????????? return;
??????????????????????????? }
??????????????????????????? if (inventoryQty > inventoryQty1)
??????????????????????????? {
??????????????????????????????? MessageLabel.Text = "您輸入的某贈(zèng)品數(shù)量大于當(dāng)前庫(kù)存,請(qǐng)重新選擇或減少輸入贈(zèng)品數(shù)量!";
??????????????????????????????? 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('您所選的贈(zèng)品小于贈(zèng)送的數(shù)量,確認(rèn)只選著這些贈(zèng)品嗎?'))" + this.Page.ClientScript.GetPostBackEventReference(this, "secondnext"), true);
??????????? return;
??????? }
??????? if (giftcount < selectcount)
??????? {
??????????? MessageLabel.Text = "你所選的贈(zèng)品數(shù)量不允許大于贈(zèng)送的數(shù)量!";
??????????? return;
??????? }
??????? string result = ShoppingCardManager.AddShoppingCardForPromotionGift(inventoryIdStr, inventoryQtyStr,UserId, 2);
??????? if (result == "1")
??????? {
??????????? ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", "CloseWithRefreshParent();", true);
??????? }
??????? else
??????? {
??????????? MessageLabel.Text = "操作失敗,請(qǐng)重新操作!";
??????????? return;
??????? }

??? }

//這個(gè)是實(shí)現(xiàn)IPostBackEventHandler接口RaisePostBackEvent方法。

個(gè)人理解是:這個(gè)接口是.NET(小)架構(gòu)設(shè)計(jì)專門(mén)為滿足前后臺(tái)轉(zhuǎn)變的一種設(shè)計(jì)。當(dāng).NET把這些對(duì)象轉(zhuǎn)化以后,在客戶端點(diǎn)擊一個(gè)POST請(qǐng)求時(shí),如果這個(gè)POST請(qǐng)求的執(zhí)行事件代碼中出現(xiàn)了IPostBackEventHandler接口中特定的標(biāo)記GetPostBackEventReference這種方法時(shí),.NET會(huì)把當(dāng)前這個(gè)POST時(shí)序執(zhí)行完同時(shí)返回給客戶端游覽器但這個(gè)時(shí)候同時(shí)會(huì)讓客戶端自動(dòng)再執(zhí)行一個(gè)POST請(qǐng)求,這時(shí)這個(gè)請(qǐng)求就會(huì)去找RaisePostBackEvent方法去執(zhí)行,然后在返回給客戶端游覽器。以上只代表個(gè)人對(duì)這個(gè)(小)架構(gòu)設(shè)計(jì)的理解。

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 = "請(qǐng)確認(rèn)您輸入的贈(zèng)品數(shù)量格式真確!";
??????????????????????????????????? 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 = "操作失敗,請(qǐng)重新操作!";
??????????????? return;
??????????? }
??????? }
??? }

?

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

轉(zhuǎn)載于:https://www.cnblogs.com/Jasson-he/p/4377398.html

總結(jié)

以上是生活随笔為你收集整理的转载:ASP.NET在后台代码实现个功能,根据选择提示用户是否继续执行操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。