Search KB Filter article types
Form Concepts CodeSample - Using vwginstance to force new context created every time
Categories: State Management
Tags: Architects, Developers, Navigation, Visual WebGui Pipeline, C#, VB.NET, 2. Intermediate, 3. Advanced, Navigation, v6.3, v6.4 and Later
Revision: 2
Posted: 27/April/2010
Updated: 21/April/2011
Status: Publish
Types: Article

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

Note! For version 6.4 and later, using the ForcePageInstance setting in web.config is equivialent to using this workaround. See SDK Version Hightlights section.

Note! Beginning with version 'v6.4 Release c' we introduced a security enhancement that requires only alphanumeric characters to be used in value of the "vwginstance" argument.

Consider the following scenario: You have Form1 and Form2 as entry points (forms) in your application. Your user starts by entering Form1.wgx and then changes the browser's addressbar to point to Form2.wgx and finally he changes the addressbar again to point to Form1.wgx. The first time Form1 is accessed, the Form.Load event fires successfully. The second time, depending on the browser though, the Load event does not fire.

The reason for this behaviour is that the browser is using the same session and the same context, and the Load event does not fire unless a new context is created, as you can read more about in the Form Concepts and Form articles.

A second consequence of the same fact is that when the user comes back to Form1, using this method, he will enter Form1 in the same condition as he left it, provided no timeouts have cleared it in the meantime.

Possible workaround

Note!: For version 6.4 and later, using the ForcePageInstance setting in web.config is equivialent to using this workaround. See SDK Version Hightlights section.

As suggested in the Form article, you can use vwginstance query parameter to help you out here. When a form is accessed the first time, using the default parameters (no parameters), a new Context is created to store the form's state within the ASP.NET session. This context is identified by vwgcontext running number, starting at 0. Hence, the first context created will have vwgcontext=0. This is stored within a query variable inside of the HttpRequest object for every request made to that form (or context).

Building upon this fact, you can use the vwginstance=0 as some kind of "jumper" instance with the only purpose of creating new instances which will have non-zero vwgcontext and thereby making sure that for every request made to the default instance of the Form, you will get a new instance. This would even make it possible for you to run two different instances of your form inside of FireFox, each on a seperate tab.

The workaround presented here consists of creating a shared (static in C#) variable that will serve as an instance counter, and then you will use the Form.Load event to check for which vwginstance is being accessed each time. If it is vwginstance=0, you increment the instance counter and redirect to a new one (creating a new context automatically), and then make sure you Terminate the "default" context (vwginstance=0) before you finish.

VB.NET code
Imports Gizmox.WebGUI.Forms
Public Class Form1
 
    Private Shared sInstanceCount = 0
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Context.HttpContext.Request.QueryString("vwginstance").ToString() = "0" Then
            sInstanceCount += 1
            Context.Redirect("Form1.wgx?vwginstance=" + sInstanceCount.ToString())
            Context.Terminate(True)
            Return
        End If
    End Sub
 
End Class

C# Code
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
 
    static int sInstanceCount = 0;
 
    private void Form1_Load(object sender, System.EventArgs e)
    {
        if ((Context.HttpContext.Request.QueryString["vwginstance"].ToString() == "0"))
        {
            sInstanceCount++;
            Context.Redirect(("Form1.wgx?vwginstance=" + sInstanceCount.ToString()));
            Context.Terminate(true);
            return;
        }
    }
}

SDK Version highlights

See also

About the author

Related Articles

State Management  
Title Update Author
Tags: Architects, Developers, Resource Handlers, Visual WebGui Pipeline, 2. Intermediate, 3. Advanced, ASP.NET, DHTML, Pre v6.3, v6.3, v6.4 and Later
12/April/2011    2011/04/12