Solved

IsClientScriptBlockRegistered  is not working

Posted on 2009-04-02
9
1,415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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