2

I have implemented a color picker control in a web forms project and would like to make sure that CSS/JS is loaded maximum once. I have tried the following:

Protected Shared _clientNeeded As Boolean = True
Public Sub NeedClient()

    If (_clientNeeded) Then
        _clientNeeded = False
    Else
        Return
    End If
    'Loading the client-side
End Sub

The problem is that after building, _clientNeeded is true at first but then becomes false and will be false forever, so Shared, as my try did not fulfill my goal. So, how could I make sure that loading the client-side will occur if there are color pickers, but not more than once.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Use `Session` variable – techspider Jun 22 '16 at 18:28
  • That's user-level persistence. I would like to have a page-level persistence, but not sure how to do it in asp.net. – Lajos Arpad Jun 22 '16 at 18:30
  • You can't achieve what you need using `Shared` or `Static` as this is not the safe way to do your task; you might need to implement a custom class for your needs; – techspider Jun 22 '16 at 18:31
  • Please see [this](http://stackoverflow.com/questions/8919095/lifetime-of-asp-net-static-variable) and [this](http://www.codeproject.com/Questions/295786/STATIC-variables-LifeTime-in-ASP-NET-application) and be vary of [this](http://stackoverflow.com/questions/14154892/scope-of-static-variable-in-multi-user-asp-net-web-application) – techspider Jun 22 '16 at 18:34
  • I understand that Shared is not the solution. This is the starting point of the question as well. Basically this could be solved by adding a property to the Page owning the Control and having access to that property from the control. But that is hacky. So I wonder whether there is a better solution – Lajos Arpad Jun 22 '16 at 18:37
  • 1
    You have the solution :) If you have `User Control` or `Custom Control`, I vote for your way; I did it in similar fashion when I had to retain a value like this – techspider Jun 22 '16 at 18:39
  • You are probably right, but I will wait for other inputs as well. If there will be answers I will choose the best one. If not, then I will answer the question. – Lajos Arpad Jun 22 '16 at 18:43
  • one murky way is to define a `Class` with multiple properties - one for each page; store that object in `Session` and perform read/write in the page with the property relevant to that page – techspider Jun 22 '16 at 18:47
  • That would mean one/page life cycle. If n users are loading the same page at the same time, that would be n values for that page. The problem is that the value of Session should be emptied. I still stick to the Page property idea as the best so far, but I hope there are more elegant ways. For now I will implement that one. – Lajos Arpad Jun 22 '16 at 18:52

1 Answers1

1

You could implement the property with a combination of Context.Items and ViewState. When the page loads several controls, the value of ClientNeeded can be shared between them through the Context.Items of the page. Once the property has been set, each control can store it in its own ViewState, so that it is maintained as long as the page is active:

protected bool ClientNeeded
{
    get
    {
        object value = ViewState["ClientNeeded"];

        if (value != null)
        {
            return (bool)value;
        }
        else
        {
            value = Context.Items["ClientNeeded"];
            if (value != null)
            {
                ViewState["ClientNeeded"] = value;
                return (bool)value;
            }
            else
            {
                return true;
            }
        }
    }

    set
    {
        Context.Items["ClientNeeded"] = value;
        ViewState["ClientNeeded"] = value;
    }
}
Community
  • 1
  • 1
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146