By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

TextBox Color Changes using Javascript

P: 12
Hello,
I know this question has been asked everywhere but I cannot find a solution that will help me. I hope someone here has some ideas.

I have 9 TextBoxes in a Repeater Control. A user can edit dates in these textboxes. If the user changes the date, I want to dynamically change the color of the background of the textbox. When the data is saved I will set the textboxes back to it's original background(I can do this in the code-behind)
Right now I can get it to change but I have to do a postback in order to do it on the TextChanged Event of the TextBox. This page has 14 rows with 9 textboxes in each so postbacks are tedious to the user after 3 of them.
Can anyone help me accomplish this in Javascript?

Thanks in advance!
Oct 2 '07 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
Use the text box's onchange event to change the color.
Oct 2 '07 #2

P: 12
Use the text box's onchange event to change the color.
I have tried using the onTextChanged Event but no avail unless the page posts back which I do not want it to do.

This is in my code-behind:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  foreach (RepeaterItem item in rptrShots.Items)
  3.  {
  4.       TextBox txt1 = (TextBox)item.FindControl("txt1");
  5.        txt1.Attributes["onTextChanged"] = "ChgColor('" + txt1.UniqueID + "');";
  6.  }
  7.  
This is in the mark-up:
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript"> 
  2. function ChgColor(sender)
  3. {
  4.     sender.style.backgroundColor = '#CC3300';
  5. }
  6. </script>
  7.  
Oct 2 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Can you not use normal JavaScript like, e.g.
[HTML]<input type="text" onchange="chgColor(this);">[/HTML]
Oct 2 '07 #4

P: 12
Can you not use normal JavaScript like, e.g.
[HTML]<input type="text" onchange="chgColor(this);">[/HTML]
No I get an error when I try to do it that way.
I get these types of errors:
Invalid expression term ')

This is what the markup for the textbox looks like:
[HTML]<asp:TextBox ID="txt1" runat="server" Text = '<%# Bind("Dose1") %>' MaxLength = "10" Width = "90%" OnTextChanged= "chgColor();"/>[/HTML]
Oct 3 '07 #5

P: 12
No I get an error when I try to do it that way.
I get these types of errors:
Invalid expression term ')

This is what the markup for the textbox looks like:
[HTML]<asp:TextBox ID="txt1" runat="server" Text = '<%# Bind("Dose1") %>' MaxLength = "10" Width = "90%" OnTextChanged= "chgColor();"/>[/HTML]
Does anyone have any ideas on how to get this to work?
Oct 12 '07 #6

acoder
Expert Mod 15k+
P: 16,027
How does that turn out on the client-side?
Oct 13 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.