【问题标题】:Custom Server Control trigger Page Load JS file自定义服务器控件触发页面加载 JS 文件
【发布时间】:2016-01-23 14:06:12
【问题描述】:

Visual Studio 2013,C# 代码。

要求明确定义为使用继承自 WebControl 基类的自定义服务器控件。

  [ToolboxData("<{0}:SampleTemplate runat=server></{0}:SampleTemplate>")]
        public class SampleTemplate : WebControl, INamingContainer
        {
            Private Button btnCustomer;
            Private Label lblCountry;
            private const string _JAVASCRIPT_URL = "/Scripts/ServerControlUtility/Customer.js";

已在CreateChildControls 事件中初始化按钮和标签控件。

protected override void CreateChildControls()
        {
            Controls.Clear();
            lblCountry = new Label();
            lblCountry.ID = "Country";

            btnCustomer = new Button();
            btnCustomer.ID = "btnLookUpAddress";
            btnCustomer.Text = "Look Up Address";
            Controls.Add(btnCustomer);
        }

已使用自定义服务器控件中的 PageScriptManager 注册了 JS 文件。与此控件相关的 CustomerJS 文件,负责处理客户端功能。

使用 Render/OnLoad 事件注册脚本。

protected override void Render(HtmlTextWriter writer)
        {
            var p = (Page)System.Web.HttpContext.Current.Handler;
            ScriptManager.RegisterClientScriptInclude(p, p.GetType(), "CustomerExtendedScript", _JAVASCRIPT_URL);

        }

问题是:Customer.JS 文件在主页面完全渲染之前被调用但是希望它在实际使用自定义控件的页面完全加载时被触发。就像 Jquery 中的 document.ready 函数一样。

要点: 1.服务器控制应该是独立的实体所以不能使用Jquery 插件(或任何其他)
2.Customer.Js文件是纯Javascript文件
3.无需在主页面再次添加Customer.JS文件引用 原因开始引用已经存在于自定义服务器控件中

主要问题: 1.如何调用Customer.JS文件,使其在主页完全加载后加载。 2. Customer.JS 文件只能在自定义服务器控件中引用,其他地方不要再引用。

【问题讨论】:

  • 检查我的答案,如果有帮助,请告诉我。

标签: c# asp.net c#-4.0 custom-controls


【解决方案1】:

使用

ClientScript.RegisterStartupScript

这将在&lt;/body&gt;标签之后加载javascript,所以页面应该已经加载了。

【讨论】:

猜你喜欢
  • 2010-11-04
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2011-01-23
  • 2012-03-26
相关资源
最近更新 更多