Results 1 to 5 of 5

Thread: Ajax

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2009
    Posts
    60

    Ajax

    I know how to fetch the records from DB,But i want to ask u just small thing.

    SQL TABLE
    Code:
    roll                name                  marks
    1                    Ritu                       10
    2                    Sneha                    20
    3                    Ruchi                       30


    CODE BEHIND PAGE
    Default.aspx

    Code:
     protected void Page_Load(object sender, EventArgs e)
        {
            cmbRoll.Items.Add("--Select--");
            cmbRoll.Items.Add("1");
            cmbRoll.Items.Add("2");
            cmbRoll.Items.Add("3"); 
     
            cmbRoll.Attributes.Add("onChange", "javascript:return check()");  
     
        }

    JS CODE
    Code:
     <title>Untitled Page</title>
        <script type ="text/javascript" language ="javascript">
        var xmlHttp;
        var WholeString;
        var col_array;
        
        function check()
        {
        var Roll=document.getElementById("<%=cmbRoll.ClientID %>").value;
         //checkXHR("Retrieve.aspx?Roll=" + Roll + "&sid="+Math.random());
        checkXHR("Retrieve.aspx?Roll=" + Roll );
                      
        WholeString=document.getElementById("hd1").value;
         alert (WholeString); //Result of Particular roll No that is fetched from DB thru AJAX
         col_array=WholeString.split("^^^^");
        for (i = 0; i <= col_array.length; i++)
        {
         
        document.getElementById("txtName").value=col_array[1];
         document.getElementById("txtMarks").value=col_array[2];
          
        }
          
            xmlHttp=null;
                    return true;
        }
        
        function checkXHR(url)
        {
           try
           {
             xmlHttp=new XMLHttpRequest();        
           }
           catch (e)
           {
             try
             {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
             }
             catch (e)
             {
                try
                {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                
                }
                catch (e)
                {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
             }
           }
          
           xmlHttp.open("GET",url,false);       
           xmlHttp.onreadystatechange=statechanged;
           xmlHttp.send(null);
        }
        
        function statechanged()
        {
           if(xmlHttp.readyState==4)
           {
             document.getElementById("hd1").value = xmlHttp.responseText;
           }
           else
           {
             document.getElementById("hd1").value="";
           }         
        }


    Retrieve.aspx
    Code:
      protected void Page_Load(object sender, EventArgs e)
        {
            string a;
            string b;
            string c;
    
            SqlDataAdapter da = new SqlDataAdapter("select * from [Information] where upper(roll)='" +
                               Request["Roll"].ToUpper() + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
                  
            foreach(DataRow row in ds.Tables[0].Rows) 
            {
                a = row[0].ToString();
                b = row[1].ToString();
                c = row[2].ToString();
      
                Response.Write(a + "^^^^" + b + "^^^^" + c);
             
            }



    I take hidden field in Default.aspx & as u know response.write to hidden field..SO in Default.aspx I m getting my result back in variable in the line
    Code:
     WholeString=document.getElementById("hd1").value;

    Now I have employee info in database & i want to bind it to the Gridview using AJAX...Even for that i have to return value in the above way or there is other way out..

    Plz help me out!!!

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Ajax

    I observe that though you are using a database and all data is comming from database, you are not using Data binding. Instead you loop and fill the controls. Is there and reason you are avoiding that?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Ajax

    Go for the ASP.NET AJAX Framework. You can surround the grid with an UpdatePanel and then use the results of your codebehind method or page to populate the gridview.

  4. #4
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Ajax

    Code:
    try
                {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                
                }
                catch (e)
                {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
    What if the browser is running on Linux? I would second Mendhak's advice. Have a look at AJAX framework and all the hard work related XMLHTTP is now taken care of. I hate those times.

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Ajax

    Quote Originally Posted by rjv_rnjn View Post
    What if the browser is running on Linux?
    Don't forget Mono.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width