How to expose property from User Control ?

dynamic Image

While working with user control in , 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 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" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">

       <user:PropertUserControl ID="Test"  ImageFolderPath="Img" runat="server" />


Dynamic FolderName


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.