在ASP.NET开发中,服务器控件是构建动态网页的重要工具。尽管服务器控件提供了丰富的功能,但在某些情况下,如需要实现消息框(Message Box)或确认框(Confirmation Box)时,现有的控件可能不够用。为了解决这个问题,开发者Ning Liao和Liang Yang开发了一种优秀的服务器控件,可以在CodeProject上找到。然而,这可能还不够,因为在这个模型中,需要以编程方式检查所有内容。因此,开发了这种事件驱动的服务器控件,它在客户端确认框的按钮点击时触发一个名为GetMessageBoxRespose的事件。在这个事件中,可以捕获哪个按钮被按下。当用户按下按钮时,页面会再次提交,状态会丢失,处理的任何数据都需要重新获取。因此,提供了一种在确认时传递数据的方法,可以在触发的事件中将其取回。这很酷,不是吗?
要在代码中使用这个服务器控件,需要将它作为.NET Framework组件添加到Web应用程序中。右键点击工具箱中的“组件”标签,点击“添加/移除项”,点击“浏览”,然后选择并添加计算机上存储的服务器控件MessageBox.dll。然后,将服务器控件拖放到Web表单上。以下是包含服务器控件的Default.aspx代码:
<%@ Register Assembly="MessageBox" Namespace="Utilities" TagPrefix="cc2" %>
<cc2:MessageBox ID="msgBox" runat="server" Style="z-index: 101; left: 0px; position: absolute; top: 0px" Width="112px" OnGetMessageBoxResponse="msgBox_GetMessageBoxResponse" />
要显示确认框,只需调用msgBox对象的Confirm方法,如下所示:
C# private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
// 在客户端显示确认框
msgBox.Confirm("This control is cool isn't it?", "Test Data");
// msgBox.Confirm("This control is cool isn't it?");
}
else
{
msgBox.Alert("Page posback happened.");
}
}
当用户通过点击“确定”或“取消”按钮响应确认框时,网页将被回发。回发后,MessageBox控件将触发GetMessageBoxResponse事件,因此需要通过注册其事件处理程序来捕获这个事件。在这个事件中,将得到用户按下的按钮以及通过Confirm方法传递的其他数据(如果有的话):
C# private void msgBox_GetMessageBoxResponse(object sender, Utilities.MessageBox.MessageBoxEventHandler e)
{
if (e.ButtonPressed == Utilities.MessageBox.MessageBoxEventHandler.Button.Ok)
{
lblMsg.Text = "You said ok. Thanks!!!";
}
else if (e.ButtonPressed == Utilities.MessageBox.MessageBoxEventHandler.Button.Cancel)
{
lblMsg.Text = "You said cancel. :(";
}
if (e.Data != null)
{
Response.Write(e.Data);
}
}
C# namespace Utilities
{
/// <summary>
/// Summary description for MessageBox.
/// </summary>
[ToolboxData("<{0}:MessageBox runat=\\"server\\"></{0}:MessageBox>")]
[System.Serializable(), DesignTimeVisible()]
public class MessageBox : System.Web.UI.WebControls.WebControl
{
// Event Arguments from MessageBox response
public class MessageBoxEventHandler : System.EventArgs
{
public enum Button
{
Ok, Cancel, NotPressed
}
public readonly MessageBoxEventHandler.Button ButtonPressed;
public readonly object Data;
public MessageBoxEventHandler(MessageBoxEventHandler.Button buttonPressed)
: this(buttonPressed, null)
{
}
public MessageBoxEventHandler(MessageBoxEventHandler.Button buttonPressed, object data)
{
this.ButtonPressed = buttonPressed;
this.Data = data;
}
}
public delegate void Message(object sender, MessageBoxEventHandler e);
// Declare event to pass message box response
public event Message GetMessageBoxResponse;
// private string msg;
private string content;
private const string ok = "ok";
private const string cancel = "cancel";
private const string notPressed = "notPressed";
public void Alert(string message)
{
string msg = message.Replace("\n", "\\n");
msg = message.Replace("\"", "'");
StringBuilder sb = new StringBuilder(50);
sb.Append("\n");
content = sb.ToString();
}
public void Confirm(string message)
{
this.Confirm(message, null);
}
/// <summary>
/// Displays confirmation box on client side
/// </summary>
/// <param name="message">Message to display on confirmation box</param>
/// <param name="data">Data to pass to GetMessageBoxResponse event.</param>
public void Confirm(string message, object data)
{
string msg = message.Replace("\n", "\\n");
msg = message.Replace("\"", "'");
StringBuilder sb = new StringBuilder(100);
sb.AppendFormat("", notPressed, HiddenFieldName);
sb.Append("\n");
content = sb.ToString();
if (data != null)
{
this.Page.Session[m_Session + this.ClientID] = data;
}
}
private string HiddenFieldName
{
get
{
return "hidF" + this.ID;
}
}
protected override void OnLoad(EventArgs e)
{
object data = null;
switch (this.Page.Request.Form[HiddenFieldName])
{
case ok:
this.Page.Request.Form[HiddenFieldName].Replace(ok, notPressed);
if (this.Page.Session[m_Session + this.ClientID] != null)
{
data = this.Page.Session[m_Session + this.ClientID];
this.Page.Session.Remove(m_Session + this.ClientID);
}
OnMessageBoxShow(new MessageBoxEventHandler(MessageBoxEventHandler.Button.Ok, data));
break;
case cancel:
this.Page.Request.Form[HiddenFieldName].Replace(cancel, notPressed);
if (this.Page.Session[m_Session + this.ClientID] != null)
{
data = this.Page.Session[m_Session + this.ClientID];
this.Page.Session.Remove(m_Session + this.ClientID);
}
OnMessageBoxShow(new MessageBoxEventHandler(MessageBoxEventHandler.Button.Cancel, data));
break;
case notPressed:
break;
default:
break;
}
base.OnLoad(e);
}
/// <summary>
/// Invokes registered event handlers with MessageBox class's object.
/// </summary>
/// <param name="e">Pass MessageBox event arguments to registered event handlers</param>
protected virtual void OnMessageBoxShow(MessageBoxEventHandler e)
{
if (GetMessageBoxResponse != null)
{
GetMessageBoxResponse(this, e);
}
}
protected override void Render(HtmlTextWriter output)
{
if (!this.DesignMode)
{
output.Write(this.content);
}
else
{
System.Web.UI.WebControls.Label lbl = new Label();
lbl.Font.Bold = this.Font.Bold;
lbl.Font.Italic = this.Font.Italic;
lbl.Font.Names = this.Font.Names;
lbl.Font.Overline = this.Font.Overline;
lbl.Font.Size = this.Font.Size;
lbl.Font.Strikeout = this.Font.Strikeout;
lbl.Font.Underline = this.Font.Underline;
lbl.ForeColor = this.ForeColor;
lbl.BackColor = this.BackColor;
lbl.BorderColor = this.BorderColor;
lbl.BorderStyle = this.BorderStyle;
lbl.BorderWidth = this.BorderWidth;
lbl.Text = this.ID;
lbl.RenderControl(output);
}
}
}
}