Prev | Current Page 459 | Next

Rob Cameron and Dale Michalk

"Pro ASP.NET 3.5 Server Controls and AJAX Components"

ClientScript.RegisterStartupScript(
typeof(RolloverImageLink),
"PRELOAD_SCRIPT",
PRELOAD_SCRIPT.Replace("{arrayname}", SWAP_ARRAY),
true);
}
}
}
}
The first client script feature we use in RolloverImageLink is the capability to render a
script function that swaps the images for rollover effect at the top of the HTML document.
The server control code uses the ClientScriptManager method RegisterClientScriptBlock in
conjunction with the constant string named SWAP_SCRIPT, as shown in the following code. This
string constant uses the ???at??? symbol (@) to enable verbatim strings that allow for easy formatting
and maintenance inside the server control. Before we register the client script, we check to see
if the script block has already been registered by another instance of the control on the page with
a call to IsClientScriptBlockRegistered. Note that if we were to call RegisterClientScript
twice, the content of the second invocation would replace the script generated by the first call.
protected const string SWAP_SCRIPT = @"
function __Image_Swap(sName, sSrc)
{
document.images[sName].src = sSrc;
}
";
RegisterClientScriptBlock takes four parameters: a type definition that scopes the script
registration on a Page, a unique ID that identifies the script block for that Type, the string value
of the script to be emitted, and a Boolean value that tells the control system to include enclosing