Web User Controls

Web Site -> Add New Item -> Web User Control

Then you design the user control as a standard web site and drag&drop it from the Solution Explorer to your web site.

 

In Default.aspx.cx:

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        string street = AddressBox1.Street;

        string city = AddressBox1.City;

        AddressBox2.Street = street;

        AddressBox2.City = city;       

    }

}

 

You can’t access directly the controls on the web user control. To do this you must create properties and define get and set statements.

in AddressBox.ascx.cs:

 

public partial class AddressBox : System.Web.UI.UserControl

{

 

        public string Street

        {

            get { return TextBox1.Text; }

            set { TextBox1.Text = value; }

        }

 

        public string City

        {

            get { return TextBox2.Text; }

            set { TextBox2.Text = value; }

        }

 

}

Source code