Wednesday, March 28, 2012

Changing Background Color Of Text box ?

Hello,

I need something as Follow..

I am having one Data Entry form. Here I have many controls (All type of controls like Textbox, Dropdownlis, Checkboxex esc...)

Now when user focus any text box then its Background color should change. Can we do it using ATLAS, xmlScript, Javascript...

one more problem is that I am having nearly 20 Text boxes on my page so which way I can get this functionality using less efforts...

Atlas is a really "heavy" way to do this. I'd just do it with Javascript and CSS.

<style type="text/css">

.normal

{

background-color:inherit;

}

.focus

{

background-color:yellow;

}

</style>

<script type="text/Javascript">

function changeTextbox(control){

var x = document.getElementByID(control);

if (x.classname == "normal"){

x.classname = "focus";

}

else {

x.classname = "normal")

}

}

</script>


Nice..

But the Parameter CONTROL in function... comes from where ?

Means from where we can pass it ?


You pass the control paramater when you call the function from the onFocus even of your textbox. It would be just as easy to capture the sender though, and get the calling object of the function, so it didnt need a paramater.
You don't need atlas or ajax at all, some javascript and dhtml will do:
foreach (Control ctrlin Form.Controls){if (ctrl.GetType() ==typeof(TextBox)) { ((TextBox)ctrl).Attributes.Add("onfocus","javascript:this.style.backgroundColor='red'"); ((TextBox)ctrl).Attributes.Add("onblur","javascript:this.style.backgroundColor='white'"); }}

No comments:

Post a Comment