0

I am trying to convert the following JavaScript into the code behind (C#) of an ASP.NET page. This is the JavaScript:

 function DisableButton() {
        document.getElementById("<%=Submit.ClientID %>").disabled = true;
    }
   window.onbeforeunload = DisableButton;

I am trying to build a Client Script as follows (but get error message, see after the code):

StringBuilder cstext = new StringBuilder();

cstext.Append("<script type=\"text/javascript\"> function DisableButton() {");
// I get error on the following line that semi-colon is missing
cstext.Append("document.getElementById("<%=Submit.ClientID %>").disabled = true; }";
cstext.Append("window.onbeforeunload = DisableButton;");
cstext.Append("</script>");

I get error that the semi-colon is missing. What do I need to change in my code?

1 Answer 1

1

The line

cstext.Append("document.getElementById("<%=Submit.ClientID %>").disabled = true; }";

should be

cstext.Append("document.getElementById('" + Submit.ClientID + "').disabled = true; }");
Sign up to request clarification or add additional context in comments.

2 Comments

It would be nice to understand why changing the syntax from ClientID inside the percent sign and brackets to a sting works. I just don't get it; but it works.
Percent sign is used when writing in the page code to indicate that some C# code should be executed when rendering the page. See msdn.microsoft.com/en-us/library/vstudio/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.