如何在部分回发期间检查客户端脚本是否已注册

Ism*_*ilS 13 asp.net asp.net-ajax ajaxcontroltoolkit .net-3.5

以下是我目前实施的代码.

if (!Page.ClientScript.IsStartupScriptRegistered(Page.GetType(), scriptKey))
{
  ScriptManager scriptManager = ScriptManager.GetCurrent(page);
  if (scriptManager != null && scriptManager.IsInAsyncPostBack)
  {
    //if a MS AJAX request, use the Scriptmanager class
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), scriptKey, script, true);
  }
  else
  {
    //if a standard postback, use the standard ClientScript method
    Page.ClientScript.RegisterStartupScript(Page.GetType(), scriptKey, script, true);
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在按照这个答案中的建议进行操作,以便我可以在两个时间注册启动脚本,即有部分回发和完整回发.

问题Page.ClientScript.IsStartupScriptRegistered(Page.GetType(), scriptKey)始终是(即使以前注册过脚本),当它是部分回发时返回false.我找不到ScriptManager.IsStartupScriptRegistered(静态)方法.因此,所有部分/异步回发都会发出附加脚本.

请注意,我正在使用AjaxControlToolkit版本4.1的脚本管理器,即ToolkitScriptManager在我的母版页中.但我不认为这与此有关.

UPDATE

  <asp:UpdatePanel ID="ContactDetailsUpdatePanel" UpdateMode="Conditional" runat="server">
    <Triggers>
      <asp:AsyncPostBackTrigger ControlID="UpdateContactDetailsButton" EventName="Click" />
    </Triggers>
    <ContentTemplate>
      <div id="ContactDetailsContent" class="contact_details_content">
        <div class="customer_contactdetails_left_pane">
          <div class="customer_name_field">
            <asp:Label ID="CustomerNameLabel" runat="server" Text="Customer" />
            <asp:TextBox ID="CustomerNameValue" runat="server" />
          </div>
          <div class="customer_address_field">
            <asp:Label ID="CustomerAddressLabel" runat="server" Text="Address" />
            <asp:TextBox ID="CustomerAddressValue" runat="server" />
            <asp:TextBox ID="CustomerAddressValue1" runat="server" />
            <asp:TextBox ID="CustomerAddressValue2" runat="server" />
            <asp:TextBox ID="CustomerAddressValue3" runat="server" />
          </div>
          <div class="customer_postcode_field">
            <asp:Label ID="CustomerPostcodeLabel" runat="server" Text="Postcode" />
            <asp:TextBox ID="CustomerPostcodeValue" runat="server" />
          </div>
        </div>
        <div class="customer_contactdetails_right_pane">
          <div>
            <asp:Label ID="CustomerContactLabel" runat="server" Text="Contact" />
            <asp:TextBox ID="CustomerContactValue" runat="server" />
          </div>
          <div>
            <asp:Label ID="CustomerTelephoneLabel" runat="server" Text="Telephone" />
            <asp:TextBox ID="CustomerTelephoneValue" runat="server" />
          </div>
          <div>
            <asp:Label ID="CustomerMobileLabel" runat="server" Text="Mobile" />
            <asp:TextBox ID="CustomerMobileValue" runat="server" />
          </div>
          <div>
            <asp:Label ID="CustomerFaxLabel" runat="server" Text="Fax" />
            <asp:TextBox ID="CustomerFaxValue" runat="server" />
          </div>
          <div>
            <asp:Label ID="CustomerEmailLabel" runat="server" Text="Email" />
            <asp:TextBox ID="CustomerEmailValue" runat="server" />
          </div>
          <div>
            <asp:Label ID="CustomerWebLabel" runat="server" Text="Web" />
            <asp:TextBox ID="CustomerWebValue" runat="server" />
          </div>
        </div>
      </div>
      <div class="update_button_field">
        <asp:Button ID="UpdateContactDetailsButton" runat="server" Text="Update" 
          onclick="UpdateContactDetailsButton_Click" />
      </div>          
    </ContentTemplate>    
  </asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

提前致谢.

注意:为了能够理解此问题的进展,请在回复之前查看对此答案的评论.

更新
我已经实现了一个临时的解决方案,通过检查javascript,如果脚本已经执行,那么不执行两次.Javascript在每次部分回发时仍会多次出错.无法阻止它.

随着对这篇文章的看法越来越多,我可以看到还有其他人可能也想要回答这个问题.

小智 10

如果你使用这个;

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "noPasswordMatch", script, true);
Run Code Online (Sandbox Code Playgroud)

然后检查它是否已经注册,你必须使用这个:

if (Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "noPasswordMatch"))
Run Code Online (Sandbox Code Playgroud)

if (Page.ClientScript.IsClientScriptBlockRegistered("noPasswordMatch")) 不起作用!