65.9K
CodeProject 正在变化。 阅读更多。
Home

服务器端消息框

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (31投票s)

2005 年 9 月 19 日

2分钟阅读

viewsIcon

243418

downloadIcon

3161

一个无需弹出窗口的 Web(模态)消息框。

Sample Image

Sample Image

引言

作为 Web 开发者,我们总是需要模态窗口。但 JavaScript 中最简单的模态窗口存在一个问题:“弹出窗口阻止器”。你可以在脚本中使用 window.showModalDialog(...),但接下来呢?

我的组件,一个 Web 消息框,是 ASP.NET 2.0 的可定制模态对话框,但它不是弹出窗口,也不是新窗口。它出现在所有对象之上并隐藏其他对象,因此无法点击其他控件。

背景

这个想法非常简单。在我的控件中,我有一些面板(<div>)和它们的 z-index。我打开一个透明的面板在所有元素之上。这样可以防止点击它们。然后,我需要几个新的面板来显示消息框,以及一些脚本和样式。

该控件有三个事件

  • Btn1Clicked
  • Btn2Clicked
  • Btn3Clicked

两种样式

  • msg.MessageBoxStyle.Violet
  • msg.MessageBoxStyle.Blue

和四个图标

  • msg.MessageBoxIcons.Exclamation
  • msg.MessageBoxIcons.Asteriks
  • msg.MessageBoxIcons.Question
  • msg.MessageBoxIcons.None

你可以通过编辑 Mbox.ascx.cs 中的 InitializeIconInitializeStyle 函数来编辑视觉效果。

使用控件

你唯一需要做的就是将 Mbox.ascx 用户控件和 Resources 文件夹添加到你的项目中。然后将用户控件拖放到你的 Web 表单中。

将以下代码添加到你的 default.aspx 或任何其他文件..

protected void Page_Load(object sender, EventArgs e)
{   //bind the events to messagebox buttons...
    Mbox1.Btn1Clicked += new EventHandler(Mbox1_Btn1Clicked);
    Mbox1.Btn2Clicked += new EventHandler(Mbox1_Btn2Clicked);
}
void Mbox1_Btn2Clicked(object sender, EventArgs e)
{
    Response.Write("2nd button clicked");
}

void Mbox1_Btn1Clicked(object sender, EventArgs e)
{
    Response.Write("1st button clicked");
}
//Show methods and their overloads...
protected void Button1_Click(object sender, EventArgs e)
{
    Mbox1.Show("hede", "hodo");
}
protected void Button2_Click(object sender, EventArgs e)
{
    Mbox1.Show("My test message", "Warning", "OK", "Cancel", null);
}
protected void Button3_Click(object sender, EventArgs e)
{
    Mbox1.Show("My test message", "Warning", "OK", "Cancel", null, 
                                 msg.MessageBoxIcons.Exclamation);
}
protected void Button4_Click(object sender, EventArgs e)
{
    Mbox1.Show("My test message", "Warning", "OK", "Cancel", "Retry", 
               msg.MessageBoxIcons.Exclamation,msg.MessageBoxStyle.Violet);
}

我不会再告诉你关于这个消息框的更多信息了。我已经告诉你它的逻辑。一点努力就足以理解代码了。

关注点

该产品的下一个版本将是一个类似窗口的弹出控件,但当然不是弹出窗口。它很快就会发布... 对于错误报告和建议,我将等待你的邮件至 ozuolmez@msn.com

© . All rights reserved.