【问题标题】:why my js is not loaded on postback?为什么我的 js 没有在回发时加载?
【发布时间】:2011-11-02 19:30:59
【问题描述】:

我有一个通过一些 js 脚本增强的下拉列表。这个 DropDownList 位于 UpdatePanel 中,如下所示:

 <asp:UpdatePanel ID="upStoreDetails" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:DropDownList ID="dropRestaurants" runat="server" CssClass="styleCB" AutoPostBack="true" OnSelectedIndexChanged="ddlTenant_SelectedIndexChanged">
        </asp:DropDownList>
        <uc:popuptenantdescription id="popupTenantDescriptionControl" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

我已经为它注册了脚本:

protected void Page_PreRender(object sender, EventArgs e)
{
    loadJs();
}

private void loadJs()
{
    Type cstype = this.GetType();
    String scriptName = "cufon-yui";

   if (!Page.ClientScript.IsClientScriptIncludeRegistered(cstype, scriptName))
    {
        Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), scriptName, VirtualPathUtility.ToAbsolute("~/js/cufon-yui.js"));
   }
}

第一次加载页面时插入成功。

但是,在回发时(asp:dropDownList 的 selectedIndexChanged 事件),应用于下拉列表的样式丢失了...您知道原因吗?

谢谢!

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    我的解决方案:

    • 基本上,src 文件包含一些在文档就绪时触发的 jQuery 方法。

    但是仅仅注册这个脚本(就像我上面所做的那样)并没有调用这些方法,而是只包含了那个 js 文件。

    所以我不得不手动调用那些当 doc.准备好和 IsPostBack,就我而言:

    private void loadJs()
    {
        Type cstype = this.GetType();
        String csname = "applyStyleToDropDownList";
        if (!Page.ClientScript.IsClientScriptBlockRegistered(cstype, csname))
        {
            StringBuilder cstext2 = new StringBuilder();
            cstext2.Append("$(function(){ $('.styleCB').customStyle();})");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), csname, cstext2.ToString(), true);
        }
    }
    

    也许它对其他人也有用。

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 2020-10-11
      • 2021-11-27
      • 2020-04-01
      • 2015-12-24
      • 2012-01-09
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      相关资源
      最近更新 更多