Alert Message in Code Behind File
2008-03-14 12:18 liuqhui 阅读(370) 评论(0) 编辑 收藏 举报
我们平时在开发过程中,需要在后台代码中控制对话框的弹出
比如说,数据保存成功后要提示用户保存成功.
我们一般的做法是
Response.Write("<script>alert('Save successfully.')</script>");
但是如果页面套在MasterPage的一个iframe的话,在后台代码调用上面的语句,
页面的样式就变了.我们可以通过下面的办法来解决
我们在调用的时候只要新建一个Alert的实例,然后调用ShowMessage方法就可以
Alert alert = new Alert();
alert.Show("Save successfully.");
比如说,数据保存成功后要提示用户保存成功.
我们一般的做法是
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![](/Images/OutliningIndicators/None.gif)
12
/// <summary>
13
/// A Javascript Alert
14
/// </summary>
15
public class Alert
16
{
17
public Alert()
18
{
19
20
}
21![](/Images/OutliningIndicators/InBlock.gif)
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![](/Images/OutliningIndicators/InBlock.gif)
33
public static void ShowMessage()
34
{
35
throw new Exception("The method or operation is not implemented.");
36
}
37
}
38![](/Images/OutliningIndicators/None.gif)
39![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
我们在调用的时候只要新建一个Alert的实例,然后调用ShowMessage方法就可以
Alert alert = new Alert();
alert.Show("Save successfully.");