Solved

IsClientScriptBlockRegistered  is not working

Posted on 2009-04-02
9
1,378 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem of RegEx to match the first occurence of 10 35
Not showing page correctly 3 29
Error in script 11 45
execute SQL Stored Procedure 1000 times from vb.net 3 19
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now