Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7759805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:43:14+00:00 2026-06-01T13:43:14+00:00

On the OnPreRender of my custom server control, I am registering an alert to

  • 0

On the OnPreRender of my custom server control, I am registering an alert to popup everytime the page is loaded after a partial postback (add_pageLoaded(function() { alert(‘Hi’)})). I only want this to be called once, but its gets called as many times as there has been partial postbacks. For example, in the code below, if you open the page and click “Click me” you get one alert, click again and you will get two alerts, three –> three alerts and so forth. My understanding was that ScriptManager.RegisterClientScriptBlock, using the scriptKey parameter was suppose to check if the script has already been registered, and if so, dont register it again.
I cannot register this script on !IsPostBack because my custom control will be inserted during Postbacks into pages, so using !IsPostBack will not include my script.

Here is a simple example of this problem:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"                                          Inherits="RegisterClientScriptExperimentation._Default" %>
<%@ Register Assembly="RegisterClientScriptExperimentation"         Namespace="RegisterClientScriptExperimentation" TagPrefix="cc" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:ScriptManager ID="ScriptManager1" runat="server"/>       
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <asp:Panel ID="panel" runat="server"/>                    
            </ContentTemplate>
        </asp:UpdatePanel>        
    </div>
    </form>
</body>
</html>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace RegisterClientScriptExperimentation
{
    public partial class _Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            panel.Controls.Add(new CustomControl());
        }
    }

    public class CustomControl : CompositeControl
    {
        private Button button;
        public CustomControl()
        {
            button = new Button();
            button.Text = "Click me";
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            Controls.Add(button);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            StringBuilder _text = new StringBuilder();
            _text.Append("var prm = Sys.WebForms.PageRequestManager.getInstance();");
            _text.Append("prm.add_pageLoaded(function() { alert('Page Loaded'); });");

            if (null != System.Web.UI.ScriptManager.GetCurrent(Page) && System.Web.UI.ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "customControlScript", _text.ToString(), true);
            else
                Page.ClientScript.RegisterStartupScript(this.GetType(), "customControlScript", _text.ToString(), true);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:43:15+00:00Added an answer on June 1, 2026 at 1:43 pm

    I found the solution to this.

    In your page events, after you execute whatever you need to do, you need to remove the function from the event which you hooked into.

    For example:

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(DoSomething);
    
    function DoSomething() {
      alert('Hello');
      Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(DoSomething);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control which inherits from System.Web.UI.Control and some of its properties
I had been created text box custom validation control and I'm using this in
So, I'm writing a custom validator to validate the Ajax Toolkit's Rating control. The
in my overridden OnPreRender function, I can modify the text of a label: Label1.Text='in
When I create a control dynamically and add it to the page's controls collection,
I have an ASP.NET page with two instances of the same Web User Control
I have a custom DropDownList control built off of a Telerik RadComoBox . I'm
I have a custom control that is based off the gridview control located at:
I am trying to do an ASP.net custom control for the Flow Player flv
I have a custom UserControl which uses a simple ITemplate: <asp:Panel runat=server ID=pnlExpander CssClass=expander>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.