views:

299

answers:

2
public string GetRandomImage(string StrDirectory, string StrFileName)
{
    Response.Write("Test: GetRandomImage True");
    string GetRandomImage;
    int IntFileCount = Directory.GetFiles(Server.MapPath(StrDirectory), "*.*", SearchOption.TopDirectoryOnly).Length;
    Random Random1 = new Random();
    IntFileCount = IntFileCount + 1;
    GetRandomImage = StrDirectory + StrFileName + Random1.Next(1, IntFileCount) + ".png";
    Response.Write(GetRandomImage);
    return GetRandomImage;
}

this code is in my codebehind file (default.aspx.cs). i want to call it from my default.aspx file. I tried to call with

<%# GetRandomImage("images/random/","random_") %>

but i have get error. How can I do this? Thank you for all helper(s) and your help(s).

+1  A: 

You can call it with the fully qualified namespace if its a static method or with a this if its a page method. Use an equal sign instead of hash

<%= this.GetRandomImage("images/random/","random_") %>
Binoj Antony
I could not understand. What you told me.
Kerberos
I have updated the post..
Binoj Antony
OK i'll try to explain more. i'll edit my post soon.
Kerberos
First, i want to thank you for your help. I have used your suggestion. I have not get error but it has not return string value. So i have solved my issue in codebehind's Page_Load event with using asp.net's imageURL attribute of image control. Thank you again.
Kerberos
A: 

# requires a call to DataBind() on the control.

protected string GetRandomImage(string StrDirectory, string StrFileName)
{
    Response.Write("Test: GetRandomImage True");
    string GetRandomImage;
    int IntFileCount = Directory.GetFiles(Server.MapPath(StrDirectory), "*.*", SearchOption.TopDirectoryOnly).Length;
    Random Random1 = new Random();
    IntFileCount = IntFileCount + 1;
    GetRandomImage = StrDirectory + StrFileName + Random1.Next(1, IntFileCount) + ".png";
    Response.Write(GetRandomImage);
    return GetRandomImage;
}
rick schott