Creating a demo version of your ASP.NET application

Setting up a live demo on your website is often important for your business - people want to see how the application looks and get a feeling of it and not all people are willing to download a trial version and go through a (painful?) install process just to get the first look.

However, the problem is that you need to strip down the demo version a little bit. You don't want visitors to be able to change login password etc (after that noone would have access to the demo) or change information in your underlying database.

Here's how I have solved it for my demosites for AdMentor and KBMentor.

In short : I compile the application with an extra flag defined (DEMOVERSION) and in my code I insert some extra conditional code to behave differently when that flag DEMOVERSION is defined.

First step is to create a custom project context

Then we add the conditional variable (called DEMOVERSION):

Now we can insert some conditional code.We do that by using the #if directive in our code.

#if (DEMOVERSION)
here we can add code which only will be compiled in the special demoversion
#endif

I want to throw up a JavaScript when the user tries something not allowed - for example when trying to change password:

So, this is done by adding a JavaScript alert box to the onclick event:

private void Page_Load(object sender, System.EventArgs e)
{
EnsureEncType();
EnsureChildControls();
if ( !IsPostBack )
{

#if (DEMOVERSION)
btnUpdate.Attributes.Add("onclick", "alert('Not possible in demo version'); return false;");
btnDelete.Attributes["onclick"] = "alert('Not possible in demo version'); return false;";
#endif
Now, this is done in Page_Load and while this example is pretty easy to understand you might have a more finegrained structure of how to enable/disbale controls, for example based on current user rights, and/or after a lot of postbacks a certain button might be enabled etc. The easiest way to override such code without needing to make the original code much more complex is to leave it at is and make your changes in OnPreRender:
protected override void OnPreRender(EventArgs e)
{
#if (DEMOVERSION)
btnDelete.Enabled = false;
btnDoUpload.Enabled = false;
btnOK.Enabled = false;
#endif
Here it doesn't matter what code has been executed and checked against all different kind of state - we just disable the buttons regardless of which. Now, the last thing to do. Not all people have JavaScript enabled - so those people must also be stopped. Here we need to add some serverside code in the actual handler functions:
private void btnOK_Click(object sender, System.EventArgs e)
{
#if (DEMOVERSION)
return;
#endif

..regular code to save etc

0 comments:

Blog Archive