Sunday, May 04, 2008

AJAX GridView Databinding























C# code to Load the GridView using AJAX Asynchronous Postback

protected void bindGridView()
{
string connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
SqlConnection sqlCon = new SqlConnection(connectionString);

try
{
SqlCommand sqlCmd = new SqlCommand("select * from categories", sqlCon);
sqlCmd.CommandType = CommandType.Text;
SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
DataSet myDataSet = new DataSet();
sqlAdp.Fill(myDataSet);

// Delay the current Thread to display
// the UpdateProgress status
// Not required in real projects
System.Threading.Thread.Sleep(4000);

GridView1.DataSource = myDataSet;
GridView1.DataBind();
}
catch(Exception ex)
{
lblErrorMsg.Text = ex.Message;
}
}

protected void btnLoadData_Click(object sender, EventArgs e)
{
bindGridView();
}