Selfmade MessageBox.Show() in ASP.NET
I want to rebuild the "look & feel" of MessageBox.Show() of WinForms applications.
With "look & feel" I mean to JUST CALL the MessageBox.Show() command with the logic of my already implemented ShowMessage method.
My current codebehind:
public partial class Testpage : System.Web.UI.Page
{
public enum MessageBoxIcon { Information, Error, Warning }
protected void Page_Load(object sender, EventArgs e)
{
ShowMessage("Testmessage", MessageBoxIcon.Error, ex);
}
private void ShowMessage(string title, string message, MessageBoxIcon type, Exception ex)
{
if (type == MessageBoxIcon.Information)
{
pnlMsgBox.CssClass = "MessageBoxInformation";
imgMsgBox.ImageUrl = "Resources/img/icon/MessageBoxIcon.Information.png";
}
else if (type == MessageBoxIcon.Warning)
{
pnlMsgBox.CssClass = "MessageBoxWarning";
im开发者_StackOverflow中文版gMsgBox.ImageUrl = "Resources/img/icon/MessageBoxIcon.Warning.png";
}
else if (type == MessageBoxIcon.Error)
{
pnlMsgBox.CssClass = "MessageBoxError";
imgMsgBox.ImageUrl = "Resources/img/icon/MessageBoxIcon.Error.png";
}
else
throw new NotImplementedException();
lblMsgBox.Text = "<span style=\"font-size: large;\">" + title + "</span><br /><br />" + message;
#if (DEBUG)
if (ex != null)
lblMsgBox.Text += "<br /><br /><b>Debug information:</b> " + ex.Message;
#endif
pnlMsgBox.Visible = true;
updPnl.Update();
}
}
I'd like to call something like this:
protected void Page_Load(object sender, EventArgs e)
{
MessageBox.Show("Testmessage", MessageBoxIcon.Error, ex);
}
But
public static class MessageBox : Testpage
{
public static void Show(string message)
{
// do stuff with the page controls.
}
}
doesn't work as you all know, because Testpage isn't static.
Does anybody have some suggestions for me?
Reading the comments to the previous answer, I think next:
Its difficult to get access to current page context from the separated static class. So then, i recomendate do next:
Define next interface:
public interface IMessageBoxContainer
{
UpdatePanel UpdatePanel { get; }
Label Label { get; }
}
and implement him by your page as:
public partial class _Default : System.Web.UI.Page, IMessageBoxContainer
{
protected void Page_Load(object sender, EventArgs e)
{
}
public UpdatePanel UpdatePanel
{
get { return updatePanel1; }
}
public Label Label
{
get { return label1; }
}
}
This steps provide you to get access to your page from anywhere. After that implement you MessageBox NON-static class:
public class MessageBox
{
private IMessageBoxContainer container = null;
public MessageBox(IMessageBoxContainer _container)
{
container = _container;
}
public void Show(string message)
{
container.Label.Text = message;
container.UpdatePanel.Update();
}
}
Why non-static? Becouse if you do container field as static and set him value anywhere in page (for example in Page_Load), when another page will load - your container property will reset. But without this field, that pointer to your current page, you cant get access to your specefied controls.
After that you can use your class like that:
protected void Button1_Click(object sender, EventArgs e)
{
var box = new MessageBox(this);
box.ShowMessage("hello world");
}
So, you get separated implementation logic of your MessageBox and simple using him, yes? But unfurtunently, its non-static.
Try like this:
public static class MessageBox
{
public static void Show(string message)
{
HttpContext.Current.Response.Write(string.Format("<script>alert('{0}');</script>", message));
}
}
Of course, you will need to implement your own correct JavaScript to display you MessageBox on client-side. But I thing – you approach is wrong. For you method programming try to look in ASP.NET MVC, becouse classic ASP.NET do not fully support JavaScript coding.
精彩评论