1

At the moment i have this:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="Drawing.Usercontrols.WebUserControl2" %>
<asp:Image ID="Image1" runat="server" alt="Image Not Found" ImageUrl="~/images/k8up7l1i.bmp"/>

in the webUserControl2.ascx page.

In WebUserControl2.ascx.cs page, all of the logic is found. An Image is generated there, and then saved to a path (C:\Work Drawing\Drawing\Drawing\images) where:

string thisPathName = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/" + randomFileName);

My question is, can i call a method "getPathName()" from the asp page (where ImageUrl is, instead of a static URL). So each time i call it, the new image will be displayed in the browser with that pathname generated in the c# code?

Please notify me if the question was unclear.

2 Answers 2

5

the answer is Yea , you can .

let's assume you have this function in code behind:

public string getPathName(){
    return "my_image_path.jpg";
}

by that , you can have this in the form :

<img alt="Image Not Found" src="<%=getPathName()%>"/>

and there you go . you get you'r image path from the code behind .

good luck .

Sign up to request clarification or add additional context in comments.

9 Comments

Splendid! However, the page displays "image not found", it also doesn't seem to call the method when i put breakpoints. <asp:Image ID="Image1" runat="server" alt="Image Not Found" ImageUrl="<%=getPathName()%>"/> public string getPathName() { string pathName = "~/images/k8up7l1i.bmp"; return pathName; } Do i need to initiate or invoke something?
no you don't need to invoke something . you just need to change the img from server tag to html tag . check the edited answer
It is following my breakpoints now with the edited code, which is good! Except that image not found is still displayed whilst using the mentioned pathname as a hardcoded parameter.
client side html tags do not understand what is ~ means . you should specify the full path .
Cool it seems to work now. <asp:Image ID="Image1" runat="server" alt="Image Not Found" ImageUrl="<%=this.getPathName()%>"/> -=-=-=-=-=-=-= Image1.ImageUrl = getPathName(); -=-=-=-=-=-=-= public string getPathName() { return "~/images/" + randomFileName; //where randomfilename = 12341263.bmp for example } -=-==-=-=-=-=-==-=-
|
0

You can't assign function call result to server control property (like ImageUrl) unless that control is within data-bound control like GridView or Repeater.

What you can do is to set ImageUrl in code behind e.g. in PageLoad event:

Image1.ImageUrl = getPathName();

or, if you want to do it from asp page and you don't need to reference Image1 from server, render it as html element:

<img alt="Image Not Found" src='<%= this.getPathName %>' />

getPathNmae must be 'protected' or 'public' for above to work.

1 Comment

It is following my breakpoints now with the edited code, as mentioned in the other comment. Except that image not found is still displayed whilst using the mentioned pathname as a hardcoded parameter. I have yet to try the Image1.ImageUrl = getPathName(); solution, i wonder if it is possible to change the images like this dynamically in runtime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.