简体   繁体   English

RegisterStartupScript似乎不在更新面板中的页面回发上工作

[英]RegisterStartupScript doesn't appear to be working on page postback within update panel

OK - so am working on a system that uses a custom datepicker control (I know there are other ones out there.. but for consistency would like to understand why my current issue is happening and fix it). 好的 - 所以我正在使用一个使用自定义日期选择器控件的系统(我知道还有其他的...但为了一致性,我想了解为什么我当前的问题正在发生并修复它)。

So its a custom user control with a textbox and on Page_PreRender does this: 因此,带有文本框和Page_PreRender的自定义用户控件执行此操作:

protected void Page_PreRender(object sender, EventArgs e)
    {

        string clientScript = @"
        $(function(){
        $('#" + this.Date1.ClientID + @"').datepicker({dateFormat: 'dd/mm/yy', constrainInput: true});
        });";
        Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, clientScript, true);

        //Type t = this.GetType();
        //if (!Page.ClientScript.IsStartupScriptRegistered(t, this.ClientID))
        //{
        //    Page.ClientScript.RegisterStartupScript(t, this.ClientID, clientScript, true);
        //}
    }

Ignore commented out stuff - that was me trying something different - didn't help. 忽略注释掉的东西 - 那是我尝试不同的东西 - 没有帮助。

My issue is that this all works fine when I load the page. 我的问题是,当我加载页面时,这一切都正常。 But if I select something from a dropdownlist causing a page postback - when I click into my date fields they stop working. 但是,如果我从下拉列表中选择一些导致页面回发的内容 - 当我点击我的日期字段时,它们就会停止工作。 As in I should be able to click into the textbox and a nice calendar control appears. 就像我应该能够点击进入文本框一样,会出现一个漂亮的日历控件。 But after postback there is no nice calendar control appearing! 但在回发后,没有出现漂亮的日历控件!

It's currently all wrapped (in the hosting page) inside an update panel. 它目前全部包含在(在托管页面中)更新面板中。 So I comment out the update panel stuff and the dates are working after page postback. 所以我评论了更新面板的东西,日期在页面回发后工作。 So it appears to be something related to that update panel. 所以它似乎与该更新面板有关。

Any suggestions please? 有什么建议吗?

Thanks!! 谢谢!!

Have you tried ScriptManager instead of Page.ClientScript? 您是否尝试过ScriptManager而不是Page.ClientScript? I looked at some code that I'd written before with UpdatePanels and jQuery and I'd used ScriptManager. 我查看了之前使用UpdatePanels和jQuery编写的一些代码,并且我使用了ScriptManager。 I think that the script is not available during a partial postback if it is not registered with the ScriptManager. 我认为如果脚本未在ScriptManager中注册,则在部分回发期间该脚本不可用。

Looks like this: 看起来像这样:

ScriptManager.RegisterStartupScript(Page, this.GetType(), "DatePickerScript", clientScript, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM