简体   繁体   中英

Calling same javascript function from different .Net User Control

I have a page and inside the page, I have 2 user controls. Both user control will include the same .js and call the same method (but pass different args into the method) from code behind. But only one of the user controls can able to execute the js.

User control 1

ScriptManager.RegisterClientScriptInclude(Page, typeof(Page), "AutoSlider", "../../../Themes/Ooredoo/js/Slider.js");
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "AutoSliderInit", "$(document).ready(function(){ SliderAutoSlide('ul#sliderImage'); });", true);

User control 2

ScriptManager.RegisterClientScriptInclude(Page, typeof(Page), "AutoSlider", "../../../Themes/Ooredoo/js/Slider.js");
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "AutoSliderInit", "$(document).ready(function(){ SliderAutoSlide('ul#smallSliderImage'); });", true);

When I tried to inspect element, I can only find UserControl1 javascript but not UserControl2.

What should I do in order for both usercontrols able to call the same js (pass diff args)?

You're registering the ClinetScriptBlock with the same Key but with different functions

ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "AutoSliderInit", "$(document).ready(function(){ SliderAutoSlide('ul#sliderImage'); });", true);
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "AutoSliderInitSmall", "$(document).ready(function(){ SliderAutoSlide('ul#smallSliderImage'); });", true);
                                                                           ^ Change the Key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM