Using properties in an ASP.net Web Control

When creating a web control in an ASP.net application many times you will need to make references to information, data values and other contents of the web control from the web control parent. This is done by adding a Property statement on the control. It could be as simple as a Username and Password control to set or get the information at times of requesting login details.

Follow these steps to connect and use data on the User Web Control:

  1. Create a new Web Control in your application project
  2. Place two label and two textboxes on the control
  3. Name the two controls txtUsername and txtAddress
  4. In the code behind of the control add the following code (C#)private string username = String.Empty;
    private string password; = String.Empty;
  5. public string Username {
    get { return username; }
    set { set username = value; }
    }

    public string Password{
    get { return password; }
    set { set password; = value; }
    }

  6. Close the control designer windows, saving all changes
  7. Open the web page that will use the control
  8. Drag and drop the new web control onto the web page canvas (if you already had the web control on your webpage and you added new interface elements (public functions or properties), you will likely need to remove the controls including the REGISTER statements at the top of the page so that the interface elements are reconnected to the page for you.
  9. In the code behind the web page, make any references to the Tag name of the control (assuming “uc1” in this case) using the following statements (C#).this.uc1.Username = “Your username”;
    this.uc1.Password{ = “Your password”;

This concludes the tutorial to add a simple web control and connect to it from the parent web page where it resides. This concept will help in many ways like, login pages, bread crumbs, contained lists, or other user interface lists.

Leave a Reply