您的位置:首页 > 编程语言 > ASP

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: