Solved

Using Page.ClientScript.RegisterClientScriptBlock is not working

Posted on 2011-09-26
3
365 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

680 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