Solved

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

Posted on 2008-06-25
6
876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 11

Expert Comment

by:Anurag Agarwal
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
 

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Anurag Agarwal
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:Anurag Agarwal
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:
Anurag Agarwal 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

726 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