RegisterStartupScript似乎在更新面板中的页面回发上不起作用 [英] RegisterStartupScript doesn't appear to be working on page postback within update panel

查看:88
本文介绍了RegisterStartupScript似乎在更新面板中的页面回发上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的-所以正在使用自定义datepicker控件的系统上工作(我知道那里还有其他控件..但是为了保持一致性,我想了解为什么我当前的问题正在发生并予以解决).

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).

因此,它是带有文本框并在Page_PreRender上的自定义用户控件,它可以执行以下操作:

So its a custom user control with a textbox and on Page_PreRender does this:

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.

有什么建议吗?

谢谢!

推荐答案

您是否尝试了ScriptManager而不是Page.ClientScript?我查看了之前使用UpdatePanels和jQuery编写的一些代码,并使用了ScriptManager.我认为,如果未向ScriptManager注册,则脚本在部分回发期间将不可用.

Have you tried ScriptManager instead of Page.ClientScript? I looked at some code that I'd written before with UpdatePanels and jQuery and I'd used ScriptManager. I think that the script is not available during a partial postback if it is not registered with the ScriptManager.

看起来像这样:

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

这篇关于RegisterStartupScript似乎在更新面板中的页面回发上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