附上测试代码:
测试代码
1
public
partial
class 后台注册脚本 : System.Web.UI.Page
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5
6 }
7 #region ShowMsg
8 protected void ShowMsg( string msg)
9 {
10 Response.Write( " <script>alert(' " + msg + " ')</script> ");
11 }
12
13 protected void ShowMsg2( string msg)
14 {
15 ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), " alert ", " alert(' " + msg + " ') ", true);
16 }
17
18 protected void ShowMsg3( string msg)
19 {
20 Page.RegisterClientScriptBlock( " alert ", " <script>alert(' " + msg + " ')</script> ");
21 }
22
23 protected void ShowMsg4( string msg)
24 {
25 Page.RegisterStartupScript( " alert ", " <script>alert(' " + msg + " ')</script> ");
26 }
27
28 protected void ShowMsg5( string msg)
29 {
30 ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), " show ", " alert(' " + msg + " ') ", true);
31 }
32
33 #endregion
34 protected void Button1_Click1( object sender, EventArgs e)
35 {
36 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
37 {
38 ShowMsg( " Response.Write--不能为空 ");
39 }
40 }
41
42 protected void Button2_Click( object sender, EventArgs e)
43 {
44 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
45 {
46 ShowMsg2( " ScriptManager.RegisterClientScriptBlock(Page——不能为空 ");
47 }
48 }
49
50 protected void Button3_Click( object sender, EventArgs e)
51 {
52 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
53 {
54 ShowMsg3( " Page.RegisterClientScriptBlock——不能为空 ");
55 }
56 }
57
58 protected void Button4_Click( object sender, EventArgs e)
59 {
60 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
61 {
62 ShowMsg4( " Page.RegisterStartupScript——不能为空 ");
63 }
64 }
65
66 protected void Button5_Click( object sender, EventArgs e)
67 {
68 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
69 {
70 ShowMsg5( " ScriptManager.RegisterClientScriptBlock(UpdatePanel——不能为空 ");
71 }
72 }
73 }
2 {
3 protected void Page_Load( object sender, EventArgs e)
4 {
5
6 }
7 #region ShowMsg
8 protected void ShowMsg( string msg)
9 {
10 Response.Write( " <script>alert(' " + msg + " ')</script> ");
11 }
12
13 protected void ShowMsg2( string msg)
14 {
15 ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), " alert ", " alert(' " + msg + " ') ", true);
16 }
17
18 protected void ShowMsg3( string msg)
19 {
20 Page.RegisterClientScriptBlock( " alert ", " <script>alert(' " + msg + " ')</script> ");
21 }
22
23 protected void ShowMsg4( string msg)
24 {
25 Page.RegisterStartupScript( " alert ", " <script>alert(' " + msg + " ')</script> ");
26 }
27
28 protected void ShowMsg5( string msg)
29 {
30 ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), " show ", " alert(' " + msg + " ') ", true);
31 }
32
33 #endregion
34 protected void Button1_Click1( object sender, EventArgs e)
35 {
36 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
37 {
38 ShowMsg( " Response.Write--不能为空 ");
39 }
40 }
41
42 protected void Button2_Click( object sender, EventArgs e)
43 {
44 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
45 {
46 ShowMsg2( " ScriptManager.RegisterClientScriptBlock(Page——不能为空 ");
47 }
48 }
49
50 protected void Button3_Click( object sender, EventArgs e)
51 {
52 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
53 {
54 ShowMsg3( " Page.RegisterClientScriptBlock——不能为空 ");
55 }
56 }
57
58 protected void Button4_Click( object sender, EventArgs e)
59 {
60 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
61 {
62 ShowMsg4( " Page.RegisterStartupScript——不能为空 ");
63 }
64 }
65
66 protected void Button5_Click( object sender, EventArgs e)
67 {
68 if ( string.IsNullOrEmpty(TextBox1.Text.Trim()))
69 {
70 ShowMsg5( " ScriptManager.RegisterClientScriptBlock(UpdatePanel——不能为空 ");
71 }
72 }
73 }
测试结果:
页面没有UpdatePanel-----都有效果
都放在UpdataPanel-----只有 ScriptManager.RegisterClientScriptBlock有效果
都不放在UpdatePanel----都有效果
都放在UpdataPanel-----只有 ScriptManager.RegisterClientScriptBlock有效果
都不放在UpdatePanel----都有效果
结论:
1>.如果页面没有UpdatePanel或者注册的控件不在UpdatePanel中,以上方法都可以
2>.如果注册UpdatePanel控件中的脚本,则只有 ScriptManager.RegisterClientScriptBlock事件有效果,两种传参方式都可行。
2>.如果注册UpdatePanel控件中的脚本,则只有 ScriptManager.RegisterClientScriptBlock事件有效果,两种传参方式都可行。