Solved

Using Page.ClientScript.RegisterClientScriptBlock is not working

Posted on 2011-09-26
3
361 Views
Last Modified: 2012-05-12
My web page is quite simple, but after I click the button it is not working. Thanks for help.
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Adding JavaScript</title>
    <script runat="server">
        protected void Page_Lord(object sender, EventArgs e)
        {
            string myScript = @"function AlertHello() { alert('Hello ASP.NET');}";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myScript, true);
        }
     
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="AlertHello()" />
        <br />
        <br />

    </div>
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:zhshqzyc
  • 2
3 Comments
 
LVL 16

Expert Comment

by:disrupt
ID: 36599885
Use a scriptmanager:

 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "MyScript", "alert('test');", true);

make sure you have

 <asp:ScriptManager runat="Server" ID="ScriptManager1" />

in your aspx page
0
 

Author Comment

by:zhshqzyc
ID: 36599978
I found the error. If I remove the script block above html tag, then it works.
0
 
LVL 16

Accepted Solution

by:
disrupt earned 500 total points
ID: 36600072
Gotcha, you should use code-behind to better separate your logic
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question