hello!
i need help to call a function in same aspx file to set the imageURL.
so i have:
in the default.aspx
sub fillImage()

<code>

end Sub
and on the form load i have:
img1.ImageURl= here i want to call the sub "fillImage" in the same aspx file the default.aspx (of course i don't know how to call a function in another aspx file :( ,i know to use img1.ImageURL="get.aspx" only,and i didn't find anything in the internet)


so i need help .
thank you!

Dani AI

Generated

Short answer: do not try to call a sub that writes image bytes into the same page render and expect an Image control to pick that up. That sub can only stream bytes to the current HTTP response (which would corrupt the page HTML). The correct approach is to expose the image as a separate URL and point the Image control at that URL. Moving the DB retrieval into a reusable method/class (as suggested) is the right first step so the same logic can be called from any endpoint.

Typical workflow:

  • Put DB code into a reusable method that returns the raw bytes (byte[]).
  • Create a small HTTP endpoint (an .ashx handler or a lightweight .aspx) that calls that method, sets Response.ContentType, and writes the bytes.
  • Set img1.ImageUrl = "GetImage.ashx?id=" + id; (or the equivalent markup).

Example handler (simplified):

public class GetImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int id = int.Parse(context.Request.QueryString["id"] ?? "0");
        byte[] data = ImageRepository.GetImageBytes(id); // reusable method in App_Code
        if (data != null)
        {
            context.Response.ContentType = "image/jpeg";
            context.Response.OutputStream.Write(data, 0, data.Length);
        }
    }
    public bool IsReusable => true;
}

Practical tips and cautions:

  • Always set the correct ContentType and clear any prior output before writing binary.
  • Add caching headers on the handler to avoid hitting the DB every request.
  • If you prefer not to add an .ashx, the same pattern works with a simple aspx whose Page_Load writes the bytes and then calls Response.End.
  • Avoid Server.Execute/Server.Transfer to “call” another page for image bytes — they are brittle for binary output.

For a formal how-to on HTTP handlers see Microsofts guidance on creating custom handlers:

Recommended Answers

All 2 Replies

ok you have a page where you have created a method which does the required job

so i'm thinking if you

create a class in App_Code folder

make a function in which add your method
which might be fetching some parameters and
returning an imageURL

create a class object in any page you want and call the method

hope that helps and makes your code Reusable !

thank you,but the problem is that this sub doesn't return anything it's do this:
retrieve image from database and than i use response.write,so i can create an aspx file and place this method and it's work but in page_load of the second aspx file but i want to call a funtion in the second or current file which do the response.write.
hope that i cleared what i need.
thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.