Solved

change the background color of textbox inside a datagrid in asp.net 1.1

Posted on 2008-06-25
6
865 Views
Last Modified: 2008-06-26
Hi,

  I have a datagrid, which has a textbox inside the itemtemplate column.
  what i want is, once the user types in a value lesser than 25, i shud change the
background of the the textbox to red immediatly.
If the entered value is changed to a value more than 25, then remove the background color automatically.

how to do this???

Very Urgent ....Thanks
0
Comment
Question by:srk1982
  • 4
6 Comments
 
LVL 11

Expert Comment

by:anuragal
ID: 21871747
Add following code in the ItemDataBound of the Datagrid...

TextBox tx = (TextBox)(e.Item.FindControl("TextBox1"));
tx.Attributes.Add("onKeyPress", "ChangeColor(" + tx.ClientId +  ");");

And in .aspx page add this javascript function

function ChangeColor(obj)
{
      var txtbox = document.getElementById(obj);
            if(txtbox.value < 25)
            {
                  txtbox.backGroundColor = "red";
            }
                                else
                                {
                                              txtbox.backGroundColor = "set default color";
                                }
}


If it doesn't work then please follow the below link to get an idea of various ways to access the textbox

http://forums.asp.net/p/1194696/2077518.aspx

Anurag

0
 
LVL 11

Expert Comment

by:Vivek Thangaswamy
ID: 21871756
0
 

Author Comment

by:srk1982
ID: 21871818
hi anurag,

  some problem in

var txtbox = document.getElementById(obj);

i could not get the txtbox here.

I used like this...

<asp:TextBox OnKeyPress="ChangeColor(this.value);" id=Txt_EML Runat="server">
________________________________________________________
function ChangeColor(obj)
{
    var txtbox = document.getElementById(obj);

    if(obj < 25)
    {
            txtbox.backGroundColor = "red";
    }
      else
      {
                  txtbox.backGroundColor = "White";
    }
}

____________________________________________________________

Here when i put the alert statement, like
alert(obj) i am getting the value.
but when i put
alert(txt.value), i am getting error - object required.

i think some problem in
    var txtbox = document.getElementById(obj);
it is not getting the getelementbyid.

Urgent...thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:anuragal
ID: 21872175
as i said put the code in itemdatabound for attaching the javascript function to the textbox.

Anurag
0
 
LVL 11

Expert Comment

by:anuragal
ID: 21872181
I have not seen completely what you wrote in yur mail...

try
<asp:TextBox OnKeyPress="ChangeColor(this);" id=Txt_EML Runat="server">

Anurag
0
 
LVL 11

Accepted Solution

by:
anuragal earned 500 total points
ID: 21872248
For setting the color you have to do like this

var eC='#E5E5E5'; //error color
var dC='#FFFFFF'; //default color

then set accordingly, keep in mind that javascript is case sensitive..

use TextBox1.style.backgroundColor=eC or TextBox1.style.backgroundColor=dC on conditions

Anurag


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now