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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.