ASP.NET在后台代码实现个功能,根据选择提示用户是否继续执行操作
2012-02-29 18:17
1071 查看
这种情况是指在后台处理一个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;
}
}
}
给个列子如下(关键代码红色标明):
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;
}
}
}
相关文章推荐
- ASP.NET提示框,且根据用户选择执行不同代码的实现
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- asp.net 调用JS 实现客户端用户选择提示功能
- ASP.NET中服务器控件Button调用javascript代码运用正则表达式验证TextBox中输入的是否为正整数或正小数,符合要求则继续运行服务器端代码功能的实现
- asp.net+Ajax校验用户是否存在的实现代码
- asp.net 后台cs代码怎么操作前台用ajax实现下拉框进行搜索
- 根据JS判断是否执行C#后台代码(后台弹出确定/取消提示窗口)
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- asp.net+Ajax校验用户是否存在的实现代码
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 在asp.net中弹出一个提示框,单击“是”则继续执行操作,单击“否”则终止操作
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- Asp.net实现在线人数统计功能代码实例
- asp.net web 后台判断提示框,点击'是'执行代码A(),点击'否'执行代码B()
- [导入]Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net(C#)实现验证码功能代码
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Ext 上传文件,前后台实现, Asp.net 代码
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]