Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IsClientScriptBlockRegistered  is not working

Posted on 2009-04-02
9
Medium Priority
?
1,435 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

719 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