解决方案如下
1.设置Page指令中 EnableEventValidation="false",否则会提示错误,具体请看MSDN
2.给Page继承 IPostBackEventHandler接口,实现 public void RaisePostBackEvent(string eventArgument)方法
Code1
1public void RaisePostBackEvent(string eventArgument)
2 {
3 using (System.IO.StreamWriter sw = new System.IO.StreamWriter("E:\\test.txt", true))
4 {
5 sw.WriteLine(eventArgument);
6 }
7 }
3.然后在需要的地方注册回发脚本,点击确认将执行RaisePostBackEvent里的逻辑,jsCancel用于触发是一个隐藏的按钮的回发事件,比如取消操作
1public void RaisePostBackEvent(string eventArgument)
2 {
3 using (System.IO.StreamWriter sw = new System.IO.StreamWriter("E:\\test.txt", true))
4 {
5 sw.WriteLine(eventArgument);
6 }
7 }
RegisterPostbackScript
1string jsContinue = "";
2 string jsCancel = ClientScript.GetPostBackEventReference(btnCancel, "");
3 string jsConfirm = "";
4 if (!IsPostBack)
5 {
6 for (int i = 0; i < 10; i++)
7 {
8 if (i % 2 == 0)
9 {
// 注册继续操作的脚本,触发页面的回发事件,传递参数i
10 jsContinue = ClientScript.GetPostBackEventReference(this, i.ToString());
11 jsConfirm = string.Format("if (confirm('是否继续操作')) {0}; else {1};", jsContinue, jsCancel);
12 ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), jsConfirm, true);
13 }
14 }
15 }
1string jsContinue = "";
2 string jsCancel = ClientScript.GetPostBackEventReference(btnCancel, "");
3 string jsConfirm = "";
4 if (!IsPostBack)
5 {
6 for (int i = 0; i < 10; i++)
7 {
8 if (i % 2 == 0)
9 {
// 注册继续操作的脚本,触发页面的回发事件,传递参数i
10 jsContinue = ClientScript.GetPostBackEventReference(this, i.ToString());
11 jsConfirm = string.Format("if (confirm('是否继续操作')) {0}; else {1};", jsContinue, jsCancel);
12 ClientScript.RegisterClientScriptBlock(GetType(), Guid.NewGuid().ToString(), jsConfirm, true);
13 }
14 }
15 }
另外ClientScript注册RegisterClientScriptBlock的脚本是在执行完所有server端的代码之后才执行客户端脚本的,
并不是想象的那样循环一次执行一个confirm,可能会对某些业务逻辑有些限制.
当然解决方案还是有很多的,也可考虑用AJAX技术,学习中......