This is a migrated thread and some comments may be shown as answers.

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

1 Answer 29 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mikael
Top achievements
Rank 1
Mikael asked on 17 Jan 2013, 02:40 PM

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

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 22 Jan 2013, 02:04 PM
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.
Tags
General Discussions
Asked by
Mikael
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or