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 DrawingDrawingDrawingimages)
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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 .
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0