Search code examples
c#asp.netclientscript

Non-invocable member 'System.Web.UI.Page.ClientScript' cannot be used like a method


Non-invocable member 'System.Web.UI.Page.ClientScript' cannot be used as a method.


I am trying to Freeze a button upon click so the user won't double-click accidentally causing duplicates entity. Here is my code:

      private void FreezeButton()
    {
        var sb = new StringBuilder();
        sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");

        sb.Append("var oldPage_IsValid = Page_IsValid; var oldPage_BlockSubmit = Page_BlockSubmit;");
        sb.Append("if (Page_ClientValidate('" + btnAdd.ValidationGroup + "') == false) {");
        sb.Append(" Page_IsValid = oldPage_IsValid; Page_BlockSubmit = oldPage_BlockSubmit; return false; }} ");

        sb.Append("this.value = 'Processing...';");
        sb.Append("this.disabled = true;");

        sb.Append(Page.ClientScript(btnAdd, null) + ";");
        sb.Append("return true;");

        string submitButton = sb.ToString();

        btnAdd.Attributes.Add("onclick", submitButton);
    }

First I tried to use the Client without the Page.ClientScript and it gave me Error for: The name 'ClientScript' does not exist in the current context

Then I looked at This question and found out that you could use it like Page.ClientScript but now I am getting the Error Non-invocable member


Solution

  • I found the solution:

      private void FreezeButton()
        {
            var sb = new StringBuilder();
            sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
    
            sb.Append("var oldPage_IsValid = Page_IsValid; var oldPage_BlockSubmit = Page_BlockSubmit;");
            sb.Append("if (Page_ClientValidate('" + btnAdd.ValidationGroup + "') == false) {");
            sb.Append(" Page_IsValid = oldPage_IsValid; Page_BlockSubmit = oldPage_BlockSubmit; return false; }} ");
    
            sb.Append("this.value = 'Processing...';");
            sb.Append("this.disabled = true;");
    
            sb.Append(Page.ClientScript.GetPostBackEventReference(btnAdd, null) + ";");
            sb.Append("return true;");
    
            string submitButtonOnclickJs = sb.ToString();
    
            btnAddReceipt.Attributes.Add("onclick", submitButtonOnclickJs);
        }