0

I want to access a usercontrol by a predifened name, like the DevExpress components can. Something like this:

"<dx:MyUserControl runat="server" ID="My" ClientInstanceName="MyClient"></>"

In my main page I would like to access a client method of my usercontrol. Something like:

MyClient.DoSomething();

In my usercontrol page there would be an object that implements this method:

function myClass{
    DoSomething : function(){ var a = 2 + 2;}
}

So, normally I would declare the object like : var MyClient = new myClass();

But now MyClient is dynamic, the ClientInstanceName could be whatever...

So how could I do this?

Maybe JSON or eval?

1 Answer 1

1

Your question is a bit dual meaning, so let me post answer of both the meanings that I found.

You have a variable named MyClient and a class named myClass.

Now, even if we assume both the names here are dynamic, and you get and store both the names in variables A and B, such that

if B equals "foobar" , then you are sure that there is a class on the page like

function foobar() { ... }

and if A equals "myFoobar", you know that you have to actually do something like

var myFoobar = new foobar();

Now, since both the variable and the class are globally scoped, you can easily do

window[A] = new window[B]();

and that's it!

Sign up to request clarification or add additional context in comments.

4 Comments

So does it mean, that in my usercontrol page I could also declare something like: var myObject = new myClass(); window['<%# clientInstanceName %>'] = myObject;
Not sure what the JS of your page is like, but if the class is a global class (not inside any function), then you can do this.
<%# clientInstanceName %> does not seem like valid JS, is someone preprocessing it ? If yes, and that someone is simply replacing <%# clientInstanceName %> with a string, then yes.
Yes it is a server property (string)that i set in the declaration of my usercontrol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.