sutorius
asked on
AJAX - textbox won't change background color
I have the following default.aspx and default.aspx.cs and I cannot figure out why the textbox won't change background colors. The debugger steps through all of the server side code without error but when I get back to the webpage there is no change in color.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Ajax C# Example</title>
<style type="text/css">
</style>
</head>
<body>
<form autocomplete="off" id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextB ox>
<asp:TextBox ID="TextBox2" runat="server" Visible=false></asp:TextBo x>
</div>
</form>
</body>
</html>
<script language="javascript">
var net=new Object();
net.READY_STATE_UNINITIALI ZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIV E=3;
net.READY_STATE_COMPLETE=4 ;
/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function (url,onloa d,onerror, method,par ams,conten tType){
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadXMLDoc(url,method ,params,co ntentType) ;
}
net.ContentLoader.prototyp e.loadXMLD oc=functio n(url,meth od,params, contentTyp e){
if (!method){
method="GET";
}
if (!contentType && method=="POST"){
contentType='application/x -www-form- urlencoded ';
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.X MLHTTP");
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechang e=function (){
net.ContentLoader.onReadyS tate.call( loader);
}
this.req.open(method,url,t rue);
if (contentType){
this.req.setRequestHeader( 'Content-T ype', contentType);
}
this.req.send(params);
}catch (err){
this.onerror.call(this);
}
}
}
net.ContentLoader.onReadyS tate=funct ion(){
var req=this.req;
var ready=req.readyState;
if (ready==net.READY_STATE_CO MPLETE){
var httpStatus=req.status;
if (httpStatus==200 || httpStatus==0){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
}
net.ContentLoader.prototyp e.defaultE rror=funct ion(){
alert("error fetching data!"
+"\n\nreadyState:"+this.re q.readySta te
+"\nstatus: "+this.req.status
+"\nheaders: "+this.req.getAllResponseH eaders());
}
function doIt(){
var strValue = document.getElementById("< %=TextBox1 .ClientID %>").value;
var url = 'default.aspx?id=' + strValue;
var strParams = strValue;
var loader1 = new
net.ContentLoader(url,Fill DropDown,n ull,
"POST",strParams);
}
function FillDropDown(){
//alert("returned");
}
</script>
++++++++++++++++++++++++++ ++++++++++ ++++++++++ ++++++++++ ++++++++++ ++++++++++ +++++++++
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("o nblur", "doIt()");
}
if (Request.QueryString["id"] == "Andy")
DoIt();
}
public void DoIt()
{
TextBox1.BackColor = ColorTranslator.FromHtml(" #f1f1f1");
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Ajax C# Example</title>
<style type="text/css">
</style>
</head>
<body>
<form autocomplete="off" id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextB
<asp:TextBox ID="TextBox2" runat="server" Visible=false></asp:TextBo
</div>
</form>
</body>
</html>
<script language="javascript">
var net=new Object();
net.READY_STATE_UNINITIALI
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIV
net.READY_STATE_COMPLETE=4
/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadXMLDoc(url,method
}
net.ContentLoader.prototyp
if (!method){
method="GET";
}
if (!contentType && method=="POST"){
contentType='application/x
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.X
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechang
net.ContentLoader.onReadyS
}
this.req.open(method,url,t
if (contentType){
this.req.setRequestHeader(
}
this.req.send(params);
}catch (err){
this.onerror.call(this);
}
}
}
net.ContentLoader.onReadyS
var req=this.req;
var ready=req.readyState;
if (ready==net.READY_STATE_CO
var httpStatus=req.status;
if (httpStatus==200 || httpStatus==0){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
}
net.ContentLoader.prototyp
alert("error fetching data!"
+"\n\nreadyState:"+this.re
+"\nstatus: "+this.req.status
+"\nheaders: "+this.req.getAllResponseH
}
function doIt(){
var strValue = document.getElementById("<
var url = 'default.aspx?id=' + strValue;
var strParams = strValue;
var loader1 = new
net.ContentLoader(url,Fill
"POST",strParams);
}
function FillDropDown(){
//alert("returned");
}
</script>
++++++++++++++++++++++++++
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("o
}
if (Request.QueryString["id"]
DoIt();
}
public void DoIt()
{
TextBox1.BackColor = ColorTranslator.FromHtml("
}
}
ASKER
yes, dynamically
var strValue = document.getElementById("< %=TextBox1 .ClientID %>").value;
var url = 'default.aspx?id=' + strValue;
var strValue = document.getElementById("<
var url = 'default.aspx?id=' + strValue;
try this:
function doIt(){
var txtbox = document.getElementById("< %=TextBox1 .ClientID %>");
if (txtbox.value=='Andy')
txtbox.style.backgroundCol or = '#f1f1f1';
}
function doIt(){
var txtbox = document.getElementById("<
if (txtbox.value=='Andy')
txtbox.style.backgroundCol
}
ASKER
I am trying to change the color from the server side (c#) after the xmlhttprequest, prior to callback. You solution is aimed at the javascript.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("o nblur", "doIt()");
}
if (Request.QueryString["id"] == "Andy")
DoIt();
}
public void DoIt()
{
TextBox1.BackColor = ColorTranslator.FromHtml(" #f1f1f1");
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("o
}
if (Request.QueryString["id"]
DoIt();
}
public void DoIt()
{
TextBox1.BackColor = ColorTranslator.FromHtml("
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
e.g. "default.aspx?id=Andy"