Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register javascript inside User Control using C#

I want to call javascript function from User Control using C#. For that i am trying to use

ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", "javascript:ShowPopup('Select a row to rate');", true); 

but it is not working for me. This works fine on the page. Can some one help me out how can i call javascript function at runtime using C#.

Thanks,

like image 388
mehul9595 Avatar asked Dec 29 '22 02:12

mehul9595


2 Answers

Try this.GetType() instead of typeof(string):

ScriptManager.RegisterStartupScript(this, this.GetType(), "alertbox", "ShowPopup('Select a row to rate');", true); 
like image 62
zavaz Avatar answered Jan 08 '23 00:01

zavaz


The following is taken from working code, showing script being registered to fire from an asynchronous postback in an UpdatePanel.

ScriptManager.RegisterStartupScript( this.upnl, this.upnl.GetType(), Guid.NewGuid().ToString(), "alert('test');", true );

If your code is not executed from inside an UpdatePanel, it still should not be typeof(string); you should use the type of some container (typically the control itself).

Type: The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.

like image 28
Tim M. Avatar answered Jan 07 '23 22:01

Tim M.