Link to home
Start Free TrialLog in
Avatar of dontnetsanjay
dontnetsanjay

asked on

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
Avatar of hongjun
hongjun
Flag of Singapore image

In the onfocus event, try this

TextBox1.BackColor = System.Drawing.Color.Red;
        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;
        }
It should have been "Enter" event.
Avatar of dontnetsanjay
dontnetsanjay

ASKER

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
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 :)
}
ASKER CERTIFIED SOLUTION
Avatar of udhayakumard
udhayakumard
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
k forget that sanjay try this

txtEmail.Attributes.Add( "onFocus", "this.style.backgroundColor='aliceblue';" );
  txtEmail.Attributes.Add( "onBlur", "this.style.backgroundColor='white';" );
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;
        }
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
I dnt think there is a problem in this... May be u check someother part of the code. Else comment this and check...
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