title: How to avoid writing messy JavaScript in an ASP.NET code-behind?
body: <p>I'm questionning about what is the best practice to use Javascript with ASP.NET. </p>
  
  <p>I don't know if it's the best practice but I add the javascript client side event inside the codebehind. It is working correctly but is this the best practice?</p>
  
  <p>For example, I got a radio button control and I add the Javascript client side event in the Page_Init. The page init can be recalled multiple time so the Javascript will be rendered each time that the Page_It is called. </p>
  
  <p>Also, it is hard to debug a long Javascript string. How it can be more clean ... is there a way?</p>
  
  <p>Let see an example of a variable that contains Javascript :</p>
  
  <pre><code>scripts.Text += "&lt;script type='text/javascript'&gt;function ValidateDdl" + metachamp.ID +
  "(sender, args) {  if(" + txtReason.ClientID + ".GetText() != '' ||" +
  dynamicControl.ClientID +
  ".style.display == 'none' || HiddenFieldSaveError.Contains('" + metachamp.ID +
  "') ){" + dynamicControl.ClientID + ".className='';HiddenFieldError.Remove(" +
  metachamp.ID + ");" + errorImage.ClientID +
  ".SetClientVisible(false);args.IsValid = true;}else{var comboVal = document.getElementById('" +
  Image9.ClientID + "'.substring(0,'" + Image9.ClientID +
  "'.length - 6) + 'ddl').value ;if (comboVal != '0' ) {args.IsValid = true;HiddenFieldError.Remove(" +
  metachamp.ID + ");" + validImage.ClientID +
  ".SetClientVisible(false);HiddenField.Remove('Bypass-' + '" +
  metachamp.ID.ToString() + "');HiddenFieldSaveError.Remove(" + metachamp.ID +
  ");" + dynamicControl.ClientID + ".className='';" + errorImage.ClientID +
  ".SetClientVisible(false);}";
  </code></pre>