在ASP.NET的服务器端使用message box(Message box Server side in ASP.Net)

作者: R.Arockia Jayaseelan 译:涟漪勇

Message Box是用于显示消息的对话框,等待用户选择上面的不同按钮来执行不同的程序.完成了用户与程序的交互,在我们些windows程序是可以很容易的实现这个功能并且来自定义它,可是对于web程序就没有那么容易了,当然,可以JavaScript来实现这些功能.但是它不可以自己定义按钮的类型在对话框上.

用JavaScript你可以访问Message Box在页面载入和回送时,利用JavaScript你的程序没有办法得到用户的确认信息,为了实现的到用户选择的确认信息,你必须有服务器端的Message Box.

服务器端的Message Box提供用户选择不同的条件,这些条件是根据程序来的,例如确定或者取消, 得到用户的确认选择后我们来控制我们的程序执行不同的段落.

下面是运行是的效果图

下面是实现的代码
1. MessageBox.vb(Class)
Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.HtmlControls Imports System.IO Imports System.Drawing Imports System.Drawing.Imaging Imports System.Drawing.Drawing2D Public Class js Public Function js() End Function End Class Public Class MessageBox Inherits WebControl Private strLeft As String Private strTop As String Private intButton As Integer Private strMessage As String Private strTitle As String Private strImage As String Private strCss As String Private strCssTitle As String Private strCssMessage As String Private strButtonYes As String Private strButtonNo As String Private strButtonCancel As String Private strButtonWidth As Integer Private strMessageBoxIDYes As String Private strMessageBoxIDNo As String Private strMessageBoxIDCancel As String Private intMessageBoxWidth As Integer Private intMessageBoxHeight As Integer Private intMessageBoxImageWidth As Integer Private intMessageBoxImageHeight As Integer Dim homedirectory As String ' Message box left position Public Property MessageBoxLeft() As String Get Return strLeft End Get Set(ByVal Value As String) strLeft = Value End Set End Property ' Message box top position Public Property MessageBoxTop() As String Get Return strTop End Get Set(ByVal Value As String) strTop = Value End Set End Property ' Number of buttons you want to display in the message box Public Property MessageBoxButton() As Integer Get Return intButton End Get Set(ByVal Value As Integer) intButton = Value End Set End Property ' Customize message you want to display in the message box Public Property MessageBoxMessage() As String Get Return strMessage End Get Set(ByVal Value As String) strMessage = Value End Set End Property ' Title you want to display in the message box Public Property MessageBoxTitle() As String Get Return strTitle End Get Set(ByVal Value As String) strTitle = Value End Set End Property ' Image you want to display in the message box (like information / warning) Public Property MessageBoxImage() As String Get Return strImage End Get Set(ByVal Value As String) strImage = Value End Set End Property ' Message box ID for Yes button Public Property MessageBoxIDYes() As String Get Return strMessageBoxIDYes End Get Set(ByVal Value As String) strMessageBoxIDYes = Value End Set End Property ' Message box ID for No button Public Property MessageBoxIDNo() As String Get Return strMessageBoxIDNo End Get Set(ByVal Value As String) strMessageBoxIDNo = Value End Set End Property ' Message box ID for Cancel button Public Property MessageBoxIDCancel() As String Get Return strMessageBoxIDCancel End Get Set(ByVal Value As String) strMessageBoxIDCancel = Value End Set End Property ' Style you want to incorporate for message box Public Property MessageBoxCss() As String Get Return strCss End Get Set(ByVal Value As String) strCss = Value End Set End Property Public Property MessageBoxCssTitle() As String Get Return strCssTitle End Get Set(ByVal Value As String) strCssTitle = Value End Set End Property Public Property MessageBoxCssMessage() As String Get Return strCssMessage End Get Set(ByVal Value As String) strCssMessage = Value End Set End Property ' Message box Text for Yes button Public Property MessageBoxButtonYesText() As String Get Return strButtonYes End Get Set(ByVal Value As String) strButtonYes = Value End Set End Property ' Message box Text for No button Public Property MessageBoxButtonNoText() As String Get Return strButtonNo End Get Set(ByVal Value As String) strButtonNo = Value End Set End Property ' Message box Text for Cancel button Public Property MessageBoxButtonCancelText() As String Get Return strButtonCancel End Get Set(ByVal Value As String) strButtonCancel = Value End Set End Property ' Message box buttons width Public Property MessageBoxButtonWidth() As Integer Get Return strButtonWidth End Get Set(ByVal Value As Integer) strButtonWidth = Value End Set End Property ' Message box width Public Property MessageBoxWidth() As Integer Get Return intMessageBoxWidth End Get Set(ByVal Value As Integer) intMessageBoxWidth = Value End Set End Property ' Message box height Public Property MessageBoxHeight() As Integer Get Return intMessageBoxHeight End Get Set(ByVal Value As Integer) intMessageBoxHeight = Value End Set End Property ' Message box image width Public Property MessageBoxImageWidth() As Integer Get Return intMessageBoxImageWidth End Get Set(ByVal Value As Integer) intMessageBoxImageWidth = Value End Set End Property ' Message box image height Public Property MessageBoxImageHeight() As Integer Get Return intMessageBoxImageHeight End Get Set(ByVal Value As Integer) intMessageBoxImageHeight = Value End Set End Property Protected Friend layer As HtmlGenericControl Protected Friend ilayer As HtmlGenericControl Protected Friend img As HtmlGenericControl Protected Friend div As HtmlGenericControl Protected Friend ButtonOK As Button Protected Friend ButtonYes As Button Protected Friend ButtonNo As Button Protected Friend ButtonCancel As Button Protected Alertimage As System.Web.UI.WebControls.Image Public Sub New() MyBase.New("div") End Sub Protected Overrides Sub OnInit(ByVal e As EventArgs) MyBase.OnInit(e) ' Default properties settings for message box control If strLeft Is Nothing Then strLeft = "250" End If If strTop Is Nothing Then strTop = "250" End If If strTitle Is Nothing Then strTitle = "MessageBox" End If If intButton < 0 Then intButton = 1 End If If strMessageBoxIDYes Is Nothing Then strMessageBoxIDYes = "MessageBoxIDYes" End If If strMessageBoxIDNo Is Nothing Then strMessageBoxIDNo = "MessageBoxIDNo" End If If strMessageBoxIDCancel Is Nothing Then strMessageBoxIDCancel = "MessageBoxIDCancel" End If If strCss Is Nothing Then strCss = "" End If If strCssMessage Is Nothing Then strCssMessage = "" End If If strCssTitle Is Nothing Then strCssTitle = "" End If If strMessage Is Nothing Then strMessage = "No message to display here." End If If intButton = 1 Or intButton > 3 Or intButton < 1 Then If strButtonYes Is Nothing Then strButtonYes = "OK" End If ElseIf intButton > 1 And intButton < 4 Then If strButtonYes Is Nothing Then strButtonYes = "Approve" End If If strButtonNo Is Nothing Then strButtonNo = "Cancel" End If If strButtonCancel Is Nothing Then strButtonCancel = "Ignore" End If End If If strButtonWidth < 5 Then strButtonWidth = 70 End If If intMessageBoxWidth < 10 Then intMessageBoxWidth = 250 End If If intMessageBoxHeight < 1 Then intMessageBoxHeight = 8 End If If intMessageBoxImageWidth < 5 Then intMessageBoxImageWidth = 36 End If If intMessageBoxImageHeight < 5 Then intMessageBoxImageHeight = 36 End If If homedirectory Is Nothing Then homedirectory = Me.Page.Request.PhysicalApplicationPath End If End Sub Protected Overrides Sub createChildControls() ' Creating message box Dim myRow As TableRow Dim myCell As TableCell Dim myTable As Table = New Table myTable.BorderWidth = New Unit(0) myTable.CellSpacing = 0 myTable.Width = New Unit(intMessageBoxWidth) myTable.Height = New Unit(intMessageBoxHeight) Controls.Add(myTable) myRow = New TableRow myRow.BorderWidth = New Unit(0) myTable.Rows.Add(myRow) myCell = New TableCell Dim NewLabel As Label = New Label NewLabel.Text = strTitle NewLabel.CssClass = strCssTitle myCell.Controls.Add(NewLabel) myCell.ID = "dragbar" myCell.ColumnSpan = 5 myCell.CssClass = strCssTitle If strCssTitle = "" Then myCell.ForeColor = System.Drawing.Color.White myCell.BackColor = System.Drawing.Color.DarkBlue myCell.Font.Name = "Verdana" myCell.Font.Bold = True myCell.Font.Size = New FontUnit(8) myCell.Style.Add("CURSOR", "hand") End If myRow.Cells.Add(myCell) myRow = New TableRow myRow.BorderWidth = New Unit(0) myTable.Rows.Add(myRow) myCell = New TableCell myCell.ColumnSpan = 5 myCell.CssClass = strCssMessage If strCssMessage = "" Then myCell.BackColor = System.Drawing.Color.LightGray End If myRow.Cells.Add(myCell) Dim myRow1 As TableRow Dim myCell1 As TableCell Dim myTable1 As Table = New Table myTable1.BorderWidth = New Unit(0) myTable1.CellSpacing = 0 myCell.Controls.Add(myTable1) myRow1 = New TableRow myRow1.BorderWidth = New Unit(0) myTable1.Rows.Add(myRow1) myCell1 = New TableCell myCell1.CssClass = strCssMessage myCell1.BorderWidth = New Unit(0) myCell1.Width = New Unit(36) Dim Alertimage As System.Web.UI.WebControls.Image = New System.Web.UI.WebControls.Image Alertimage.Height = New Unit(intMessageBoxImageHeight) Alertimage.Width = New Unit(intMessageBoxImageWidth) Alertimage.BorderWidth = New Unit(0) Alertimage.ImageUrl = strImage myCell1.Controls.Add(Alertimage) myRow1.Cells.Add(myCell1) myCell1 = New TableCell myCell1.CssClass = strCssMessage myCell1.BorderWidth = New Unit(0) myCell1.CssClass = strCssMessage If strCssMessage = "" Then myCell1.HorizontalAlign = HorizontalAlign.Center myCell1.ForeColor = System.Drawing.Color.Black myCell1.BackColor = System.Drawing.Color.LightGray myCell1.BorderColor = System.Drawing.Color.LightGray myCell1.Font.Name = "Verdana" myCell1.Font.Bold = True myCell1.Font.Size = New FontUnit(8) End If Dim NewLabel1 As Label = New Label NewLabel1.Text = strMessage myCell1.Controls.Add(NewLabel1) myRow1.Cells.Add(myCell1) myRow = New TableRow myRow.BorderWidth = New Unit(0) myTable.Rows.Add(myRow) If intButton = 1 Or intButton > 3 Or intButton < 1 Then myCell = New TableCell myCell.ColumnSpan = 5 myCell.BorderWidth = New Unit(0) myCell.CssClass = strCssMessage myCell.HorizontalAlign = HorizontalAlign.Center If strCssMessage = "" Then myCell.ForeColor = System.Drawing.Color.Black myCell.BackColor = System.Drawing.Color.LightGray myCell.Font.Name = "Verdana" myCell.Font.Bold = True myCell.Font.Size = New FontUnit(8) End If ButtonOK = New Button ButtonOK.ID = strMessageBoxIDYes ButtonOK.Text = strButtonYes ButtonOK.Width = New Unit(strButtonWidth) ButtonOK.Style.Add("CURSOR", "hand") myCell.Controls.Add(ButtonOK) myRow.Cells.Add(myCell) End If If intButton > 1 And intButton < 4 Then myCell = New TableCell myCell.CssClass = strCssMessage myCell.BorderWidth = New Unit(0) myCell.HorizontalAlign = HorizontalAlign.Right If strCssMessage = "" Then myCell.ForeColor = System.Drawing.Color.Black myCell.BackColor = System.Drawing.Color.LightGray myCell.Font.Name = "Verdana" myCell.Font.Bold = True myCell.Font.Size = New FontUnit(8) End If ButtonYes = New Button ButtonYes.ID = strMessageBoxIDYes ButtonYes.Text = strButtonYes ButtonYes.Width = New Unit(strButtonWidth) ButtonYes.Style.Add("CURSOR", "hand") myCell.Controls.Add(ButtonYes) myRow.Cells.Add(myCell) myCell = New TableCell myCell.Width = New Unit(20) myCell.BorderWidth = New Unit(0) myCell.CssClass = strCssMessage If strCssMessage = "" Then myCell.BackColor = System.Drawing.Color.LightGray End If myRow.Cells.Add(myCell) myCell = New TableCell myCell.CssClass = strCssMessage myCell.BorderWidth = New Unit(0) If strCssMessage = "" Then myCell.ForeColor = System.Drawing.Color.Black myCell.BackColor = System.Drawing.Color.LightGray myCell.Font.Name = "Verdana" myCell.Font.Bold = True myCell.Font.Size = New FontUnit(8) End If If intButton = 2 Then myCell.HorizontalAlign = HorizontalAlign.Left ElseIf intButton = 3 Then myCell.HorizontalAlign = HorizontalAlign.Center End If ButtonNo = New Button ButtonNo.ID = strMessageBoxIDNo ButtonNo.Text = strButtonNo ButtonNo.Width = New Unit(strButtonWidth) ButtonNo.Attributes("WIDTH") = strButtonWidth.ToString() ButtonNo.Attributes("HEIGHT") = strButtonWidth.ToString() ButtonNo.Style.Add("CURSOR", "hand") myCell.Controls.Add(ButtonNo) myRow.Cells.Add(myCell) If intButton = 3 Then myCell = New TableCell myCell.Width = New Unit(10) myCell.BorderWidth = New Unit(0) myCell.CssClass = strCssMessage If strCssMessage = "" Then myCell.BackColor = System.Drawing.Color.LightGray End If myRow.Cells.Add(myCell) myCell = New TableCell myCell.CssClass = strCssMessage myCell.BorderWidth = New Unit(0) myCell.HorizontalAlign = HorizontalAlign.Left If strCssMessage = "" Then myCell.ForeColor = System.Drawing.Color.Black myCell.BackColor = System.Drawing.Color.LightGray myCell.Font.Name = "Verdana" myCell.Font.Bold = True myCell.Font.Size = New FontUnit(8) End If ButtonCancel = New Button ButtonCancel.ID = strMessageBoxIDCancel ButtonCancel.Text = strButtonCancel ButtonCancel.Width = New Unit(strButtonWidth) ButtonCancel.Style.Add("CURSOR", "hand") myCell.Controls.Add(ButtonCancel) myRow.Cells.Add(myCell) End If End If End Sub Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter) ' Rendering message box control to the browser MyBase.AddAttributesToRender(writer) writer.AddAttribute(HtmlTextWriterAttribute.Id, "showimage") ' writer.AddAttribute(HtmlTextWriterAttribute.Style, "Z-INDEX: 9999; LEFT:" + strLeft + "px; WIDTH:" + strLeft + "px; POSITION: absolute; TOP: " + strTop + "px; filter:progid:DXImageTransform.Microsoft.Shadow(color='dimgray', direction="135," strength="3;"") writer.AddAttribute(HtmlTextWriterAttribute.Style, "Z-INDEX: 9999; LEFT:" + strLeft + "px; WIDTH:" + strLeft + "px; POSITION: absolute; TOP: " + strTop + "px; filter:progid:DXImageTransform.Microsoft.Shadow(color=""dimgray"", direction=""135,"" strength=""3"");") End Sub End Class
2. MessageBox.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MessageBox.aspx.vb" Inherits="WebApplication13.MessageBox1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>MessageBox</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder> <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 216px; POSITION: absolute; TOP: 8px" runat="server" Text="Click"></asp:Button> </form> </body> </HTML>

