For veteran C# windows developers ASP.Net can be a strange new world. I personally found myself stuck on what seemed to be a simple task, displaying a message box to the user with simple C# code in ASP.Net.
In this article I'll briefly explain the most effect method to write a MessageBox function for ASP.Net.
A simple solution would be to import the System.Windows.Form reference so you can use the familiar MessageBox.Show("") C# function.
However you have to keep in mind this would be run from the ASP.Net server and not all browsers like that...
A much more elegant solution is to use ASP.Net to call a client side programming language like JavaScript.
The java code for a message box is as follows:
window.alert("[message here]")
Integrating JavaScript with ASP.Net
The first step to integrating JavaScript in ASP.Net is to set up the script for web format:
<script language='javascript'>
window.alert("[message here]")
</script>
This next step has some flexibility, which is getting C# in ASP.Net to run the script. The way I like to do it is by adding LiteralControl to the Page's Controls with the script. Simple and it works, here is the full C# function:
private void MessageBox(string msg) { Page.Controls.Add(new LiteralControl(
"<script language='javascript'> window.alert('"+ msg + "')</script>")); }
Drawbacks
The drawbacks of such simplicity is simplicity itself. The above C#.Net function will display a message box to a user from an ASP.Net website. However it cannot do much more like display a meaningful Yes-No message box or ask for user input.
Yet this is a great tool for debugging your ASP.Net websites and displaying helpful messages to users...