Solved

IsClientScriptBlockRegistered  is not working

Posted on 2009-04-02
9
1,399 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:shieldguy
9 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 24053227
You need to use the SAME overloaded function as your RegisterStartupScript().  So use
  this.Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "sScriptKey")
0
 
LVL 5

Expert Comment

by:Rikus_Trent
ID: 24053236
Try IsStartupScriptRegistered() instead.
0
 
LVL 1

Author Comment

by:shieldguy
ID: 24053399
what do u mean by same overloaded function??
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:shieldguy
ID: 24053446
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 24053455
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
 
LVL 1

Author Comment

by:shieldguy
ID: 24053487
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
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 24053990
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24054534
Check for postback
0
 
LVL 1

Author Comment

by:shieldguy
ID: 24057654
I cant check for the post back as code is placed at load events of both locations
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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