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
Solved

Using Page.ClientScript.RegisterClientScriptBlock is not working

Posted on 2011-09-26
3
364 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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