Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
All-Star
154927 Points
19867 Posts
Moderator
MVP
Mar 01, 2012 05:52 AM|LINK
You have unbalanced html tags in your if statement. Use the <text> tags to tidy that up and it should work:
<script type="text/javascript"> function notEmpty(){ var myTextField = document.getElementById('user_name'); alert("You entered: " + myTextField.value); } </script> <html> <body> @{ if (IsPost) { string user_name = Request["user_name"]; <text><p>You entered: <br /> User Name: @user_name<br /></text> } else { <form method="post" action=""> user name:<br /> <input type="text" name="user_name" value="" /><br /> <br /> <input type="submit" value='notEmpty' onclick="notEmpty()" onkeypress="notEmpty()" class="submit" /> </form> } } </body> </html>
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: using Javascript in cshtml
Mar 01, 2012 05:52 AM|LINK
You have unbalanced html tags in your if statement. Use the <text> tags to tidy that up and it should work:
<script type="text/javascript"> function notEmpty(){ var myTextField = document.getElementById('user_name'); alert("You entered: " + myTextField.value); } </script> <html> <body> @{ if (IsPost) { string user_name = Request["user_name"]; <text><p>You entered: <br /> User Name: @user_name<br /></text> } else { <form method="post" action=""> user name:<br /> <input type="text" name="user_name" value="" /><br /> <br /> <input type="submit" value='notEmpty' onclick="notEmpty()" onkeypress="notEmpty()" class="submit" /> </form> } } </body> </html>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter