Javascript code in code page RRS feed

  • Question

  • Hi,

    I am trying to write a javascript confirmation script in the page load of a server side.
    anyone got any samples?


    • Moved by Harry Zhu Thursday, March 4, 2010 2:41 AM (From:JScript for the .NET Framework)
    Thursday, February 25, 2010 1:05 PM

All replies

  •   public void Page_Load(Object sender, EventArgs e)
        // Define the name and type of the client scripts on the page.
        String csname1 = "ConfirmationScript";
        Type cstype = this.GetType();
        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;
        // Check to see if the startup script is already registered.
        if (!cs.IsStartupScriptRegistered(cstype, csname1))
          String cstext1 = "alert('Confirmed!');";
          cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    • Proposed as answer by Machine Elf Thursday, February 25, 2010 1:16 PM
    Thursday, February 25, 2010 1:16 PM
  • I was looking for a delete confirmation pop up with OK and CAncel buttons in the pop-up in server side.
    Thursday, February 25, 2010 1:34 PM
  • Or can we write the Javascript in the client side and call in the server side page_load?
    Thursday, February 25, 2010 1:38 PM
  • Here's a client side demo of an AJAX call to a Page Method that you could automatically trigger similarly to the page load code from before. Instead of "alert('Confirmed!');" pass "DeleteItem(1);", and replace 1 with the identifier of the item you're deleting. (I included an ID parameter because that's typical for deletion routines). You could also give the user a way to trigger the client script later on, without needing to reload the page.

    Add a using statement to the previous example for System.Web.Services and create a static method decorated with the [WebMethod] attribute.

    using System;
    using System.Web.Services;
    public partial class ConfirmDel : System.Web.UI.Page {
        public static void DeleteItem(int id) {
            if (Something.isRemovable(id)) Something.remove(id);
            else throw new ArgumentOutOfRangeException(String.Format("Item #{0} was not found.", id));

    You can add the following client script to your page using ScriptManager if you like, or just add a script tag.

    function DeleteItem(itemNo) {
        if (confirm('Are you sure you want to delete item #'+itemNo+'?')) {
                function() { 
                   alert('Item #'+itemNo+' was deleted.'); 
                function(err) { 
                   alert('Cannot delete item #'+itemNo+'. ' +err.get_message()); 


    • Proposed as answer by Machine Elf Friday, February 26, 2010 10:00 AM
    • Edited by Machine Elf Friday, February 26, 2010 10:02 AM grammar
    Friday, February 26, 2010 10:00 AM
  • Hi,

    For the
    questions relating to ,please post to:


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, March 4, 2010 2:41 AM