3. MessageBox.aspx.vb
Public Class MessageBox1 Inherits System.Web.UI.Page Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder Protected WithEvents Button1 As System.Web.UI.WebControls.Button Dim newBox As MessageBox = New MessageBox #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here CheckYesNo() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click newBox.MessageBoxButton = 3 newBox.MessageBoxTop = 50 newBox.MessageBoxLeft = 150 newBox.MessageBoxWidth = 300 newBox.MessageBoxHeight = 150 newBox.MessageBoxButtonWidth = 50 newBox.MessageBoxIDYes = "yesno" newBox.MessageBoxIDNo = "yesno" newBox.MessageBoxIDCancel = "yesno" newBox.MessageBoxButtonYesText = "Yes" newBox.MessageBoxButtonNoText = "No" newBox.MessageBoxButtonCancelText = "Cancel" newBox.MessageBoxTitle = "Dynamic message box." newBox.MessageBoxMessage = "Do you want to continue?" newBox.MessageBoxImage = "information.gif" PlaceHolder1.Controls.Add(newBox) End Sub Private Sub CheckYesNo() If Request.Form("yesno") = "Yes" Then Response.Write("Button - Yes - Selected") ElseIf Request.Form("yesno") = "No" Then Response.Write("Button - No - Selected") ElseIf Request.Form("yesno") = "Cancel" Then Response.Write("Button - Cancel - Selected") End If End Sub End Class

转载于:https://www.cnblogs.com/rippleyong/archive/2004/11/06/61011.html

在ASP.NET的服务器端使用message box(Message box Server side in ASP.Net)相关推荐

  1. asp服务器端作用,ASP中的服务器端脚本代码由()解释执行。 - 问答库

    问题: [单选] ASP中的服务器端脚本代码由()解释执行. A . WEB服务器 B . 应用程序服务器 C . 浏览器 D . 专用的浏览器插件程序 Cookie的应用是通过()对象和()对象来实 ...

  2. ASP.NET删除服务器端文件,asp.net 删除服务器上的文件系统

    用ASP.NET MVC自己管理自己的View:ASP.NET MVC File ... 于是我之前做了ASP.NET MVC View Management,用于管理服务器上ASP.NET MVC中 ...

  3. power bi报表服务器_如何将Power BI Report Server报表嵌入ASP.Net Web应用程序

    power bi报表服务器 Every once in a while, teams from different functional areas of the business (i.e. bus ...

  4. asp access的安全:不要认为简单的改后缀mdb为asp就能防下载

    asp access的安全:不要认为简单的改后缀mdb为asp就能防下载 昨天和animator试验了一下,把data.mdb文件改名为data.asp文件后放在wwwroot目录里.然后在IE中输入 ...

  5. 技术贴:asp.net实现唯一账户在线 禁止同一帐号同时在线 asp.net实现您的帐号在别处登录,您已被迫下线!...

    技术贴:asp.net实现唯一账户在线 禁止同一帐号同时在线 asp.net实现您的帐号在别处登录,您已被迫下线! 原文 http://www.cnblogs.com/cdemo/p/3386534. ...

  6. A CAPTCHA Server Control for ASP.NET C# 版

    今晚看到了A CAPTCHA Server Control for ASP.NET , 一个非常棒的CAPTCHA控件, 因为它原来是VB写的, 我照着用C#抄了一遍.... 原版下载地址: Down ...

  7. Server 对象 错误 'ASP 0177 : 800401f3' Server.CreateObject 失败

    今天早上客户网站出现"Server 对象 错误 'ASP 0177 : 800401f3' Server.CreateObject 失败  "问题,最后找到一篇博客有比较详细的解决 ...

  8. js中box和box()的区别

    window.onload = function(){ var input = document.getElementByTagName('input')[0]; input.onclick = bo ...

  9. SQL Server LocalDB 在 ASP.NET中的应用

    SQL Server LocalDB 在 ASP.NET中的应用. 使用SQL Server LocalDB的优势: 快速部署完整的SQL Server.以后项目可以无缝升级到高级版本. 它是真正的S ...

  10. Android Handler Message 里面的message.what, message.arg1,message.obj,obtainMessage, message.setData的使用

    Android Handler Message 里面的message.what, message.arg1,message.arg2,message.obj,obtainMessage, messag ...

最新文章

  1. 将二维数组保存为.csv格式
  2. ApacheCN 数据科学/人工智能/机器学习知识树 2019.2
  3. php 清除缓存的操作,PHP清除缓存的几种方法
  4. 神经网络的迭代次数和收敛误差与谐振子的位移和时间
  5. 编译gcc4.4.6与ICE遇到的几个问题
  6. mysql exists in join_子查询、left join、 exists 替代 not in
  7. java plug机制_【maven实战】20-插件解析机制
  8. 博途v15安装过程中提示出错_博图V15为什么安装失败?
  9. 漫步数学分析六——聚点
  10. Android中关于键盘的处理
  11. 基于C# 和Access数据库的电影院管理系统
  12. scratch 极简坦克大战
  13. 贝叶斯网络:故障诊断方法研究
  14. 手机整人脚本html,教大家用vbs代码制作恶搞整人
  15. 图像与视频处理中的优化方法
  16. end 键功能的定义
  17. Linux线程操作以及相关知识
  18. 内存管理(一)MRC
  19. 我们最喜欢的技术:How-To Geek值得感谢的
  20. BSN公网发展如何影响区块链的技术演进?

热门文章

  1. 5g组网sa方式的演进_关于5G的SA(独立组网)和NSA(非独立组网),这篇通俗易懂!
  2. 【渝粤教育】国家开放大学2018年秋季 0631-21T动物常见病防治 参考试题
  3. 【Python实例第5讲】Pipeline与GridSearchCV降维法
  4. linux 统计 程序运行时间
  5. Make.am中生成.la动态库的同时要链接.a静态库的问题
  6. python学习day35 并发编程 操作系统 进程概念
  7. Linux 安装Zookeeper
  8. C#-WebForm-文件上传-FileUpload控件
  9. JAVA学习之 Model2中的Servlet与.NET一般处理程序傻傻分不清楚
  10. 使用Spring的Property文件存储测试数据 - 初始化