如何检查是否客户端脚本是在一个局部回传已注册是在、局部、脚本、客户端

2023-09-07 04:21:03 作者:丫頭,別太拽!

下面是code我目前实现的。

Below is the code I've currently implemented.

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);
  }
}

我做的是在this的回答让我可以注册启动脚本上两次,即当有局部回传和一个完整的回发。

I'm doing as suggested in this answer so that I can register startup script on both times i.e. when there is partial postback and a full postback.

现在的问题是 Page.ClientScript.IsStartupScriptRegistered(Page.GetType(),scriptKey)总是(甚至当脚本之前注册)返回false时,它是局部回传。而且我找不到ScriptManager.IsStartupScriptRegistered(静态)方法。作为这样的结果,附加脚本被发射的所有部分/异步回发

The problem is Page.ClientScript.IsStartupScriptRegistered(Page.GetType(), scriptKey) always (even when the script is registered before) returns false when it is partial postback. And I couldn't find ScriptManager.IsStartupScriptRegistered (static) method. As a result of this, additional script is emitted on all partial/async postbacks.

请注意,我用的版本AjaxControlToolkit中4.1即 ToolkitScriptManager 脚本管理器在我的母版。但我不事它有事情做与此有关。

Please note that I'm using script manager of AjaxControlToolkit version 4.1 i.e. ToolkitScriptManager in my masterpage. But I don't thing it has something to do with this.

更新

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

在此先感谢。

Thanks in advance.

注意::要能够理解这个问题的进展情况,请参见this回答之前回答。

NOTE: To be able to understand the progress on this problem, please see the comments on this answer before replying.

更新 我已经通过将检查在JavaScript中,如果脚本已经执行的话不执行两次实施了临时的解决这个问题。 JavaScript是仍在吐尽多次对每一个局部回传。无法prevent吧。

UPDATE I have implemented a temporary solution to this problem by putting a check in javascript that if the script is already executing then do not execute twice. Javascript is still being spitted multiple times on every partial postback. Couldn't prevent it.

由于这个主题的意见都在增加,我可以看到有其他人谁可能也想回答这个问题。

As the views to this post are increasing, I can see that there are other people who might also want answer to this problem.

推荐答案

如果您使用的是这一点;

If you are using this;

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "noPasswordMatch", script, true);

然后检查它是否已被注册,你必须使用这样的:

Then to check if it has been registered you must use this:

if (Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "noPasswordMatch"))

如果(Page.ClientScript.IsClientScriptBlockRegistered(noPasswordMatch))不行!