How to render your control’s client script

本文介绍了三种渲染客户端脚本的方法:将脚本渲染为HTML属性值、将脚本渲染为脚本块以及将脚本打包成外部文件。具体包括如何在控件中注册JavaScript函数调用、如何在页面顶部或底部插入脚本块,以及如何引用外部脚本文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to render your control’s client script. there are three different ways to render
your client script:

  1.  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.
  2.  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.
  3.  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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值