While working with user control in asp.net , you will get scenario to expose property from user control to make it dynamic.
For example , you want to create user control to display random images on page refresh from some image folder. Now you want to make that Image folder path as dynamic then you have to expose property from user controls like this.
Step 1: Create the User Control Like this
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PropertyTest.ascx.cs" Inherits="PropertyTest" %> <asp:Image ID="imgRandom" Width="200px" Height="200px" runat="server" /> <br /> <asp:Label ID="lblRandom" runat="server"></asp:Label>
Step2: Write the code behind file for UserControl like this
Step 3: Now implement the usercontrol in asp.net page and give the folder name of image like this code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PropertyTest.aspx.cs" Inherits="PropertyTest" %> <%@ Register TagPrefix="user" TagName="PropertUserControl" Src="~/PropertyTest.ascx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <user:PropertUserControl ID="Test" ImageFolderPath="Img" runat="server" /> </div> </form> </body> </html>