How to expose property from User Control ?


dynamic Image
Hi,

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

UserControl_Property

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>


Dynamic FolderName

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.