How to use ASP.NET User Control with Place Holder and with Gird view
Rate this article
0 out of 5


Developing Modularized code in web application was allays been a challenge in the past. With the introduction of ASP.NET web technology along with the Web User Control concept , the task of modularization become very simple. In this video tutorial we will show you some of the methods on how data can be populated in from a Database using a Web user control. Specifically it will show you how to use place holder and grid view inside a web user control Watch the Video tutorial below for more details..



Source Code
Default.aspx.cs

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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1Load_Click(object sender, EventArgs e)
    {
        Response.Redirect("Myuser.aspx");
    }
    protected void ButtonLoad_Click(object sender, EventArgs e)
    {
        Response.Redirect("PlaceHolder.aspx");
    }
    protected void Button2Load_Click(object sender, EventArgs e)
    {
        Response.Redirect("GridView.aspx");
    }
}

GridView.aspx.cs
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");
    }
}




Source Code

About The Author
Revathy M S
Number Of articles Posted      : 3
Number Of Messages Posted  : 0
Joined Date : 2014/6/12
   
Author
Messages
Posted:
Post Your Comments  
Name (Max 50 Chars)
Comments
 
 

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android