Custom Controls with User Controls in ASP.Net: Part 3

Introduction

Continuous from Part - 2, in previous article we have seen that how User Controls are registering in Web.config file to use entirely in website. Now in this article we will discuss how to expose the properties from a User Control. We have noticed that the RandomImage User control always displays an image from the Images folder. It would be nice if we could specify the name of the folder that contains the images so that we could use different folder paths in different applications. We can do this by exposing a property from the RandomImage User control. The modified RandomImage control is now named as PropertyRandomImage, exposes a property named ImageFolderPath.

PropertyRandomImage.ascx File Code

<%@ Control Language="VB" ClassName="PropertyRandomImage" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">

    Private _imageFolderPath As String = "~/images1"

    Public Property ImageFolderPath() As String
        Get
            Return _imageFolderPath
        End Get
        Set(ByVal Value As String)
            _imageFolderPath = value
        End Set
    End Property

    Private Sub Page_Load()
        Dim imageToDisplay As String = GetRandomImage()
        imgRandom.ImageUrl = Path.Combine(_imageFolderPath, imageToDisplay)
        lblRandom.Text = imageToDisplay
    End Sub

    Private Function GetRandomImage() As String
        Dim rnd As New Random()
        Dim images() As String = Directory.GetFiles(MapPath("~/images1"), "*.JPG")
        Dim imageToDisplay As String = images(rnd.Next(images.Length))
        Return Path.GetFileName(imageToDisplay)
    End Function
</script>

<asp:Image
    id="imgRandom"
    Width="300px"
    Runat="server" />
<br />
<asp:Label
    id="lblRandom"
    Runat="server" />


Default.aspx File Code

<%@ Page Language="VB" CodeFile="Default.aspx.vb" Inherits="_Default"%>

<%@ Register TagPrefix="user" TagName="PropertyRandomImage" Src="~/PropertyRandomImage.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:PropertyRandomImage
        ID="PropertyRandomImage1"
        ImageFolderPath="~/images1"
        Runat="server" />
 
    </div>
    </form>
</body>
</
html>

Default2.aspx File Code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<%@ Register TagPrefix="user" TagName="PropertyRandomImage" Src="~/PropertyRandomImage.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        PropertyRandomImage1.ImageFolderPath = "~/images2"
    End Sub
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>

    <user:PropertyRandomImage
        ID="PropertyRandomImage1"
        Runat="server" />

    </div>
    </form>
</body>
</
html>

Note: Continue in next part.

HAVE A GREAT CODING!


Similar Articles