text box label backcolor change on the fly

Hello,
i have a form in which there are labels and text boxes.
Now wha I am tring to do is when there is focus on a text box(user starts to enter text) back color of text box and label should be changed?
how do we that any code?
Asp.net VS2005
Sanjay
dontnetsanjayAsked:
Who is Participating?
 
udhayakumardConnect With a Mentor Commented:
Just add this in ur page load

txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );
  txtEmail.Attributes.Add( "onBlur", "this.style.backgroundColor='white';" );
0
 
hongjunCommented:
In the onfocus event, try this

TextBox1.BackColor = System.Drawing.Color.Red;
0
 
udhayakumardCommented:
        private void textBox1_Enter(object sender, EventArgs e)  // Change Color
        {
            textBox1.BackColor = Color.Yellow;
            label1.BackColor = Color.Yellow;
        }

        private void textBox1_Leave(object sender, EventArgs e) // Back to normal
        {
            textBox1.BackColor = Color.Transparent;
            label1.BackColor = Color.Transparent;
        }
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
hongjunCommented:
It should have been "Enter" event.
0
 
dontnetsanjayAuthor Commented:
Hello again,
in my aspx file when i click on text box and then press F4 propeties of that textbox are displayed
and when i switch to event part it does not display any enter or leave event
i typed this code in my aspx.cs file but nothing is happening
thanks
0
 
udhayakumardCommented:
U need to add attributes for the text box

/Server Code
txtName.Attributes.Add("onfocus", "textBox1_Enter();")

txtName.Attributes.Add("onblur", "textBox1_Leave();")

//Client Side Code
function SomeJavaScriptFunction(){
//Hi it's txtName's lost focus calling
//Hi txtName :)
}
0
 
dontnetsanjayAuthor Commented:
protected void Page_Load(object sender, EventArgs e)
    {
        TxtCompany.Attributes.Add("onfocus", "txtCompany_Enter();");
         txtCompany.Attributes.Add("onblur", "txtCompany_Leave();");
    }


    private void txtCompany_Enter(object sender, EventArgs e)  
    {
        TxtCompany.BackColor = Color.Yellow;
        lblmsg.BackColor = Color.Yellow;
    }


these lines are givin error when we click on the text box
0
 
udhayakumardCommented:
k forget that sanjay try this

txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );
  txtEmail.Attributes.Add( "onBlur", "this.style.backgroundColor='white';" );
0
 
udhayakumardCommented:
protected void Page_Load(object sender, EventArgs e)
    {
        TxtCompany.Attributes.Add("onfocus", "txtCompany_Enter()");
         txtCompany.Attributes.Add("onblur", "txtCompany_Leave()");
    }


    private void txtCompany_Enter()  
    {
        TxtCompany.BackColor = Color.Yellow;
        lblmsg.BackColor = Color.Yellow;
    }

        private void textBox1_Leave() // Back to normal
        {
            textBox1.BackColor = Color.Transparent;
            label1.BackColor = Color.Transparent;
        }
0
 
dontnetsanjayAuthor Commented:
txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );
okay this works


but when we call function  there is till error in the code
error is: object expected
0
 
udhayakumardCommented:
I dnt think there is a problem in this... May be u check someother part of the code. Else comment this and check...
0
 
dontnetsanjayAuthor Commented:
txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );

this part is working fine if we only need to change color of the text box and when we do not want to change color of the label
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.