stringbuilder with javascript asp.net 2.0

How do I get the <%= blocks to render properly?

protected void Page_Load(object sender, EventArgs e)
 {
  StringBuilder javaScriptString = new StringBuilder();
   javaScriptString.Append("<script type='text/javascript'>");
   javaScriptString.Append("function validateCheckbox() {");
   javaScriptString.Append("if(!document.form1.<%= CheckBoxCA.ClientID %>.checked){");
   javaScriptString.Append("alert('The CA is required.');");
   javaScriptString.Append("return false;");
   javaScriptString.Append("}");
   javaScriptString.Append("if(!document.form1.<%= CheckBoxESA.ClientID %>.checked){");
   javaScriptString.Append("alert('The ESA is required.');");
   javaScriptString.Append("return false;");
   javaScriptString.Append("}");
   javaScriptString.Append("return true;");
   javaScriptString.Append("}");
   javaScriptString.Append("</script>");
 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", javaScriptString);
 form1.Attributes.Add("OnClientClick", "return validateCheckbox();");
}


Error: <%= CheckBoxCA.ClientID %> and <%= CheckBoxESA.ClientID %> are being rendered such and not like
the following:
<script type="text/javascript">
function validateCheckbox() {
 if(!document.form1.CheckBoxCA.checked){
    alert("The CA is required.");
   return false;
   }
 if(!document.form1.CheckBoxES.checked){
    alert("The ESA is required.");
   return false;
  }
   return true;  
 }
</script>

When the two checkboxes are redendered as <%= CheckBoxCA.ClientID %> and <%= CheckBoxESA.ClientID %> I
get an error.
fwstealAsked:
Who is Participating?
 
rundkaasCommented:
I don't think it is possible as the <%= is a asp tag
could you not do something like this:
protected void Page_Load(object sender, EventArgs e)
 {
  StringBuilder javaScriptString = new StringBuilder();
   javaScriptString.Append("<script type='text/javascript'>");
   javaScriptString.Append("function validateCheckbox() {");
   javaScriptString.Append("if(!document.form1."+ CheckBoxCA.ClientID+".checked){");
   javaScriptString.Append("alert('The CA is required.');");
   javaScriptString.Append("return false;");
   javaScriptString.Append("}");
   javaScriptString.Append("if(!document.form1."+ CheckBoxCA.ClientID+".checked){");
   javaScriptString.Append("alert('The ESA is required.');");
   javaScriptString.Append("return false;");
   javaScriptString.Append("}");
   javaScriptString.Append("return true;");
   javaScriptString.Append("}");
   javaScriptString.Append("</script>");
 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", javaScriptString);
 form1.Attributes.Add("OnClientClick", "return validateCheckbox();");
}
That is you rather concatinate the string instead of using the tag.
The <%= equals the same as Response.Writer, and while that may make sense inline, it is not possible in code behind as far as I know.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.