473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

RegisterClientScriptBlock and "Key" Value

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
Aug 4 '06 #1
2 4625
Hi,

Alex Maghen wrote:
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Aug 4 '06 #2
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.

Aug 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nicolas Girard | last post by:
Hi, Forgive me if the answer is trivial, but could you tell me how to achieve the following: {k1:,k2:v3,...} --> ,,,...] The subtle point (at least to me) is to "flatten" values that are...
0
by: jy2003 | last post by:
1. Does MyISAM support FOREIGN KEY constraints? When I read the online MySQL Reference Manual, it said "InnoDB also supports FOREIGN KEY constraints". Does it mean MyISAM does not support FOREIGN...
5
by: http://links.i6networks.com | last post by:
I want to force the users to click submit to submit the forms. How do I disable "Enter Key" which will submit the form automatically when they entered the data in text field then pressed "enter key"
5
by: karteikarte | last post by:
hi, i just wondered how to retrieve the 4digit key of this script? anybody who can help? best regards Karteikarte <script type="text/javascript"> <!-- Key-code script by Bart Jellema var...
1
by: Suresh | last post by:
Hi all, Here is my code, if(event.which) keycode = event.which;//For Mozilla else keycode = event.keyCode;//For IE But my problem is, the value of keycode returns 46 for pressing...
2
by: microsoft | last post by:
When I create a instance of web services proxy in winform, I get the exception. Stack Trace: System.ArgumentException: Item has already been added. Key in dictionary:"winbootdir" Key being...
0
by: Ralf Gedrat | last post by:
Hello! I have a Application, this throws after some time following exception: Item has already been added. Key in dictionary: "- 1" key being added: "- 1" I use Application.Run with...
0
by: David Penne | last post by:
I have SBS 2003 and am using the built in tape backup. My question is about the registry key "Eject Tape Flag". I understand that this is an undocumented key and the only articles I can find...
0
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.