How to render your control’s client script. there are three different ways to render
your client script:
- Render your script as HTML attribute values. This option is normally used to register a
call into a JavaScript function for a client-side event such as onmouseover,
onmouseout, and so on. If your control directly or indirectly derives from the
WebControl base class, it should override the AddAttributesToRender and/or
RenderContents methods to render its client script as HTML attribute values.
Override the AddAttributesToRender method if you need to render the script as HTML
attribute values on your control’s containing element. Override the RenderContents
method if you need to render the script as HTML attribute values on the child elements
of your control’s containing element.
If your control does not directly or indirectly derive from the WebControl base class, it
should override the Render method to render its script as HTML attribute values. - Render your script as script blocks, that is, within the opening and closing tags of
<script> elements. Override the OnPreRender method to call the
RegisterClientScriptBlock and/or RegisterStartupScript methods of the
ClientScript property of the containing page to render your script blocks.
Call the RegisterClientScriptBlock method to render the script block at the top of
the page if the HTML that your control renders needs to access the script blocks. Call
the RegisterStartupScript method to render the script blocks at the bottom of the
page if the script blocks need to access the HTML that your control renders. - Package your script into a set of external script files known as script library and
render references to these files. Override the OnPreRender method to call the
RegisterClientScriptInclude and/or RegisterClientScriptResource methods
of the ClientScript property of the containing page to render references to your
script files.
Call the RegisterClientScriptInclude method if the script files are not embedded
into your control’s assembly. These script files are normally deployed to a designated
directory in a shared location. This shared location is a directory named aspnet_client,
which is registered with IIS as virtual root aspnet_client.
Call the RegisterClientScriptResource method if the script files are embedded
into your control’s assembly. It is highly recommended that you embed your control’s
script files into your control’s assembly to avoid deployment and versioning issues.