How do I retrieve a variable in an ASP.Net server control?


J

JimCinLA

Pardon. This question seems incredibly dumb but I seem to
be suffering a brain block. I want an ASP.Net 2.0 image control
to contain a variable for the image file name as shown...


<asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName
%>" />

The variable myImageFileName is set in either the Page_Init or
Page_Load
event handler. The markup appears on a master page which is used
used by .aspx pages in different folders, and at different levels of
indirection with respect to the project root.

Is it that the script is being rendered before Page_Init?
Doesn't sound right. In fact, if I write

<td><% = myImageFileName %></td>

the file name shows up on the rendered page as expected.

I believe the previous image tag works if the feature is a client-
side
HTML tag. That is,

<img src="<%=myImageFileName %>" alt="" />

will *probably* produce the desired result. However, I have used an
active control because the path name in the file name contains the
"~" character, representing the project root which of course
is meaningless in an HTML tag.
 
Ad

Advertisements

M

Michael Nemtsev [MVP]

Hello JimCinLA,

have u tried to use # sign instead of = ?
Like <img src="<%#myImageFileName %>" alt="" />

or create the function which returs the full path to the image and use this
function in <%=.... %>

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


J> Pardon. This question seems incredibly dumb but I seem to
J> be suffering a brain block. I want an ASP.Net 2.0 image control
J> to contain a variable for the image file name as shown...
J> <asp:Image runat="server" ID="myImage" ImageUrl = "<%=myImageFileName
J> %>" />
J>
J> The variable myImageFileName is set in either the Page_Init or
J> Page_Load
J> event handler. The markup appears on a master page which is used
J> used by .aspx pages in different folders, and at different levels of
J> indirection with respect to the project root.
J> Is it that the script is being rendered before Page_Init? Doesn't
J> sound right. In fact, if I write
J>
J> <td><% = myImageFileName %></td>
J>
J> the file name shows up on the rendered page as expected.
J>
J> I believe the previous image tag works if the feature is a client-
J> side
J> HTML tag. That is,
J> <img src="<%=myImageFileName %>" alt="" />
J>
J> will *probably* produce the desired result. However, I have used an
J> active control because the path name in the file name contains the
J> "~" character, representing the project root which of course
J> is meaningless in an HTML tag
 
Ad

Advertisements

S

Stratum

Hello JimCinLA,

have u tried to use # sign instead of = ?
Like <img src="<%#myImageFileName %>" alt="" />

or create the function which returs the full path to the image and use this
function in <%=.... %>


Thanks, yes. But as I explained, it is advantageous to use the
server
control, that is <asp:Image so that the image file's path works for
different levels of indirection. The image file name is stored in a
database. The problem is that I can't seem to make the
following work:

<asp:Image runat="server" ID="myImage"
ImageUrl = "<%# myImageFileName %>" />

while the code you suggest does work but needs a different
file path depending on which page accesses it.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top