using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Core for dynamicallly load the user Control
Control Gctl = Page.LoadControl("GridViewUserControl.ascx");
}
}
GridViewUserControl.ascx.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
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 GridViewUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Code for database connection
OleDbConnection StudentConnection = new OleDbConnection();
StudentConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=";
StudentConnection.ConnectionString += Server.MapPath("StudentData.mdb");
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
cmd.CommandType = CommandType.Text;
//code for select data from the database
cmd.CommandText = "SELECT * FROM Student";
cmd.Connection = StudentConnection;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
StudentConnection.Open();
da.Fill(dt);
StudentConnection.Close();
//bind grid view with database
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
MyUserControl.ascx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Myuser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control Gctl = Page.LoadControl("MyUserControl.ascx");
}
}