Facebook Twitter Gplus LinkedIn YouTube Google Maps RSS
Home Technical Change back ground color of Asp.net Textbox on validation failure
formats

Change back ground color of Asp.net Textbox on validation failure

Here is a way of changing back ground color for Textbox on validation failure. The key here is using Page_ClientValidate function in javascript.
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ErrorMessage=”Please enter a value”
ControlToValidate=”TextBox1″></asp:RequiredFieldValidator>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” OnClientClick=”btnClick();”
OnClick=”Button1_Click1″ />
<script type=”text/javascript”>
function btnClick()
{
if (!Page_ClientValidate())
{
document.getElementById(“TextBox1″).style.backgroundColor=”red”;
}
}

function ClearBackGround()
{
document.getElementById(“TextBox1″).style.backgroundColor=”";
}
</script>

In Code behind

TextBox1.Attributes.Add(“onkeypress”, “ClearBackGround()”);

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
1 Comment  comments 

One Response

  1. thanks for the great post

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

12,871 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© All rights reserved to Cyberbrutus. 2012
credit

Switch to our mobile site