Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 20th, 2004, 07:53 AM
Authorized User
 
Join Date: Jun 2003
Location: Beograd, , Yugoslavia.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default change TextBox.BackColor property onFocus event

Hi,

I want to change TextBox1.BackColor property
when it gets focus.How can I do this, think
JavaScript is only way, but...
  #2 (permalink)  
Old March 20th, 2004, 08:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I think for focus, yes. You would have to specify autopostback=true, and the page would refresh every time you clicked in the textbox and click out (would be two refreshes if you click out of one textbox and into another). I would think Javascript, JScript, or VBScript is your best bet.
  #3 (permalink)  
Old March 21st, 2004, 08:06 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Use the javascript triggered by the onFocus and onBlur attributes within the textbox html. The javascript you need to use to actually change the color is something like this:

//To set it
textbox.style.backgroundColor = "#FF0000";

//To "clear" it. If the BGcolor is set by some CSS style,
//clearing it will make it revert back to the CSS defined appearance.
textbox.style.backgroundColor = "";

Take a look at the CSS reference at the MSDN site. This is one of the most complete CSS/DHTML references I have found:
http://msdn.microsoft.com/library/de..._ref_entry.asp

You can set the onFocus and onBlur properties directly in the HTML markup for the textbox control, or you can do it thru the Attributes collection of the control in the code. All client side events must be accessed in this way because there are no properties on the server controls for these events. Unfortunately it seems that Microsoft didn't think that any of us actually would still want to do client-side events once we started using .NET.

Peter
------------------------------------------------------
Work smarter, not harder.
  #4 (permalink)  
Old March 23rd, 2004, 09:40 AM
Authorized User
 
Join Date: Jun 2003
Location: Beograd, , Yugoslavia.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know anything about JavaScript but tried:
1)

<form id="JSTest" method="post" runat="server">
...
<SPAN onFocus="this.style.backgroundColor='red'">
  <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</SPAN></form>

and nothing happend

2)
<asp:TextBox id="TextBox1" onFocus = "TextBox1.style.backgroundColor='red'" runat="server"></asp:TextBox>
When I click on TextBox1 get error message: TextBox1 is undefined
Can you give me some example?
  #5 (permalink)  
Old March 23rd, 2004, 12:02 PM
Authorized User
 
Join Date: Feb 2004
Location: Islamabad, , Pakistan.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First you always have to include the <%@ Import Namespace%= System.Drawing.Color %>
Then you can set the color of the object in ASP.Net.For genrating events you have to specify the (Sender as Object,E as EventArgs) in the subprocedure.
Here is code that will explain you:
  <%@ Import Namespace= "System.Drawing.Color" %>
<%@ Page Language="VB" runat="server" %>

<script language="vb" runat="server">
Sub ChangeColor(Sender as Object,E as EventArgs)
    txtName.BackColor = System.Drawing.Color.Cyan
End Sub

</script>

<html>
    <body>
        <form runat="server">

            <asp:TextBox id="txtName" runat="server"/></br>
            <asp:Button id="btn" OnClick=ChangeColor Text = "Change Color" runat="server"/>
        </form>
    </body>
</html>

Now you just need to know how to catch the focus event.I have tried OnFocus,OnEnter for the TextBox but it didn't work.
Hope this helps...

&nbsp;&nbsp;CEO InteliSoft

Maqsood ur Rahman
  #6 (permalink)  
Old March 23rd, 2004, 12:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

In the page load, use the Attributes property and the Add method to add an attribute to the text box. Add attributes for the onfocus and onblur methods, and set the this.style.backgroundColor = '<color>'.

Now the backgroundColor attribute; it's either backgroundColor, background-color, or bgcolor (I can't remember; it's been a while).

Hope this helps,

Brian
  #7 (permalink)  
Old March 23rd, 2004, 04:27 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

drasko,

You are on the right track with what you tried accept for a few small things:

A) A span is not a "focus-able" object like a textbox or button. You can however use the onMouseOver type events.

B) The code you used in the span's onFocus would work for the textbox:
<asp:TextBox id="TextBox1" onFocus = "this.style.backgroundColor='red'" runat="server"></asp:TextBox>

Once this control is translated to HTML by the framework, the onFocus event will work just fine using "this". You can always call "this" from a javascript event handler within the object you need to manipulate.

Using "TextBox1" as the object name *should* have worked but when you deal with .net controls, using the server names in client side code doesn't always work because of the way .net names controls in the resulting HTML. It's safest to use "this" when you are running javascript within the object itself. If you have to call a function from a control that needs to manipulate that control you can always pass "this" to the control and still not have to worry about knowing the exactl name that .net generates for the control.

(BTW- You don't need any of the .net stuff maxood suggested because all of what you are doing is client-side.)

When calling the styles in javascript, they usually follow this form: "backgroundColor". When setting the same style in CSS it's "background-color". When the style is in the HTML tag attribute, well, there doesn't seem to be a consistency with those. You just have to have a good HTML reference with you! :)

Peter
------------------------------------------------------
Work smarter, not harder.
  #8 (permalink)  
Old March 24th, 2004, 11:00 AM
Authorized User
 
Join Date: Jun 2003
Location: Beograd, , Yugoslavia.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter you are right, it works

Thank you
  #9 (permalink)  
Old January 26th, 2009, 12:23 AM
Registered User
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi i am new to java script i am having a textbox it has some characters eg:google search by using the java script i want to change the color in onfocus using style sheet
but wen i type some characters it should not get the the color which i get from script

please help me out
anysuggestions will be thankfull
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
changing the backcolor property of the textbox Fehrer Access VBA 9 April 19th, 2016 11:59 AM
Change Textbox Backcolor for certain records only camehere Access VBA 5 April 28th, 2008 07:28 AM
Changing the BackColor property of a Form Qman69 Visual Basic 2005 Basics 0 March 14th, 2007 05:43 PM
IE not performing onFocus event for <select> kapstan Javascript 4 January 24th, 2006 02:10 PM
ComboBox BackColor change LoorD C# 4 October 6th, 2005 01:44 AM



All times are GMT -4. The time now is 03:38 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.