比如说,数据保存成功后要提示用户保存成功.
我们一般的做法是
Response.Write("<script>alert('Save successfully.')</script>");
但是如果页面套在MasterPage的一个iframe的话,在后台代码调用上面的语句,
页面的样式就变了.我们可以通过下面的办法来解决
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Text;
11
12
/// <summary>
13
/// A Javascript Alert
14
/// </summary>
15
public class Alert
16
{
17
public Alert()
18
{
19
20
}
21
22
public static void Show(string message)
23
{
24
string strMessage = message.Replace("'", "\\'");
25
string script = "<script type=\"text/javascript\">alert('" + strMessage + "');</script>";
26
Page page = HttpContext.Current.CurrentHandler as Page;
27
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
28
{
29
page.ClientScript.RegisterClientScriptBlock(typeof(Alert),"alert",script);
30
}
31
}
32
33
public static void ShowMessage()
34
{
35
throw new Exception("The method or operation is not implemented.");
36
}
37
}
38
39

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

我们在调用的时候只要新建一个Alert的实例,然后调用ShowMessage方法就可以
Alert alert = new Alert();
alert.Show("Save successfully.");