Solved

Using Page.ClientScript.RegisterClientScriptBlock is not working

Posted on 2011-09-26
3
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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