robertjuh
robertjuh

Reputation: 311

Dynamic imageURL in asp.net c#

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.

Upvotes: 1

Views: 10563

Answers (2)

Lukas Safari
Lukas Safari

Reputation: 1968

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 .

Upvotes: 5

PrzemG
PrzemG

Reputation: 795

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.

Upvotes: 0

Related Questions