views:

37

answers:

1

A minor question that I hope admits of a simple answer that I'll kick myself for not noticing.

So, when we have the following overload of RegisterStartupScript

public static void RegisterStartupScript(
    Control control,
    Type type,
    string key,
    string script,
    bool addScriptTags
)

we have to provide a type as well as a control. Now, I can see what the point of specifying the control - the script gets pushed out just in case the control is part of the partial page render. But what is the point of the Type parameter? Usually one just sets it to the type of the control. And this is in fact the suggestion made by MSDN:

control
    Type: System.Web.UI..::.Control
    The control that is registering the client script block.

type
    Type: System..::.Type
    The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.

So why do we have to specify it? Presumably not just to spare the .Net Framework the bother of retrieving the runtime type itself.

+1  A: 

Usually type is the type of the page or control that registers a script. This is just a way to prevent that two different controls register different scripts using the same key.

Excerpt from MSDN:

A client script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

M4N
Oh, so the control itself isn't part of what uniquely identifies the script? Because you might have a similar script that's associated with controls of a certain type. Yes, I see. Cool, thanks.
Yellowfog

related questions