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

484 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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

Page.ClientScript is a property, you cannot use it like a method. Probably you're looking for Page.ClientScript.RegisterClientScriptBlock() to include script content into page body, which explained by another answer in the referenced question link.

Assumed you're handling btnAdd which is a server control with FreezeButton method event handling, you should replace the content to contain RegisterClientScriptBlock and JS function name to call client-side validation like example below:

protected void FreezeButton(object sender, EventArgs e)
{
    var sb = new StringBuilder();
    sb.Append("function validate() { ")

    // script content here, skipped for brevity

    sb.Append("}");

    // use RegisterClientScriptBlock to attach script content into <script> tag inside page body
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Example", sb.ToString(), true);

    // handle client-side event click if the button is a server control
    btnAdd.OnClientClick = "validate()";
}

Then, handle server-side click event of btnAdd like this:

<asp:Button ID="btnAdd" runat="server" OnClick="FreezeButton" ... />