Solved

Using Page.ClientScript.RegisterClientScriptBlock is not working

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now