CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    35

    Ajax - Fetching Records from DB

    I have dropdown & Gridview on FrmSatelliteMain,In dropdown DataValueField uid is there,Now i want that as the user selects
    the value in dropdown ,on basis of uid i will fetch the records fromm DB & bind to Gridview using AJAX.


    Plz frnds dont tell me dat,dat do it using AJAX framework!!!!!

    Code:
    public partial class FrmSatelliteMain : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=(local);Initial catalog=SatelliteTVonPV;User ID=sonia;Password=sonia");
        string Query;
        SqlCommand cmd;
           SqlDataReader dr;
    
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                DropDownLanguages.Attributes.Add("onChange", "javascript:return AddChannels()"); 
    
            }
        }
    
    
    }


    SOURCE CODE-
    In DropDownLanguages in DataValueField,UID is there.I want to retrive all the records from DB that has UID 1 in country column.

    Code:
     <script type ="text/javascript" language ="javascript">
        var xmlHttp;
             
        
        function AddChannels()
        {
                 
          checkXHR("Retrieve.aspx?&uid="+document.getElementById("DropDownLanguages").value);
    }
    
         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="";
           }         
        }


    FRMRETRIVE.aspx


    Code:
    public partial class Retrieve : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from [pctv] where Country='" +
                             Request["uid"].ToUpper() + "'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
        }
    }



    In FrmRetrive form load,Records are coming,But i dont know that how to bind this data with Gridview of FrmSatelliteMain!! Can somebody tell me dat???????????I want to bind all the columns excluding UID,category & Country!!

    DB Columns
    UID
    Country
    Category
    Language
    Channel Name
    Channel URL
    TV RATING

  2. #2
    Join Date
    Sep 2009
    Posts
    10

    Re: Ajax - Fetching Records from DB

    I'm not sure how to bind to a datagrid that's already rendered on the page through AJAX. I do have a similar alternative to propose

    You could:
    Replace the entire rendered grid control with an entirely new grid control rendered via your ajax routine. Basically in your code behind after you've populated your DataSet object (ds) create an instance of an HtmlTextWriter and render a bran new DataGrid on it. Then you can retrieve the rendered html and send the complete rendered html back to the client like this:

    public string grid=null;
    protected void Page_Load(object sender, EventArgs e)
    {
    SqlDataAdapter da = new SqlDataAdapter("select * from [pctv] where Country='" +
    Request["uid"].ToUpper() + "'", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);

    System.Web.UI.WebControls.DataGrid dg=new DataGrid();
    dg.ID="YOUR CONTROLS ID";
    dg.DataSource=ds.Tables[0];
    dg.DataBind();

    StringWriter sw=new StringWriter();
    HtmlTextWriter writer=new HtmlTextWriter(sw);

    dg.Render(writer);

    //retrive rendered code
    this.grid=sw.ToString();

    ds.Dispose();
    ds=null;

    dg.Dispose();
    dg=null;
    }

    protected override void Render(HtmlTextWriter writer)
    {
    writer.Write(this.grid);
    }


    Then in your javascript file make it look something like this:
    function statechanged()
    {
    if(xmlHttp.readyState==4)
    {
    document.getElementById("GRID_PARENT_ELEMENT").innerHTML= xmlHttp.responseText;
    }
    else
    {
    document.getElementById("GRID_PARENT_ELEMENT").innerHTML="";
    }
    }


    There are probably some mis-typings in there somewhere... I just typed it real quick from memory. Hope that helps!

  3. #3
    Join Date
    Sep 2009
    Posts
    10

    Re: Ajax - Fetching Records from DB

    Another great part about this is that the grid retrieved via ajax will interact with the server side code running on the page the original grid is embeded as long as both grids are give the ID attribute the same value.

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

Featured