Hidalgo
Hidalgo

Reputation: 941

Convert Javascript to Code Behind in ASP.NET

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?

Upvotes: 0

Views: 786

Answers (1)

Donatas Bacius
Donatas Bacius

Reputation: 656

The line

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

should be

cstext.Append("document.getElementById('" + Submit.ClientID + "').disabled = true; }");

Upvotes: 1

Related Questions