ASP.net注册客户端JS脚本,利用两次提交解决下载后画面不能刷新的问题
2009-08-21 17:01
876 查看
以下为代码
1 using System;
2 using System.Text;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Data;
6 using System.IO;
7 using System.Drawing;
8 using System.Web;
9 using System.Web.SessionState;
10 using System.Web.UI;
11 using System.Web.UI.WebControls;
12 using System.Web.UI.HtmlControls;
13
14 namespace wuliang
15 {
16 public class WebForm1 : System.Web.UI.Page
17 {
18 protected System.Web.UI.WebControls.TextBox TextBox1;
19 protected System.Web.UI.WebControls.TextBox TextBox2;
20 protected System.Web.UI.WebControls.Button Button1;
21 protected System.Web.UI.WebControls.Button Button2;
22
23 private void Page_Load(object sender, System.EventArgs e)
24 {
25 }
26
27 #region Web
28 override protected void OnInit(EventArgs e)
29 {
30 InitializeComponent();
31 base.OnInit(e);
32 }
33
34 private void InitializeComponent()
35 {
36 this.Button1.Click += new System.EventHandler(this.Button1_Click);
37 this.Button2.Click += new System.EventHandler(this.Button2_Click);
38 this.Load += new System.EventHandler(this.Page_Load);
39
40 }
41 #endregion
42
43 // let the button1 do the check ,
44 // if ng, make the textbox bdcolor red,
45 // if ok, make the textbox bdcolor white, and call the doDownload method.
46 private void Button1_Click(object sender, System.EventArgs e)
47 {
48 if (string.Compare(this.TextBox1.Text,this.TextBox2.Text)>0)
49 {
50 this.TextBox1.BackColor=System.Drawing.Color.Red;
51 this.TextBox2.BackColor=System.Drawing.Color.Red;
52 }
53 else
54 {
55 this.TextBox1.BackColor=System.Drawing.Color.White;
56 this.TextBox2.BackColor=System.Drawing.Color.White;
57 doDownload();
58
59 // You can try the method what you have done yestoday to see the result again
60 // Comment out the doDownload() before and run the source down↓
61
62 // String test="aaaaaaaaaaaaaaaaaaaaaaaa";
63 // Response.Clear();
64 // Response.ClearHeaders();
65 // Response.Buffer=false;
66 // Response.ContentType="application/octet-stream";
67 // Response.AppendHeader("Content-Disposition","attachment;filename=test");
68 // Response.Write(test.ToCharArray(0,test.Length),0,test.Length);
69 // Response.Flush();
70 // Response.End();
71 }
72
73 }
74
75 // let the button2 to do the download
76 private void Button2_Click(object sender, System.EventArgs e)
77 {
78 String test="aaaaaaaaaaaaaaaaaaaaaaaa";
79 Response.Clear();
80 Response.ClearHeaders();
81 Response.Buffer=false;
82 Response.ContentType="application/octet-stream";
83 Response.AppendHeader("Content-Disposition","attachment;filename=test");
84 Response.Write(test.ToCharArray(0,test.Length),0,test.Length);
85 Response.Flush();
86 Response.End();
87 }
88
89 // RegisterStartupScript a client StartupScript, let the client javascript to call the Button2's click method
90 private void doDownload()
91 {
92 StringBuilder builder = new StringBuilder();
93 String newLine = Environment.NewLine;
94 builder.Append("<script language=\"javascript\">");
95 builder.Append(newLine);
96 builder.Append("document.all.Button2.click(); ");
97 builder.Append(newLine);
98 builder.Append("</script>");
99 this.Page.RegisterStartupScript("downld",builder.ToString(0,builder.Length));
}
}
}
/WebForm1.aspx.zip
1 using System;
2 using System.Text;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Data;
6 using System.IO;
7 using System.Drawing;
8 using System.Web;
9 using System.Web.SessionState;
10 using System.Web.UI;
11 using System.Web.UI.WebControls;
12 using System.Web.UI.HtmlControls;
13
14 namespace wuliang
15 {
16 public class WebForm1 : System.Web.UI.Page
17 {
18 protected System.Web.UI.WebControls.TextBox TextBox1;
19 protected System.Web.UI.WebControls.TextBox TextBox2;
20 protected System.Web.UI.WebControls.Button Button1;
21 protected System.Web.UI.WebControls.Button Button2;
22
23 private void Page_Load(object sender, System.EventArgs e)
24 {
25 }
26
27 #region Web
28 override protected void OnInit(EventArgs e)
29 {
30 InitializeComponent();
31 base.OnInit(e);
32 }
33
34 private void InitializeComponent()
35 {
36 this.Button1.Click += new System.EventHandler(this.Button1_Click);
37 this.Button2.Click += new System.EventHandler(this.Button2_Click);
38 this.Load += new System.EventHandler(this.Page_Load);
39
40 }
41 #endregion
42
43 // let the button1 do the check ,
44 // if ng, make the textbox bdcolor red,
45 // if ok, make the textbox bdcolor white, and call the doDownload method.
46 private void Button1_Click(object sender, System.EventArgs e)
47 {
48 if (string.Compare(this.TextBox1.Text,this.TextBox2.Text)>0)
49 {
50 this.TextBox1.BackColor=System.Drawing.Color.Red;
51 this.TextBox2.BackColor=System.Drawing.Color.Red;
52 }
53 else
54 {
55 this.TextBox1.BackColor=System.Drawing.Color.White;
56 this.TextBox2.BackColor=System.Drawing.Color.White;
57 doDownload();
58
59 // You can try the method what you have done yestoday to see the result again
60 // Comment out the doDownload() before and run the source down↓
61
62 // String test="aaaaaaaaaaaaaaaaaaaaaaaa";
63 // Response.Clear();
64 // Response.ClearHeaders();
65 // Response.Buffer=false;
66 // Response.ContentType="application/octet-stream";
67 // Response.AppendHeader("Content-Disposition","attachment;filename=test");
68 // Response.Write(test.ToCharArray(0,test.Length),0,test.Length);
69 // Response.Flush();
70 // Response.End();
71 }
72
73 }
74
75 // let the button2 to do the download
76 private void Button2_Click(object sender, System.EventArgs e)
77 {
78 String test="aaaaaaaaaaaaaaaaaaaaaaaa";
79 Response.Clear();
80 Response.ClearHeaders();
81 Response.Buffer=false;
82 Response.ContentType="application/octet-stream";
83 Response.AppendHeader("Content-Disposition","attachment;filename=test");
84 Response.Write(test.ToCharArray(0,test.Length),0,test.Length);
85 Response.Flush();
86 Response.End();
87 }
88
89 // RegisterStartupScript a client StartupScript, let the client javascript to call the Button2's click method
90 private void doDownload()
91 {
92 StringBuilder builder = new StringBuilder();
93 String newLine = Environment.NewLine;
94 builder.Append("<script language=\"javascript\">");
95 builder.Append(newLine);
96 builder.Append("document.all.Button2.click(); ");
97 builder.Append(newLine);
98 builder.Append("</script>");
99 this.Page.RegisterStartupScript("downld",builder.ToString(0,builder.Length));
}
}
}
/WebForm1.aspx.zip
相关文章推荐
- ASP.NET中引用JS不能调用JQuery问题 解决
- 解决UpdatePanel无刷新,注册客户端脚本不执行问题
- 用脚本解决ASP.NET页面刷新问题
- [原]Asp.Net提交后,刷新时事件触发问题解决
- [原]ASP.NET中使用JQUERY-EASYUI后,解决ClientScript.RegisterStartupScript 所注册脚本执行两次
- 绝对酷,如何解决asp.net中javascript脚本的问题(使用服务器控件执行客户端脚本)
- 绝对酷,如何解决asp.net中javascript脚本的问题(使用服务器控件执行客户端脚本)
- 用脚本解决ASP.NET页面刷新问题
- 解决UpdatePanel无刷新,注册客户端脚本不执行问题
- 用脚本解决ASP.NET页面刷新问题
- ASP.NET自定义控件的BUG在ASPX页面中注册控件后不能打出标签的问题(本人已解决)
- asp.net mvc利用Json验证数据和导向页面,解决重复提交问题
- asp.net 后台向前台注册JS脚本
- 利用ASP.NET和AJAX解决手工拼接HTML问题
- Asp.net生成l文件并下载-----解决使用迅雷下载页面而不是文件的问题
- 解决asp.net表单提交按钮当设定OnClientClick事件后不再进行表单验证的问题
- VS2005导致2003的asp.net不能调试的问题解决了
- Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器
- [Asp.net]解决禁用JS后LinkButton无效问题
- asp.net 解决重复提交问题