Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

stringbuilder with javascript asp.net 2.0

Posted on 2006-05-02
1
Medium Priority
?
1,562 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:fwsteal
1 Comment
 
LVL 3

Accepted Solution

by:
rundkaas earned 2000 total points
ID: 16590152
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question