JavaScript with User Control

S

Steve Murphy

I'd like to package a navigation bar into a user control. The problem I'm
having is getting the rollover buttons to work. Where do I place the
Javascript for the image caching and switching for the rollover effect? I'm
using image buttons.

Thanks in advance,
Steve Murphy
 
G

Grant Merwitz

I would place the JavaSCript in the Control itself.

I've often had problems with JavaScript in UserControls, as they are renamed
when placed onto the ASPX page.
When you register your ASPX page, and give your tag a name, in the output
HTML that goes to INternet explorer, you controls are renamed the tag name
followed by the ID you specified.

So if you have a textbox:
<asp:TextBox id="tb"
and you register your usercontrol
TagName="MyUserControl"
In I.E. the Textbox will now have the ID
MyUserControl_tb

So when you have javascript thats referring to this ID, it will stop working
untill you change this to refer to MyUserControl_tb.
Now this becomes a bit silly, because that ID name is based on its instance
in a given ASPX page, and cannot be generalised.

There was a post a few days ago about how to get the controls name from the
code and then generate the JavaScript there with thee right ID;s
I'm just gonna dig that up (cause i need it to for future reference), and
will post shortly

HTH
 
G

Grant Merwitz

ok, found it

This i have not tested, but will next time i have this problem

btnMyButton.UniqueID

I think .. THINK .. if you creating your JavaScript from your Code Behind,
when you try do something like
document.GetElementByID('btnMyButton');
swap this for

document.GetElementByID('" + btnMyButton.UniqueID + "')
and it should use the right ID

The same logic would go for ImageButtons with rollovers - if you need to
reference the ImageButtons ID

HTH
 
S

Steve Murphy

Grant Merwitz said:
I think .. THINK .. if you creating your JavaScript from your Code Behind,
when you try do something like
document.GetElementByID('btnMyButton');
swap this for

document.GetElementByID('" + btnMyButton.UniqueID + "')
and it should use the right ID

Why can't you just use:
document.GetElementByID('btnMyButton.UniqueID')

Why do you need to wrap it?

Thanks,
Steve Murphy
 
G

Grant Merwitz

I was referring to the code behind where you would build up the jscript then
register it on the page

string MyJScript = "<Script language="javascript">";
MyJScript += "function something() { document.GetElementByID('" +
btnMyButton.UniqueID + "');}";
MyJScript += "</script>";

Page.RegisterClientScriptBlock("SomeKey", MyJScript");


as btnMyButton.UniqueID is a c# function, not a JavaScript function
 

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

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top