IsClientScriptBlockRegistered is not working

I am using RegisterStartupScript to add javascript thorugh asp.net code
this code is present at two place in my web page, one it is on the web page load event and other it is present on the load event of a user control which is a part of the same class.

Right now whats happening that this code is registering the javascript code twice but I want control page code to not register if the main page code has already registered the code
why my IsClientScriptBlockRegistered is not working???
Thanks

//same code at both places
if (this.Page.ClientScript.IsClientScriptBlockRegistered("sScriptKey") == false)
{
                  if (sScript != string.Empty)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "sScriptKey", sScript,true);
                    }
}

Open in new window

LVL 1
shieldguyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MogalManicConnect With a Mentor Commented:
Then change your code to:
if (this.Page.ClientScript. IsStartupScriptRegistered("sScriptKey") == false)
{
  if (sScript != string.Empty)
  {
    this.Page.ClientScript.RegisterStartupScript("sScriptKey", sScript,true);
  }
}

Open in new window

0
 
MogalManicCommented:
You need to use the SAME overloaded function as your RegisterStartupScript().  So use
  this.Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "sScriptKey")
0
 
Rikus_TrentCommented:
Try IsStartupScriptRegistered() instead.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
shieldguyAuthor Commented:
what do u mean by same overloaded function??
0
 
shieldguyAuthor Commented:
I need to use RegisterStartupScript because I want to add java script to the end of the page
Is their any other way


I did try using IsStartupScriptRegistered but still same result the same javascript is added twice
0
 
MogalManicCommented:
RIKUS is right

There are multiple versions of the same function name :
        public bool IsStartupScriptRegistered(string key);
        public bool IsStartupScriptRegistered(Type type, string key);

You want to use the IsStartupScriptRegistered() that corresponds with your call to RegisterStartupScript().

Since you are registering using Type and Key you need to use the 2nd one.
        public bool IsStartupScriptRegistered(Type type, string key);
     
if (this.Page.ClientScript. IsStartupScriptRegistered(this.GetType(), "sScriptKey") == false)
{
  if (sScript != string.Empty)
  {
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "sScriptKey", sScript,true);
  }
}

Open in new window

0
 
shieldguyAuthor Commented:
I want not to use the 2nd one , so if some how on page control i can detect that same script key is already registered then dont add it otherwise add javascript code
0
 
williamcampbellCommented:
Check for postback
0
 
shieldguyAuthor Commented:
I cant check for the post back as code is placed at load events of both locations
0
All Courses

From novice to tech pro — start learning today.