Asp.net, Webservice, Ajax, Fetching data dynamically..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dj12345
    New Member
    • Jan 2008
    • 14

    Asp.net, Webservice, Ajax, Fetching data dynamically..

    Hi,

    I have 2 Gridviews on my asp.net page. In this gridview i am showing list of links. I want to update these links dynamically after 5-10 seconds, without refreshing the page(without postbak) i want to fetch these link dynamically and want to bind it to Gridview..

    I have little bit idea about this that. use the webservice to fetch record like we do it in Ajax AutoComplete. But how to fetch record dynamically and bind it to Gridview..

    Please can anybody tell me steps to do that..


    Thanks

    Yogesh Sapkale
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    Asp.net, Webservice, Ajax, Fetching data dynamically..

    ok fine.. i would just give an example of get data frm database and do databind . in C#

    1. drag a GridView in the design page
    2. declear the namespace in code behind page, call the Page_Load event

    design page

    Code:
    drag a GridView
    
     <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    code behing page

    declear namespace
    Code:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Globalization;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    
    public partial class Default2 : System.Web.UI.Page
    {
        SqlConnection myConnection;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            // Make a connection to the database
            myConnection = new SqlConnection("server=servername; Initial Catalog=database name; User ID=username; password=password");
            
        // get the data frm database when page load
            if (!IsPostBack)
                BindGrid();
    
        }
        
        public void BindGrid()
        {
           SqlDataAdapter myCommand1 = new SqlDataAdapter("select * from tablename ", myConnection);
    
           
            DataSet ds = new DataSet();
           myCommand.Fill(ds, "tablename");
    
            GridView1.DataSource = ds.Tables["tablename"].DefaultView;
            GridView1.DataBind();
            myCommand.Connection.Close();
        }
    
    
    }
    hope that gonna help you ...

    Comment

    • AnaOlinto
      New Member
      • Aug 2009
      • 4

      #3
      But this code is Binding the GridView with PostBack, isn't it? Is there a way to bind using AJAX?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        When you use Ajax in .NET the whole page ASP page life cycle happens.

        This means that if you place your GridView in an update panel and then use a Ajax Timer control to periodically cause an asynchronous post back to the page then everything server side is executed.

        When the postback is sent down to the browser, the HTML is stripped down by .NET to only include what needs to be updated (which is within the UpdatePanel).

        In my mind, it's sort of a .NET cheat. If you use pure Ajax, this is much different.

        Anyways, just try adding an UpdatePanel to the page, and place the GridView in the UpdatePanel. Place a Timer control in the UpdatePanel and have it periodically cause an asynchronous postback to the server so that the GridView is refreshed (not the whole page).

        -Frinny

        Comment

        Working...