Pass parameter to page control (C#) : Load Control « Page « ASP.Net






Pass parameter to page control (C#)

/*
ASP.NET 2.0 Unleashed (Unleashed) (Hardcover)
by Stephen Walther 

# Publisher: Sams; Bk&CD-Rom edition (June 6, 2006)
# Language: English
# ISBN: 0672328232
*/      




<%@ Page Language="C#" %>
<%@ Register TagPrefix="user" TagName="PropertyRandomImage" Src="PropertyRandomImage.ascx" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        PropertyRandomImage1.ImageFolderPath = ".";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Programmatic</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <user:PropertyRandomImage
        ID="PropertyRandomImage1"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>


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

    private string _imageFolderPath = "~/Images"; 

    public string ImageFolderPath
    {
        get { return _imageFolderPath; }
        set { _imageFolderPath = value; }
    }

    void Page_Load()
    {
        string imageToDisplay = GetRandomImage();
        imgRandom.ImageUrl = Path.Combine(_imageFolderPath, imageToDisplay);
        lblRandom.Text = imageToDisplay;
    }

    private string GetRandomImage()
    {
        Random rnd = new Random();
        string[] images = Directory.GetFiles(MapPath("."), "*.jpg");
        string imageToDisplay = images[rnd.Next(images.Length)];
        return Path.GetFileName(imageToDisplay);
    }
</script>

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








Related examples in the same category

1.Add asp:TextBox to page dynamically (VB.net)
2.LoadControl method example (VB.net)
3.Find control in a page (C#)
4.Use propery defined in asp.net control (C#)