title: Use of RegisterClientScriptBlock/RegisterStartupScript in asp.net 3.5
body: <p>I'm not able to understand what is the use of it.RegisterClientScriptBlock/RegisterStartupScript.</p>
  
  <p>When we can directly write the JavaScript code in.js file then call it on the button</p>
  
  <p>ex: 2</p>
  
  <pre><code>&lt;script&gt;
      function ReqField1Validator() { 
          if (document.forms[0].txtField1.value == '') {
              alert('TextBox cannot be empty') 
              return false
           } 
           return true 
       } 
  &lt;/script&gt;
  
  btnPostback.Attributes.Add("onclick","return ReqField1Validator()");
  </code></pre>
  
  <p>What will be the use of RegisterClientScriptBlock/RegisterStartupScript?</p>
  
  <pre><code>protected void btnPostback_Click(object sender, EventArgs e)  {
      System.Text.StringBuilder sb = new System.Text.StringBuilder();
      sb.Append(@"&lt;script language='javascript'&gt;");
      sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
      sb.Append(@"lbl.style.color='red';");
      sb.Append(@"&lt;/script&gt;");
  
      if (!ClientScript.IsStartupScriptRegistered("JSScript")){
          ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
      }
  }
  </code></pre>
  
  <p>Read few articles but they were not clear.</p>
  
  <p>Any thoughts on this would be great.</p>