admin管理员组

文章数量:1321445

I am using the following code inside a btn Click event:

 ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "alert('you just registered the start up script');", true);

The script gets registered inside the <body> and I do not get any alert.

What's the way to register startup script and get this alert message?

I am using the following code inside a btn Click event:

 ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "alert('you just registered the start up script');", true);

The script gets registered inside the <body> and I do not get any alert.

What's the way to register startup script and get this alert message?

Share Improve this question edited Feb 8, 2012 at 16:51 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Feb 8, 2012 at 16:49 AnanthAnanth 10.7k25 gold badges86 silver badges112 bronze badges 5
  • Have you tried adding the parentheses to the function call? For example, instead of "exampleScript", try "exampleScript()". – Alex Morales Commented Feb 8, 2012 at 16:53
  • thats the "script key", not the function name. It just registers the script to the registerstartupscript with that key – Mikey G Commented Feb 8, 2012 at 16:54
  • 1 are you trying to register startup script from within update panel ? – Sebastian Siek Commented Feb 8, 2012 at 16:58
  • @Devjosh Sry abt that. I was just doing a sample application without any ajax. – Ananth Commented Feb 8, 2012 at 17:32
  • no problem @Ananth try the solutions suggested by us all hope you will get your answer out of them cheers!!! – Devjosh Commented Feb 8, 2012 at 17:33
Add a ment  | 

5 Answers 5

Reset to default 5

Are you using AJAX?

So whenever you have an instance of ScriptManager on your page, you need to register the Script using ScriptManager. So you need to change the code like this

ScriptManager.RegisterStartupScript(this, this.GetType(), "exampleScript", "alert('Hello');", true);

The problem is that you need to call ClientScript.RegisterStartupScript during an event like Page_Load(), not Button_Click.

That way, to execute the code, you need to a client-side Javascript call to the code

Page_Load()
{
    ClientScript.RegisterStartupScript(typeof(Page), "exampleScript", "yourFunc();", true);
}

<asp:Button OnClientClick = "yourFunc()" .....

http://msdn.microsoft./en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

Try this:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "myKey", "alert('you just registered the start up script');", true);

I believe the answer is already given so let me explain what options you have in ASP.NET.

ASP.NET provides two ways of including JavaScript resources to your pages.

  1. ClientScriptManager (accessible via Page.ClientScript)

  2. ScriptManager and ScriptManagerProxy

The second option supports more or less the same as the first but has also support for ASP.NET AJAX and WebService & Script References. If you don't use any of those extra's then usually I choose the first option.

It's not clear what exactly you're using but If you use a update panel then you will have to use the second option.

if you want to execute the script on button click then don't useClientScript.RegisterStartupScript()

i will advise you to use ClientScript.RegisterClientScriptBlock() instead it attaches the JavaScript with button client click event

ClientScript.RegisterClientScriptBlock(Page, typeof(Page),alert(' i am executed on button click'), true);

refer this

Use ClientScript.RegisterStartupScript() if you want to execute script on every time the page loads

you can also optionally use the following method
btn.Attributes.Add("onclick","<script>alert('hello alert')</script>"); in the Page_Load() event if you wish to register the script with button on the page load

本文标签: cUnable to register startup scriptStack Overflow