Client-side: State variables which are local to the control

2 posts, 0 answers
  1. Fredrik
    Fredrik avatar
    55 posts
    Member since:
    Oct 2007

    Posted 17 Jan 2013 Link to this post

    This is probably a newbie question, and is perhaps more of a javascript general question than a Telerik specific one. However, I guess many Telerik-developers know the answer :-)

    In many cases, we need to keep track of some kind of state in client:

    var someState;
      
    function SomeEventHandler() {
          someState = someState + 1;
    }

    In the code-behind, we hook-up the event to some Telerik event:
    theRadGrid.ClientSettings.ClientEvents.OnRowClick = "SomeEventHandler";

    This works, if there is only one "theRadGrid" on the page, but if there are multiple instances of the control, we have problem, since "someState" is shared across instances, it is global to the page.

    What is the best way to make state variables local to the instance?

    Hope I made myself understood :-)

    /Fredrik
  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1153 posts

    Posted 22 Jan 2013 Link to this post

    Hello Fredrik,

    There are a few possible resolutions for this. One is to call a different event handler for every grid and use different variables. Another option would be to use the same event handler and check which control fired the event. A demonstration of this is shown in this code snippet:

    ASPX:
    <telerik:RadGrid ID="RadGrid1">
            <ClientSettings>
                <ClientEvents OnRowClick="RowClick" />
            </ClientSettings>
        </telerik:RadGrid>
        <telerik:RadGrid ID="RadGrid2">
            <ClientSettings>
                <ClientEvents OnRowClick="RowClick" />
            </ClientSettings>
        </telerik:RadGrid>

    JavaScript:
    function RowClick(sender,args)
           {
               grid = sender;
               if (grid.UniqueID == "RadGrid2")
               {
                   alert("A row in the second grid was selected");
               }
           }


    Regards,
    Angel Petrov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top