Properties in ASP.Net User Control

Background

 
I have often read a common question in forum posts of how to set the values of a User Control from a parent page in ASP.NET but no one has provided the proper solution so by considering the preceding requirements I have decided to write this article to provide the step-by-step solution to create a User Control. So let us start creating an application so beginners can also understand.
 
What User Control Properties Are
 
User Control properties are used to set the values of a User Control from the parent page.
 
For more details of how to create a User Control please refer to my previous article:

So let us learn practically about User Controls in depth.

Step 1: Create Web Application
 
Now let us create the sample web application as follows:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).

  3. Provide the web site a name such as "PropertiesinUsercontrol" or another as you wish and specify the location.

Step 2: Create the User Control

  1. Then right-click on the project in the Solution Explorer then select "Add New Item" then select Web User Control template as in the following:


Now open the design mode and add the two textboxes. After adding it, the User Control  source code will look as follows: 
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="student.ascx.cs" Inherits="student" %>  

  2. <h3>This is User Contro1   </h3>   
  3. <table>  
  4.   
  5. <tr>  
  6. <td>Name</td>  
  7. <td>  
  8.   
  9.     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  10. </td>  
  11. </tr>  
  12. <tr>  
  13. <td>City</td>  
  14. <td><asp:TextBox ID="txtcity" runat="server"></asp:TextBox></td>  
  15. </tr> 
 
 
Step 3: Create Properties in User Control
 
Write the following code in the student.ascx.cs file to create the properties as: 
  1. public string Name  
  2. {  
  3.     get { return txtName.Text; }  
  4.     set { txtName.Text = value; }  
  5. }  
  6.   
  7. public string City  
  8. {  
  9.     get { return txtcity.Text; }  
  10.     set { txtcity.Text = value; }  

In the preceding code we are setting the values to the User Control textboxes, now after creating the properties the student.ascx class file will look as in the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class student : System.Web.UI.UserControl  
  9. {  
  10.       
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.   
  14.     }  
  15.      
  16.   
  17.     public string Name  
  18.     {  
  19.         get { return txtName.Text; }  
  20.         set { txtName.Text = value; }  
  21.     }  
  22.   
  23.     public string City  
  24.     {  
  25.         get { return txtcity.Text; }  
  26.         set { txtcity.Text = value; }  
  27.     }  
  28.     

Now we are done with the properties. Let us use it in an .aspx page. As already discussed in the preceding, a User Control does not run directly on its own. To render a User Control you must use it in an .aspx page, now let us add the User Control to the .aspx page.
 
Step 4: Adding User Control into .aspx page
 
Now we need to add the User Control into an .aspx page to use it. So let us add the Default.aspx page by right-clicking on the project in the Solution Explorer. After adding the .aspx page the Solution Explorer will look such as follows:
 
 
 
Step 5: Register the User Control on .aspx page
 
To use a User Control in an .aspx we need to register it using the Register page directive, the register page directive has the following properties:
  1. <%@ Register Src="~/student.ascx" TagPrefix="uc" TagName="Student" %> 
Now open the default.aspx page and drag and drop two text boxes, one button and also add a control to the .aspx page form the section. After adding the Default.aspx the source code will be such as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <%@ Register Src="~/student.ascx" TagPrefix="uc" TagName="Student" %>  
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7.   
  8. <head id="Head1" runat="server">    
  9.     <title>Article by Vithal Wadje</title>    
  10. </head>    
  11. <body bgcolor="blue">    
  12.     <form id="form2" runat="server">    
  13.     <div style="color: White;">    
  14.         <h4>    
  15.             Article for C#Corner    
  16.         </h4>    
  17.     <table>  
  18.   
  19. <tr>  
  20. <td>Name</td>  
  21. <td>  
  22.   
  23.     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
  24. </td>  
  25. </tr>  
  26. <tr>  
  27. <td>City</td>  
  28. <td><asp:TextBox ID="txtcity" runat="server"></asp:TextBox></td>  
  29. </tr>  
  30. <tr>  
  31. <td></td>  
  32. <td>  
  33.      </td>  
  34. </tr>  
  35. <tr>  
  36. <td></td>  
  37.   
  38. <td>   
  39.     <asp:Button ID="txtSave" runat="server" Text="Save" onclick="txtSave_Click" />  </td>  
  40. </tr>  
  41. </table><br />  
  42.       
  43.     <uc:Student ID="studentcontrol" runat="server" />  
  44.   
  45.     </div>  
  46.     </form>  
  47.       
  48. </body>  
  49. </html> 
Now switch to design mode and it will look as in the following:
 
 
In the preceding UI, on the save button we will the values to the user text box controls and I will show it in the User Control text box, now let us see how properties will look, its similar to any standard TextBox control as:
 
 
 
Step 6: Set the values to User Control from .aspx page using User Control properties
 
Now in the preceding control you have seen that the properties of the User Control we have created are shown the same as standard contols. We can also create properties to set the height and width and so on in a similar manner. Double-click on the Save button and write the following code in the Default.aspx.cs file to set the values:
  1. protected void txtSave_Click(object sender, EventArgs e)  
  2. {  

  3.     //setting the values to the User Control properties   
  4.     studentcontrol.Name = txtName.Text;  
  5.     studentcontrol.City = txtcity.Text;  

 Now the entire code of the Default.aspx class file will look as in the following:
  1. using System;  
  2.   
  3. public partial class _Default : System.Web.UI.Page  
  4. {  
  5.     protected void Page_Load(object sender, EventArgs e)  
  6.     {  
  7.   
  8.     }  
  9.     protected void txtSave_Click(object sender, EventArgs e)  
  10.     {  
  11.   
  12.         //setting the values to the User Control properties   
  13.         studentcontrol.Name = txtName.Text;  
  14.         studentcontrol.City = txtcity.Text;  
  15.     }  

Now run the application, the UI will look as in the following:
 
 
Now enter the name and city into the preceding two text boxes and click on the save button. The output will be shown in the User Control text boxes as follows: 
 
 
 
Now you have seen that we do not have any code in the .ascx  page that is in the User Control but still the output is shown because all the logic is written in the User default.aspx page and the values are assigned using properties to the User Control from the parent page.

Note
  • For more details and explanation, download the Uploaded Zip file.
Summary

From all the preceding examples you have learned how to use the properties in a User Control to assign the values. I hope this article is useful for all readers, if you have a suggestion then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all