Javascript on user controls

  • Thread starter Thread starter Mantorok
  • Start date Start date
M

Mantorok

Hi

I have a user control (ascx) and it has some javascript, however I want to
use 3 instances of this control on my form - the problem is that the
javascript functions are all being generated 3 times with obviously
identical function names and each control is calling the last one.

Any ideas on how to conqure this?

Thanks
Kev
 
thx to this you won't get 3 JS scripts out on the page, isn't that what you
wanted?
 
Joe Dotnet said:
thx to this you won't get 3 JS scripts out on the page, isn't that what
you wanted?

Yes, but does this mean having to write the JS in my codebehind and
registering it from there?

Kev
 
yes but!
But you can add JS like this:
<script language=javascript src="source.js"></script>

if you want to have js code in ascx file but still detect if this code was
allready added, you have to write a test method, that checks Page object for
type of your control. Something like this.
 
If you need all 3 instances to do a single static action (say, display
alert with a generic warning message) , you can keep the javascript
funtion in
<script language=javascript src="jscode.js"></script>

If you need the javascript function specific to each instance of the
control() , one simple way to achive is rendering 3 javascript
functions with a unique name

<Script>
function SomeName_<%=this.ClientID%>();
{

}
</script>

if you have 2 instances of the control as Ctrl1 and Ctrl2, this
function would be rendered as
function SomeName_Ctrl1();
function SomeName_Ctrl2();

I forget, if this.ClientID could be accessible from HTML code like
this. If not you could declare a protected string variable and assign
ClientID to it in Page_Load
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top