[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

AJAX - textbox won't change background color

Posted on 2006-04-11
6
Medium Priority
?
422 Views
Last Modified: 2012-08-13
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:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" Visible=false></asp:TextBox>
    </div>
    </form>
</body>
</html>
<script language="javascript">
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  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.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}


net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==net.READY_STATE_COMPLETE){
    var httpStatus=req.status;
    if (httpStatus==200 || httpStatus==0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}  


function doIt(){                          
        var strValue = document.getElementById("<%=TextBox1.ClientID %>").value;                      
        var url = 'default.aspx?id=' + strValue;                              
        var strParams = strValue;                                      
        var loader1 = new                                              
        net.ContentLoader(url,FillDropDown,null,                      
                    "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("onblur", "doIt()");
        }
       
        if (Request.QueryString["id"] == "Andy")
            DoIt();
    }

    public void DoIt()
    {
        TextBox1.BackColor = ColorTranslator.FromHtml("#f1f1f1");
    }
   
}
0
Comment
Question by:sutorius
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:aki4u
ID: 16430986
do you have in address path  "id=Andy" ?
e.g. "default.aspx?id=Andy"
0
 
LVL 2

Author Comment

by:sutorius
ID: 16431122
yes, dynamically

var strValue = document.getElementById("<%=TextBox1.ClientID %>").value;                      
        var url = 'default.aspx?id=' + strValue;
0
 
LVL 10

Expert Comment

by:aki4u
ID: 16431680
try this:

function doIt(){
var txtbox = document.getElementById("<%=TextBox1.ClientID %>");
if (txtbox.value=='Andy')
      txtbox.style.backgroundColor = '#f1f1f1';
}              
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 2

Author Comment

by:sutorius
ID: 16431717
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("onblur", "doIt()");
        }
       
        if (Request.QueryString["id"] == "Andy")
            DoIt();
    }

    public void DoIt()
    {
        TextBox1.BackColor = ColorTranslator.FromHtml("#f1f1f1");
    }
   
}
0
 
LVL 10

Accepted Solution

by:
aki4u earned 2000 total points
ID: 16431781
you will not be able to because it's not a real postback.
0
 
LVL 2

Author Comment

by:sutorius
ID: 16435647
Thank you.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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