簡體   English   中英

如何覆蓋Asp.Net UpdatePanel添加的(動態)Javascript函數?

[英]How do I overwrite a Javascript function added (dynamically) by Asp.Net UpdatePanel?

我遇到的問題是我只能想到一個Javascript范圍問題以及Microsoft Asp.Net客戶端框架。

由於在這個問題上陳述的原因,我需要覆蓋由Asp.Net的ScriptResource.axd提供並由其Validator服務器控件使用的javascript函數ValidatorConvert。

首先,我將介紹如何使代碼工作。 然后我將展示一個我無法使其工作的場景。

這是一個帶有驗證器控件的簡單Asp.Net WebForm:

<body>
<form id="form1" runat="server">
    <script type="text/javascript">
        function ValidatorConvert(op, dataType, val) {
            //>>Overwrite ValidatorConvert function.
            //>>Call to the original JS file will be below the form tag and above script tag
            return op.toString(); //<<Consider everything as valid (client side)
        }
    </script>
    <asp:ScriptManager runat="server"
        ID="Scriptmanager1" 
        allowcustomerrorsredirect="true" 
        asyncpostbackerrormessage="Operation cannot be executed."
        asyncpostbacktimeout="90"
        enablepartialrendering="true"
        enablescriptglobalization="true" 
        enablescriptlocalization="true" 
        supportspartialrendering="true" 
        scriptmode="Inherit"></asp:ScriptManager>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:CompareValidator
        ID="CompareValidator1"
        runat="server"
        ErrorMessage="Ops, not an integer"
        Operator="DataTypeCheck"
        Type="Integer"
        ControlToValidate="TextBox1"></asp:CompareValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>

現在,當asp.net呈現表單時,它會以某種方式檢測到頁面上有可見的驗證器控件。 因此,在表單下方和腳本標記正上方將向客戶端JS調用驗證器。 這個JS文件將有一個ValidatorConvert函數,它將被我的覆蓋。

現在,這是一個不起作用的場景。 這是一個略有不同的WebForm:

<body>
<form id="form1" runat="server">
    <asp:ScriptManager runat="server"
        ID="Scriptmanager1" 
        allowcustomerrorsredirect="true" 
        asyncpostbackerrormessage="Operation cannot be executed."
        asyncpostbacktimeout="90"
        enablepartialrendering="true"
        enablescriptglobalization="true" 
        enablescriptlocalization="true" 
        supportspartialrendering="true" 
        scriptmode="Inherit"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                <asp:View ID="View1" runat="server">
                    <asp:Button ID="ButtonShowInput" runat="server" Text="Show Input     Field" CausesValidation="false" OnClick="ButtonShowInput_Click" />
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:CompareValidator
                        ID="CompareValidator1"
                        runat="server"
                        ErrorMessage="Ops, not an integer"
                        Operator="DataTypeCheck"
                        Type="Integer"
                        ControlToValidate="TextBox1"></asp:CompareValidator>
                    <asp:Button ID="Button1" runat="server" Text="Button"   OnClick="Button1_Click" />
                </asp:View>
            </asp:MultiView>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>
</body>

現在我在UpdatePanel中有一個MultiView。 頁面加載是第一個只有一個按鈕時可見的視圖。 按下此按鈕時,將顯示帶驗證器控件的第二個視圖。 因為這是在UpdateUpdate中,所以這將使用AJAX完成。

現在,在呈現表單時,默認情況下驗證程序控件不可見。 因此,javascript文件(ScriptResource.axd)的鏈接根本不會放在頁面上!

但是當按下按鈕並且驗證器可見時,鏈接將以動態方式添加。

問題是,這個鏈接是由頭標記中的asp.net框架放置的。

即使我的函數仍然在原始函數之下分層次定義,我的函數也不會被調用。

我嘗試將我的功能放在不同的地方,包括頭標簽,但它似乎也沒有用。 似乎它被認為是最后定義的函數有效。

那么,如何在第二種情況下覆蓋該功能呢? 此外,是否有一個適用於這兩種情況的解決方案?

提前感謝您抽出寶貴時間。

感謝Yuriy的意見,我能夠提出一個解決方案。

基本上,我創建的代碼將在頁面首次加載時定義我的函數。 此外,我注冊了我的自定義函數,以在每次ajax請求后重新定義我的函數。 鑒於我將此代碼放在MasterPage上,我能夠使其在所有應用程序中運行。

這是代碼:

MasterPage.aspx是一個簡單的Html頁面,其ContentPlaceHolders和ScriptManager位於表單標簽的正下方。 另外,我必須在頁面頂部(head標簽)添加對“Utils.js”的引用。

MasterPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (!Page.ClientScript.IsStartupScriptRegistered("jsDefineFunction"))
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "jsDefineFunction", "defineValidationFunction();", true);

        if (!Page.ClientScript.IsStartupScriptRegistered("jsDefineEndRequestFunction"))
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "jsDefineEndRequestFunction", "Sys.WebForms.PageRequestManager.getInstance().add_endRequest(defineValidationFunction);", true);
    }
}

Utils.js

function defineValidationFunction() {
    var ValidatorConvert = function (op, dataType, val) {
            //>>Consider everything as valid (client side)
            return op.toString();
        }
}

有了這個,我的代碼適用於使用MasterPage的所有頁面。 代碼將不斷覆蓋我的驗證功能。 有一點開銷,但我無法想出另一種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM