Click here to Skip to main content
13,550,883 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 18 Jan 2013
Licenced CPOL

Running a Client Side Script from Server Side Code in ASP.NET

, 18 Jan 2013
Rate this:
Please Sign up or sign in to vote.
How to run a client side script from server side code in ASP.NET

I needed to get a pop-up window to come up in ASP.NET. Now while JavaScript happily does this (with the alert(); function, ASP.NET does not. So I needed to work out how to do it. Another wrinkle in this comes from Ajax. None of the JavaScript code seemed to work when placed in an AJAX page.

As I have now been tasked with changing this code and moving from the Anthem Ajax library to a pure MS solution, it was time for this died in the wool VB/C# developer to get his head around getting these scripts working.

To do this:

  • Create a simple ASP.NET Web Project
  • Right click with the mouse on the Project title (this will be bold)
  • Select ‘Add’ and then ‘New Item’


    <%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="TestPage.aspx.cs" Inherits="TrustTest.TestPage" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    <html xmlns="" >
    <head runat="server">
        <script type="text/javascript">
          function pageLoad() {
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <asp:Button ID="Button1" runat="server" Text="Press Me!" 
                    onclick="Button1_Click" />
    protected void Button1_Click(object sender, EventArgs e)
        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), 
              "alertScript", "alert('you pressed me!');", true);
  • Select ‘AJAX Web Form’
  • Add a Button to the page
  • Now add a 'click' event handler to the button
  • And there it is. Now whenever you click on the button, a pop up appears saying ‘you pressed me!’



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul S. Chapman
Software Developer (Senior) Simplicita Online
United Kingdom United Kingdom
UK based IT Consultant. Started in 1985 selling home computers such as the Sinclair ZX Spectrum, BBC Model B and Commodore 64, and in 1987 moved into development, starting first with Torch Computers, developing software for the XXX UNIX Workstation.

Currently developing a new Azure/Silverlight based website/desktop applications for a new startup, hoping to launch in late 2009/early 2010

You may also be interested in...


Comments and Discussions

SuggestionGotcha Pin
Ruchin Munjal18-Jan-13 18:06
memberRuchin Munjal18-Jan-13 18:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 18 Jan 2013
Article Copyright 2013 by Paul S. Chapman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid