I have a toolbar in a user control. I would like to wire the toolbar's onclick event to a javascript function on the page that contains the user control. What's the easiest way to do this?
I get the the javascript error: "MyControl is not defined"
However, MyControl has the register tag at the top of the page as well as its declaration inside the form tag. It certainly seems to be defined given that everything in it (like Button1) render on the page.
do you need to write individually the function to be called on each button on your toolbar on you user control? To be able to access it on you page you need to write a property for the function name then set it on your user control. Do it like this on your
user control:
public string onClickClientScript
{
set
{
Button1.Attributes.Add("onClick",value);
}
}
mrbrightside
Member
37 Points
57 Posts
Call javascript function on containing page from user control
Feb 24, 2008 04:36 AM|LINK
I have a toolbar in a user control. I would like to wire the toolbar's onclick event to a javascript function on the page that contains the user control. What's the easiest way to do this?
VinceArcher2...
Participant
856 Points
178 Posts
Re: Call javascript function on containing page from user control
Feb 24, 2008 05:36 AM|LINK
What controls are you using for your toolbar? You can add attribute to it link the appropriate function in javascript. Like this:
Button1.Attributes.Add("onClick", "JavascriptOnClick()");
Software Engineer
Philippines
[Please click "mark as answer" If my reply helps you]
Learn more from LearnITFromVince.BlogSpot.com
mrbrightside
Member
37 Points
57 Posts
Re: Call javascript function on containing page from user control
Feb 24, 2008 04:34 PM|LINK
Well, using your example, the Button1 would be inside my user control. And the JavascriptOnClick() function would be on my main page.
When I add the attribute declaratively like this:
<button ID = "Button1" runat="server" OnClick="JavascriptOnClick()"
I get the server error:
'JavascriptOnClick' is not a member of ASP.mycontrol_ascx'.
Now referring to your example, if I do this in my main page (the one that contains mycontrol.ascx):
Button1.Attributes.Add("onClick", "JavascriptOnClick()");
The problem is that Button1 is inside MyControl. So I get the javascript error: "Button1 is not defined"
If I do:
MyControl.Button1.Attributes.Add("onClick", "JavascriptOnClick()");
I get the the javascript error: "MyControl is not defined"
However, MyControl has the register tag at the top of the page as well as its declaration inside the form tag. It certainly seems to be defined given that everything in it (like Button1) render on the page.
VinceArcher2...
Participant
856 Points
178 Posts
Re: Call javascript function on containing page from user control
Feb 25, 2008 01:08 AM|LINK
do you need to write individually the function to be called on each button on your toolbar on you user control? To be able to access it on you page you need to write a property for the function name then set it on your user control. Do it like this on your user control:
public string onClickClientScript { set { Button1.Attributes.Add("onClick",value); } }this will set the clientScript for Button1
Software Engineer
Philippines
[Please click "mark as answer" If my reply helps you]
Learn more from LearnITFromVince.BlogSpot.com
kunalsmile
Member
2 Points
1 Post
Re: Call javascript function on containing page from user control
Nov 10, 2008 06:21 AM|LINK
prajaktadesa...
Member
2 Points
1 Post
Re: Call javascript function on containing page from user control
Nov 02, 2010 06:50 AM|LINK
Thank you....it really works........