RegisterClientScriptBlock and "Key" Value

G

Guest

Hi. Two questions about "RegisterClientScriptBlock":

1. I want to be able to insert a script block without having to worry about
whether the "key" is unique. As it turns out, if a script block with that key
has already been registered, I can't register another block with the same
key. So the problem is, if I have functions that just register script blocks,
am I going to have to dynamically generate keys for each one with some kind
of awful "counter" or something? Is there a way to leave this parameter NULL
or something and have it automatically create a new key?

2. When I provide the "Key" parameter to this function, I would have thought
that it would be somehow output as an attribute to the <script /> tag that is
generated on the client. Yet the <Script /> tag says nothing. So what's the
"key" for except to manage uniqueness on the server side? Maybe that *is* all
it's for.

Thanks.

Alex
 
L

Laurent Bugnion

Hi,

Alex said:
Hi. Two questions about "RegisterClientScriptBlock":

1. I want to be able to insert a script block without having to worry about
whether the "key" is unique. As it turns out, if a script block with that key
has already been registered, I can't register another block with the same
key. So the problem is, if I have functions that just register script blocks,
am I going to have to dynamically generate keys for each one with some kind
of awful "counter" or something? Is there a way to leave this parameter NULL
or something and have it automatically create a new key?

2. When I provide the "Key" parameter to this function, I would have thought
that it would be somehow output as an attribute to the <script /> tag that is
generated on the client. Yet the <Script /> tag says nothing. So what's the
"key" for except to manage uniqueness on the server side? Maybe that *is* all
it's for.

Thanks.

Alex

The key is there to avoid that a given script is rendered more than once
on the page. Let's imagine that you create a checkbox control, for
example, with special features. Let's imagine that this control uses
client-side script. Now let's imagine that you have one page with 100
such checkboxes, each one of them rendering the exact same piece of
script, and you'll get the idea.

The "key" solution is not perfect, as you found out. I am reasonably
sure that there is no way to make sure that the key is unique, but it
can be pretty much anything (in a string form), so it's rather easy to
create a unique key.

For example, you can use the full qualified type name of a control, or
maybe a GUID for an object, etc...

HTH,
Laurent
 
W

Walter Wang [MSFT]

Hi Alex,

The Page class maintains a number of hashtables to track all the scripts to
be included in the response. A registered script is scoped with the type of
the page or control that uses it and is given a unique name. Attempts to
associate the same script multiple times with the same type and name are
blocked, and the script is registered and emitted only once. If you are
register the same script with a different name or in association with a
different type, the same script is emitted twice or more. If the script
contains some function definitions, as a result, a script error is raised
on the client when the script is invoked.

As a best practice, it's recommended that you check
ClientScript.IsClientScriptBlockRegistered first before adding script
block. For example:

Type t = this.GetType();
string myScriptBlockKey = "SetHomePage";
if (!ClientScript.IsClientScriptBlockRegistered(t, myScriptBlockKey))
{
string js = ...
ClientScript.RegisterClientScriptBlock(t, myScriptBlockKey, js, true);
}

Internally, ClientScriptManager uses the Type+Key pair to uniquely identify
which client script block is registered or not.

As for your questions:

1. Leaving the key to null will not create a new key automatically,
actually Type+null is able to uniquely identity a script block. As far as I
know, there's no way to automatically create this key. However, it's very
easy for you to use Guid.NewGuid().ToString() to create a unique string key.

2. The Type+key is internally used by ClientScriptManager to maintain the
hashtable, it's not exposed in client-side code.

Hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top