Solved

IsClientScriptBlockRegistered  is not working

Posted on 2009-04-02
9
1,367 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

13 Experts available now in Live!

Get 1:1 Help Now