Kerberos
Kerberos

Reputation: 1266

Asp.net: Calling class from codebehind in asp file

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).

Upvotes: 1

Views: 2127

Answers (2)

rick schott
rick schott

Reputation: 20617

# 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;
}

Upvotes: 0

Binoj Antony
Binoj Antony

Reputation: 16186

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_") %>

Upvotes: 2

Related Questions