Coming from a windows programming background to the web has it's pros and cons. One of the major annoyances for me was that I was so used to calling MessageBox.Show() every time I needed to inform the user of something.
While I was programming the logic behind the login form I wanted to use MessageBox.Show so I decided to create a class that would render the MessageBox as a Javascript alert. Javascript isn't anywhere near as extendable as a windows form so this is the only implimentation possible just a simple MessageBox.Show(String value); There are no overloads for changing the caption or the style of the buttons since Javascript won't allow it.
As of right now this class only uses the Javascript alert function, I may extend this to include the confirm and/or prompt functions later on if the need or want arises.
Here is the Code for the MessageBox Class: using System; using System.Text; using System.Web.UI;
public static class MessageBox { // Create a string builder to help format the javascript private static StringBuilder sb;
public static void Show(string strMessage) { // make sure our string builder is initialized with an empty value sb = new StringBuilder();
// Rewrite our Message to conform with javascript syntax strMessage = strMessage.Replace("\n", "\\n"); strMessage = strMessage.Replace("\"", "'");
// Create the javascript to be sent to the client sb.Append("");
// Get the page that is requesting this method Page pgExecutingPage = HttpContext.Current.Handler as Page;
// Wire up the unload event of the requesting page so that we can // add our javascript to the end of the response pgExecutingPage.Unload += new EventHandler(pgExecutingPage_Unload); }
private static void pgExecutingPage_Unload(object sender, EventArgs e) { // Write the javascript to the end of the current response HttpContext.Current.Response.Write(sb); } }
As you can see in the following example MessageBox is set as a Public Static Class. This is so that we can call its' methods inside the class from anywhere in the application without having to create a new instance of the object.
public static class MessageBox {}
Next, we need to create a System.Text.StringBuilder as a private static member of our MessageBox Class. We will use the System.Text.StringBuilder to help in formatting the javascript to be sent to the browser. It is static so that our static methods can access it.
private static StringBuilder sb;
Then, we create our Show() method as a public static method in MessageBox. Our Show() method takes only one parameter which is a String value containing the message that will be formatted and then shown to the requesting user as a javascript alert.
public static void Show(string strMessage) {}
The first statement in our Show() method initializes our System.Text.StringBuilder and makes sure that the value held in the variable has been emptied each time this method is called.
sb = new StringBuilder();
The next statements of our Show() method are used to convert the C# way of building strings over to the javascript way. The first makes sure that the escape \n is passed insteads of a line break. The latter converts a double quote into a single quote.
Now we construct our Javascript alert function and append it to our System.Text.StringBuilder, our strMessage variable is concatenated between quotes to make: alert("Value of strMessage");
sb.Append("");
In order to print our javascript to the requesting page, we'll need a reference to it. So, let's cast the current HttpHandler as a System.Web.UI.Page.
Page pgExecutingPage = HttpContext.Current.Handler as Page
Let's wire up our pgExecutingPage_Unload() method to handle the Unload event of the current executing page. You could use the Load event but the MessageBox will render itself before anything else on the page has had a chance to load. In the Unload event it will render itself after everything else on the page has loaded.
pgExecutingPage.Unload += new EventHandler(pgExecutingPage_Unload);
The last method we'll create is the pgExecutingPage_Unload() method. Since we are at the Unload step in the pages execution whatever we add to the response will be appended to the very end of the page.
Finally we just print our System.Text.StringBuilder to the end of the current response stream.
HttpContext.Current.Response.Write(sb);
Well, that's how I put together my MessageBox class. If you found this how to helpful, Please, add a link to it from your site, add it to your social bookmarks or email it to somebody.
Scepter Marketing Technologies means: Powerful Marketing Technologies. We are currently past the design stage and with every day they get closer and closer to becoming a swift reality.
We intend our software to be an entirely free marketing resource for anyone willing to put it's power to use. Everything about it is incredible. Users are given access to automated tools for nearly everything they need to successfully create an online business.
From submitting messages to google groups, yahoo groups, topica groups, myspace groups, orkut groups, ect... submitting articles to article directories and websites that will accept them, posting messages to discussion forums, posting pages on sites like squidoo and ehow, building downlines in viral websites, surf4hits programs, mailing list builders, link exchanges, etc...
There are tools for automating all of these marketing activities and this is just the tip of the ice burg, there will be tools for quickly creating fresh and unique niche content, tracking advertising campaigns, building business leads in any niche and following up on all of those business leads.
That doesn't even touch on the fact that this software is a full blown social networking platform that allows you to create friends and groups of friends for chat, instant messaging, networking, helping each other create content, creating advertising co-ops and joint ventures, sharing links and bookmarks, rate and comment on anything.
We intend on the marketing of our software to spread similar to a virus. We're talking massive content creation mixed in with referral marketing and the incredible power of Web 2.0's best social networking tools.
Scepter Marketing Technologies will have no choice but to become the ultimate resource for internet marketers or anyone looking to make any money online!
The best feature of Scepter Marketing Technologies is that all of our marketing tools, resources or anything else that is developed will be - FREE! Our entire marketing system will be built around promoting empowerism's affiliate program and adwords clicks for monitization.
We will release it as a web application first, and then upgrade it to a desktop app later. The desktop app will introduce our membership to a whole new world of marketing capabilities. Download sites, P2P networks, torrent trackers, email attachments, instant messenger file sharing, compact disk burning, etc...
Further updates are planned for after that, but you'll have to read about them at another time. In the mean time, please join empowerism to support our development efforts, and to get in front of the massive spillover that will be created when this software is released.
Please, contact me on Google Talk "ScepterMT" If you think you can contribute to this project in any way. Subscribe, to the rss feed on this blog if you would like to get our progress updates or to be one of the first to know when it is released.
Scepter Marketing Technologies will become the most technologically advanced money making opportunity the world has ever seen. Please, become a part of the development process. Thank You.
ScepterDonFetti
feedcat.net promotes your content, measures audiences and saving load of your server resources!